gpt4 book ai didi

python - 使用 defaultdict 搜索键及其值

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:20 30 4
gpt4 key购买 nike

我是 python 的新手,如果这不是最好/最快的方法,请纠正我。我创建了一个字典,其中为每个键分配了多个值。在 codonDict 中,我只包含了 1 个键及其一些值(还会有更多)。现在我有一个文件,我在这里调用了 calls。我想做的是在文件中找到与 #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]})

调用文件:

#CHROM      POS
HE667775 106824
HE667775 24

因此,从这个示例数据中,所需的输出将是 HE667775 106824,它获取 appendtest

我尝试过的:

    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

错误:

ValueError: too many values to unpack

编辑:这是完整的错误回溯,但是行不同,所以我相信上面代码中的第 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 来检查成员资格通过:

condonDict[chrom] 中的位置

关于python - 使用 defaultdict 搜索键及其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21648676/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com