python - Check if complex values of dict matches certain criteria -
i have python dictionary values being list of list:
dictionary={'sca4': [['bm1', 17], ['bm2', 33]], 'sca6': [['gm2', 46], ['gm2', 67], ['bm2', 17]]}
please note number can repeated in different keys, 17 in case.
i have text file following format:
sca4 15 25 sca4 20 32 sca6 45 62
i parsed text file follows:
for line in textfile.split("\n"): if not line.strip(): continue col1, col2, col3 = line.strip().split(" ")
i want see if each of second elements of each list in dictionary (that 17, 33, 46, 67) falls within range of interval of col2 , col3 of text file following code:
for value_list in dictionary.get(col1, []): if int(col2) <= value_list[1] <= int(col3): print "withinrange" else: print "outsiderange"
to have output:
wihtinrange outsiderange withinrange outsiderange
i don't error neither output. great if 1 comment on code , how debug it.
have added answer requirements specified in comments of previous posted answer:
def printlist(l, dict1): l1 in l: if dict1.has_key(l1): print "withinrange" else: print "outsiderange" file = open("test1.txt") textfile = file.readlines() dict={'sca4': [['bm1', 17], ['bm2', 33]], 'sca6': [['gm2', 46], ['gm2', 67], ['bm',17]]} dict1 = {} l =[] key = '' line in textfile: if not line.strip(): continue col1, col2, col3 = line.strip().split(" ") if not (key == col1): printlist(l, dict1) l =[] key = col1 dict1 = {} value_list in dict.get(col1, []): #print value_list[1] if not (value_list[1] in l): l.append(value_list[1]) if ((int(col2) <= value_list[1]) , (value_list[1] <= int(col3))): dict1[value_list[1]] = 'correct' printlist(l, dict1)
Comments
Post a Comment