[英]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.我不知道这是否是一个值得赞赏的建议,但我不会将
key
和count
定义为...键和值,我只会使用搜索到的 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.