我有一个超过 20 行的二维列表。我想排序并返回每个子列表中重复次数最多的。例如,这是我下面的列表
list1 = [[a,a,b, b, b, v,v,v,v,x,x,p], [b, c, d, c], [a, j,j,j,c,c,f,f,h,h,h,h]]
我希望我的结果是这样的
List2 = [[b,v,a,x],[c],[h,j,c,f]]
您可以使用
Counter.most_common
按频率顺序获取子列表中的项目。 v
在这里比 b
更常见:
from collections import Counter
a="a"; b="b"; c="c"; d="d"; f="f"; h="h"; j="j"; p="p"; v="v"; x="x"
ll = [[a,a,b, b, b, v,v,v,v,x,x,p], [b, c, d, c], [a, j,j,j,c,c,f,f,h,h,h,h]]
ls = [[e for e, n in Counter(items).most_common() if n >= 2] for items in ll]
print(ls) # [['v', 'b', 'a', 'x'], ['c'], ['h', 'j', 'c', 'f']]
这是使用计数器的解决方案:
import collections
list1 = [["a","a","b","b","b","v","v","v","v","x","x","p"],["b","c","d","c"],["a","j","j","j","c","c","f","f","h","h","h","h"]]
list2 = []
for l in list1:
c = collections.Counter(l)
list2.append( [k for k,v in c.items() if v > 1] )
print(list2)
输出:
[['a', 'b', 'v', 'x'], ['c'], ['j', 'c', 'f', 'h']]
您可以结合使用集合模块中的Counter和列表理解来实现这一点。 这是一个示例代码:
from collections import Counter
list1 = [['a','a','b','b','b','v','v','v','v','x','x','p'], ['b', 'c', 'd', 'c'], ['a', 'j','j','j','c','c','f','f','h','h','h','h']]
List2 = [[k for k, v in Counter(sublist).most_common()] for sublist in list1]
print(List2)
输出:
[['b', 'v', 'a', 'x'], ['c', 'd', 'b'], ['h', 'j', 'c', 'f', 'a']]
这段代码使用Counter来统计list1的每个子列表中每个元素的出现次数,然后most_common()方法返回按次数排序的元素。最后,列表推导式用于创建一个新列表 List2,其中仅包含按计数排序的元素。
希望对您有所帮助!如果您还有其他问题,或者这些解决方案是否解决了您的问题,请告诉我。
from collections import Counter
a = [[10, 15, 17], [20, 21, 27], [10, 15, 17], [21, 27, 28], [21, 27, 28], [5, 10, 15], [15, 17, 20]]
# count duplicates
counts = Counter(map(tuple,a))
# find the maximum count (the values of counts are the duplicate count)
maximum_count = max(counts.values())
# filter and convert back to list
result = [list(e) for e,count in counts.items() if count == maximum_count]
print(result)