简体   繁体   English

使用defaultdict搜索键及其值

[英]Search keys and its values using defaultdict

I am new to python, so correct me if this is not the best/fastest way of doing this. 我是python的新手,所以如果这不是最好/最快的方法,请更正我。 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). codonDict我仅包含一个键及其一些值(会有更多值)。 Now I have a file which I have called calls here. 现在,我有一个文件,在这里已将其称为“ calls 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 . 我想做的是在文件中找到与#CHROM对应的密钥,然后搜索密钥值以查看它是否包含相应的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: calls档案:

#CHROM      POS
HE667775    106824  
HE667775    24

So from this sample data the desired output would be that HE667775 106824 which gets append to test 因此,从此样本数据中获得的期望输出将是HE667775 106824 ,该数据将appendtest

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]) 编辑:这是完整的错误回溯,但是各行不同,因此上述代码中的第28行是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

要检查文件中的pos是否在condonDict不需要循环,可以使用python in检查成员身份,方法是:

pos in condonDict[chrom]

So i don' t know exactly what your code is doing I' m pretty sure you get the ValueError because of this line of code: 所以我不知道您的代码在做什么,我很确定您会因为以下代码而收到ValueError:

for scaf, position in codonDict.itervalues()

itervalues gives you an iterator over the values of your dictionary. itervalues使您可以对字典的值进行迭代。 In your case this is a list. 在您的情况下,这是一个列表。 But you can' t unpack two variables scaf and position . 但是您不能解压缩scaf and position两个变量。

Try it this way and there should be no ValueError anymore: 尝试这种方式,应该不再有ValueError了:

for val in codonDict.itervalues()

To check if chrom is in codonDict just use in like dm03514 wrote before. 要检查是否CHROM是codonDict只是用in像dm03514以前写的。 I could imagine something like this with codonDict as an ordinary dictionary: 我可以用codonDict作为普通字典来想象这样的事情:

def find(chrom, pos):
    if chrom in codonDict:
        values = codonDict[chrom]
        if pos in values:
            print "%i in %s" % (pos, chrom)
        else:
            print "%i not in %s" % (pos, chrom)
    else:
        print "chrom %s not found" % chrom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM