0

I have a dictionary as shown in the picture and I'm wondering how should I code something in to check for each value for each key and check them to see if they meet a certain condition like a value above 8 and such.

http://i.imgur.com/qtfkK61.png

The values are float and there are multiple values per a dictionary value of region.

def main(): #defining main function
    magList = [] #magnitude list of all together
    regionList = [] #creating list to hold region names
    wholeList = []
    combinedList = []

    with open("earthquakes.txt", "r") as eqList: #opens earthquake text file and gets the magnitudes
        eqList.readline()
        for line in eqList:
            line = line.split()
            magList.append(float(line[1])) #appends magnitude as float values in list

    with open("earthquakes.txt", "r") as eqList2: #open file 2nd time for gathering the region name only 
            eqList2.readline()
            for line in eqList2:
                line = line.split()
                line = line[0:7] + [" ".join(line[7:])] #takes whole line info and adds to list
                wholeList.append(line)

    greatMag = [] #creating lists for different category magnitudes
    majorMag = []
    strongMag = []
    moderateMag = []

    for x in magList: #conditions for seperating magnitude
        if x >= 8:
            greatMag.append(x)
        elif  7 <= x <= 7.9:
            majorMag.append(x)
        elif 6 <= x <= 6.9:
            strongMag.append(x)
        elif 5 <= x <= 5.9:
            moderateMag.append(x)

    for line in wholeList: #takes only magnitude and region name from whole list
        combinedList.append([line[7], line[1]])

    infoDict = {} #creates dictionary    
    for key, val in combinedList: #makes one key per region and adds all values corresponding to the region
        infoDict.setdefault(key, []).append(val)
    print(infoDict)

    for k, v in infoDict.items():
        for i in v:
            if i >= 8:
                print("Great Magnitude:", i)
                pass

if __name__ == "__main__": #runs main function
    main()
Lompang
  • 105
  • 2
  • 11
  • It would be helpful if you pick some sample from your dictionary and post here. – Tanveer Alam Dec 04 '14 at 05:15
  • Possible duplicate? http://stackoverflow.com/a/23862438/1174169. If not, it's definitely related. That filters a dictionary based on the keys using a dictionary comprehension. Also, what do you want *to do* with the data that meets your criteria? – cod3monk3y Dec 04 '14 at 05:17
  • added my code, you can't run it since you guys do not have the text file but just to look at how i've done it near the end – Lompang Dec 04 '14 at 05:25
  • The values are lists of strings. – wwii Dec 04 '14 at 05:28

1 Answers1

0
for k,v in d.items(): # iterate through key, value of dict 'd'
  for i in v: # iterate to dict value which is a list
    if i > 8: # your condition
      # do something here
      pass
orange
  • 6,483
  • 12
  • 52
  • 113
  • The line "if i>=8" gives me an error if i >= 8: TypeError: unorderable types: str() >= int() I ran into the same problem doing a similar style of how you have done it before coming here. It shouldn't be giving me a string comparison error since the values are floats not strings but it does anyways. – Lompang Dec 04 '14 at 05:20
  • @Lompang, look close at your data - your *floats* are actually strings, try ```if float(i) > something:``` – wwii Dec 04 '14 at 05:26
  • Ah I forgot about that you can change it to a float when you do a condition, that makes sense, thank you! – Lompang Dec 04 '14 at 05:29