简体   繁体   English

当我 go 通过 Python 中的循环时,只剩下最后一个键/值对添加到字典

[英]Only left with the last key/value pairs added to a dictionary when I go through a loop in Python

Using Python 3 I am trying to get a dictionary of names and counts of the occurrence of certain strings in one long string.使用 Python 3 我正在尝试获取名称字典和一个长字符串中某些字符串的出现次数。

I am sitting here pulling my hair out as this should not be complicated but I have read a lot of answers to this already and I still am not getting it.我坐在这里拔头发,因为这不应该很复杂,但我已经阅读了很多答案,但我仍然没有得到它。 I'm 5 hours in and definitely not seeing the wood for the trees now.我已经 5 个小时了,现在绝对看不到树木的树木。

Hopefully, someone can show me where I am going wrong.希望有人能告诉我哪里出错了。

The string is called seq .该字符串称为seq

seq = 'AAGGTAAGTTTAGAATATAAAAGGTGAGTTAAATAGAATAGGTTAAAATTAAAGGAGATCAGATCAGATCAGATCTATCTATCTATCTATCTATCAGAAAAGAGTAAATAGTTAAAGAGTAAGATATTGAATTAATGGAAAATATTGTTGGGGAAAGGAGGGATAGAAGG'

I have a CSV of words I am looking for and that is in a list called nu我有一个 CSV 我正在寻找的单词,它在一个名为nu的列表中

nu = ['AGATC', 'AATG', 'TATC']

The code should use each of the words in nu and get a count of the number of occurrences in seq .代码应该使用 nu 中的每个单词并计算seq中出现的次数。

Here is my loop这是我的循环

for i in nu:
    searchstr = {}
    # Line returns a dict of the last value added
    searchstr = dict(key = (i), count = (seq.count(i)))
    print(searchstr)

print(searchstr.keys())
print(searchstr.values())

and the output so I know I'm matching the count correctly with the keys:和 output 所以我知道我将计数与键正确匹配:

{'key': 'AGATC', 'count': 4}
{'key': 'AATG', 'count': 1}
{'key': 'TATC', 'count': 5}
dict_keys(['key', 'count'])
dict_values(['TATC', 5])

I just can't for the life of me get the three dicts into one.我一生都无法将三个字典合二为一。 I am just left with a dict of ['TATC', 5] as it has overwritten the previous in the list.我只剩下一个 ['TATC', 5] 的字典,因为它已经覆盖了列表中的前一个。

I'm still new to this but trying to learn along the way.我对此仍然很陌生,但一直在努力学习。

All you need is to assign elements to the dictionary, not create a new dictionary each time:您所需要的只是将元素分配给字典,而不是每次都创建一个新字典:

searchstr = {}
for i in nu:
    searchstr[i] = seq.count(i)
print(searchstr)

I think this is what you want:我认为这就是你想要的:

searchstr = {}
for i in nu:
    # Line returns a dict of the last value added
    searchstr[i] = seq.count(i)
print(searchstr)
searchstr = {}
for i in nu:
    # Line returns a dict of the last value added
    # Earlier the dictionary declaration was here which was overriding the previous value
    searchstr = dict(key = (i), count = (seq.count(i)))
    print(searchstr)

print(searchstr.keys())
print(searchstr.values())

Move the dictionary declaration outside将字典声明移到外面

You declare each at iteration of the loop;您在循环的迭代中声明每个; that's why you can always see just the last inserted key.这就是为什么您总是可以看到最后插入的密钥。

I don't know if it would be an appreciated suggestion, but instead of defining key and count as... key and value I would just use the searched DNA sequence as a key.我不知道这是否是一个值得赞赏的建议,但我不会将keycount定义为...键和值,我只会使用搜索到的 DNA 序列作为键。 Something like that:像这样的东西:

searchstr = dict()
for i in nu:
    searchstr[i] = seq.count(i)
    
print(searchstr.keys())
print(searchstr.values())
print(searchstr)

print(searchstr['AATG']) #reading a specific result

Output: Output:

dict_keys(['AGATC', 'AATG', 'TATC'])
dict_values ([4, 1, 5])
{'AGATC': 4, 'AATG': 1, 'TATC': 5}
1

As you can see, the dictionary just needs to be declared outside the loop, and in the loop you'll add an element for every searched string.如您所见,字典只需要在循环外声明,并且在循环中您将为每个搜索到的字符串添加一个元素。

Please note how it will be easier accessing the specific sequence count.请注意如何更容易访问特定的序列计数。

暂无
暂无

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

相关问题 Dictionary只返回for循环中的最后一个键值对 - Dictionary returns only last key value pairs inside for loop Python - For 循环不会用它的匹配值替换每个字典键......只合作字典中的最后一个键和值 - Python - For loop will not replace every dictionary key with it's matching value... Only cooperating for last key & value in dictionary Function 只打印字典的最后 2 个键:值对 - Function only printing last 2 key:value pairs of dictionary 如何遍历字典,并使用键应用 function:值对作为 arguments - How do I loop through a dictionary, and apply a function using key: value pairs as arguments 遍历python字典中的键值对以构建对象列表 - Iterating through key value pairs in a python dictionary to build an list of objects Python for循环仅返回字典的最后一个值 - Python for loop only returning last value of a dictionary python在for循环后打印字典键值对的单个实例 - python print single instance of dictionary key value pairs after for loop 在For循环中将键/值对追加到Python字典,输出顺序 - Appending Key/Value Pairs to Python Dictionary in For Loop, Output Ordering Python3:我如何 append 多个键、值对到 for 循环中的字典? - Python3: How do I append multiple key,value pairs to a dictionary in for loop? 当只有两个键可用时,Python如何从字典中获取键值对的范围 - Python how to fetch range of key value pairs from dictionary when only two keys are available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM