I am new to python, so correct me if this is not the best/fastest way of doing this. I have created a dictionary with multiple values assigned to each key. In codonDict
I have included only 1 key with a few of its values (there will be a lot more). Now I have a file which I have called calls
here. What I want to do is find the key that corresponds to the #CHROM
in the file and then search through the keys values to see if it contains the corresponding POS
.
codonDict = defaultdict(<type 'list'>, {'HE667775': [106690, 106692, 106694, 106696, 106698, 106700, 106702, 106704, 106706, 106708, 106710, 106712, 106714, 106716, 106718, 106720, 106722, 106724, 106726, 106728, 106730, 106732, 106734, 106736, 106738, 106740, 106742, 106744, 106746, 106748, 106750, 106752, 106754, 106756, 106758, 106760, 106762, 106764, 106766, 106768, 106770, 106772, 106774, 106776, 106778, 106780, 106782, 106784, 106786, 106788, 106790, 106792, 106794, 106796, 106798, 106800, 106802, 106804, 106806, 106808, 106810, 106812, 106814, 106816, 106818, 106820, 106822, 106824, 106826, 106828, 106830, 106832, 106834, 106836]})
calls
file:
#CHROM POS
HE667775 106824
HE667775 24
So from this sample data the desired output would be that HE667775 106824
which gets append
to test
What I have tried:
test = []
line = calls.readline()
while len(line) > 1:
#for line in calls:
objects = line.split()
pos = int(objects[1])
chrom = objects[0]
#if scaf in codonDict and pos associated with that key
for scaf, position in codonDict.itervalues():
if pos == position and chrom in scaf:
test.append(line)
print test
Error:
ValueError: too many values to unpack
Edit:
This is the complete error traceback, however the lines differ, so line 28 in the above code would be I believe pos = int(objects[1])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 28, in main
ValueError: too many values to unpack
To check if the pos
from your file is in the condonDict
no loop is required, you can use python in
to check for membership by:
pos in condonDict[chrom]