嗨,我是编程新手,想学习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.
注意:我们不知道列表中最常见的元素是什么,因此我们必须找到最常见的元素,如果有多个元素,则应返回all。如果列表中有数字或其他字符串作为元素,那么代码也必须工作
我不知道如何继续。我可以用一些帮助。
以下是整个计划:
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链中存在的那个长度的角蛋。
示例输入:
ACGTTGCATGTCGCATGATGCATGAGAGCT
4
示例输出:
CATG GCAT
答案 0 :(得分: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())]
答案 1 :(得分: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']
答案 2 :(得分:0)
只是为了给出不使用collections
&amp;的解决方案。使用列表推导。
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
:(学习时间!
答案 3 :(得分: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']