-3

嗨,我是编程新手,想学习 python。我正在编写一个代码,该代码应该返回列表中最冗余的项目。如果有超过 1 个,那么它应该全部返回。前任。

List = ['a','b','c','b','d','a'] #then it should return both a and b.
List = ['a','a','b','b','c','c','d'] #then it should return a b and c.
List = ['a','a','a','b','b','b','c','c','d','d','d'] #then it should return a b and d.

注意:我们不知道列表中哪个元素最常见,因此我们必须找到最常见的元素,如果有多个元素,则应返回所有元素。如果列表有数字或其他字符串作为元素,那么代码也必须工作

我不知道如何进行。我可以使用一点帮助。

这是整个程序:

from collections import Counter

def redundant(List):
    c = Counter(List)
    maximum = c.most_common()[0][1]
    return [k for k, v in c.items()if v == maximum]

def find_kmers(DNA_STRING, k):
    length = len(DNA_STRING)
    a = 0
    List_1 = []
    string_1 = ""
    while a <= length - k:
        string_1 = DNA_STRING[a:a+k]
        List_1.append(string_1)
        a = a + 1
    redundant(List_1)

该程序应获取 DNA 字符串和 kmer 的长度,并找出该 DNA 字符串中存在的该长度的 kemer。

样本输入:

ACGTTGCATGTCGCATGATGCATGAGAGCT
4

样本输出:

CATG GCAT  
4

4 回答 4

2

您可以使用collections.Counter

from collections import Counter
def solve(lis):
    c = Counter(lis)
    mx = c.most_common()[0][1]
    #or mx = max(c.values())
    return [k for k, v in c.items() if v == mx]

print (solve(['a','b','c','b','d','a']))
print (solve(['a','a','b','b','c','c','d']))
print (solve(['a','a','a','b','b','b','c','c','d','d','d'] ))

输出:

['a', 'b']
['a', 'c', 'b']
['a', 'b', 'd']

使用上述代码的稍微不同的版本itertools.takewhile

from collections import Counter
from itertools import takewhile
def solve(lis):
    c = Counter(lis)
    mx = max(c.values())
    return [k for k, v in takewhile(lambda x: x[1]==mx, c.most_common())]
于 2013-11-08T07:00:53.400 回答
1
inputData = [['a','b','c','b','d','a'], ['a','a','b','b','c','c','d'], ['a','a','a','b','b','b','c','c','d','d','d'] ]
from collections import Counter
for myList in inputData:
    temp, result = -1, []
    for char, count in Counter(myList).most_common():
        if temp == -1: temp = count
        if temp == count: result.append(char)
        else: break
    print result

输出

['a', 'b']
['a', 'c', 'b']
['a', 'b', 'd']
于 2013-11-08T07:01:22.493 回答
0

只是为了提供一个不使用collections& 使用列表推导的解决方案。

given_list = ['a','b','c','b','d','a']
redundant = [(each, given_list.count(each)) for each in set(given_list) if given_list.count(each) > 1]
count_max = max(redundant, key=lambda x: x[1])[1]
final_list = [char for char, count in redundant if count == count_max]

PS-我自己还没用过Counters:(是时候学习了!

于 2013-11-08T06:58:50.433 回答
0
>>> def maxs(L):
...   counts = collections.Counter(L)
...   maxCount = max(counts.values())
...   return [k for k,v in counts.items() if v==maxCount]
... 
>>> maxs(L)
['a', 'b']
>>> L = ['a','a','b','b','c','c','d']
>>> maxs(L)
['a', 'b', 'c']
>>> L = ['a','a','a','b','b','b','c','c','d','d','d']
>>> maxs(L)
['d', 'a', 'b']
于 2013-11-08T07:00:31.397 回答