问题:
您好我是Python的新手,所以寻求一些帮助。我有多条输入线。我正在寻找一种方法来从多个列表中获取每个单词,然后将它们相加以获得多个List中该单词的每个实例的总计数。我真的很感激任何指导。
答案 0 :(得分:1)
编辑:
"""I need a count of the word given by key(s) in multiple lists at the given index. So what I need is
>>> count(["a", "b", "c"], ["c", "b", "a"])
{"a": [1,0,1], "b": [0,2,0], "c": [1,0,1]}":
"""
我希望这是正确的:
def count (*args):
l = len(args[0])
end = {}
for lst in args:
y = 0
for x in lst:
end.setdefault(x, l*[0])
end[x][y] += 1
y += 1
return end
如果您需要一种从RNA / DNA操纵基因组材料的方法,您可以在pypi.python.org上搜索文库。有很多好的。
答案 1 :(得分:0)
查看dict.setdefault()方法和enumerate()函数:
def count_items(data):
count = {}
for datum in data:
count[datum] = count.setdefault(datum, 0) + 1
return count
def collate(*data):
collated = {}
for datum in data:
for k, v in datum.items():
collated[k] = datum.setdefault(k, 0) + 1
return collated
def key_position(sequence, key):
sequence_map = [0 for _ in sequence]
for i, item in enumerate(sequence):
if key == item:
sequence_map[i] += 1
return sequence_map
data1 = ['a', 'b', 'c', 'd', 'a']
data2 = ['a', 'b', 'c', 'd', 'a']
counted1 = count_items(data1)
counted2 = count_items(data2)
collated = collate(counted1, counted2)
a_positions = key_position(data1, 'a')
答案 2 :(得分:0)
首先,zip
您的读物:
Read_1 = ['GGGA', 'ATTA']
Read_2 = ['GATT', 'ATTA']
reads = zip(Read_1, Read_2)
# ['GGGA', 'GATT'], ['ATTA', 'ATTA']
然后,计算一下:
from collections import Counter
counters = [Counter(read) for read in reads]
然后询问给定序列的频率:
print(list(cnt['ATTA'] for cnt in counters)
# [0, 2]
print(list(cnt['GGGA'] for cnt in counters)
# [1, 0]