如何在二维列表中获取最多的重复项。

huangapple go评论66阅读模式
英文:

How to get the most duplicates in 2d list

问题

我有一个包含超过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]]

英文:

I have a 2d list with more than more than 20 rows. I would like to sorted and return the most duplicates in each sublist.For example, here is my list below

 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]]

I would like my result to be like this.

 List2 = [[b,v,a,x],[c],[h,j,c,f]]

答案1

得分: 3

你可以使用 `Counter.most_common` 来按频率顺序获取子列表中的项目 在这里,`v``b` 更常见

```py
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']]

<details>
<summary>英文:</summary>

You can use `Counter.most_common` to get the items in the sublist in order of frequency. `v` is more common than `b` here:

```py
from collections import Counter

a=&quot;a&quot;; b=&quot;b&quot;; c=&quot;c&quot;; d=&quot;d&quot;; f=&quot;f&quot;; h=&quot;h&quot;; j=&quot;j&quot;; p=&quot;p&quot;; v=&quot;v&quot;; x=&quot;x&quot;
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 &gt;= 2] for items in ll]
print(ls)  # [[&#39;v&#39;, &#39;b&#39;, &#39;a&#39;, &#39;x&#39;], [&#39;c&#39;], [&#39;h&#39;, &#39;j&#39;, &#39;c&#39;, &#39;f&#39;]]

答案2

得分: 2

这是使用Counter的解决方案的代码:

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']]
英文:

Here's the solution using a Counter:

import collections

list1 = [[&quot;a&quot;,&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;b&quot;,&quot;v&quot;,&quot;v&quot;,&quot;v&quot;,&quot;v&quot;,&quot;x&quot;,&quot;x&quot;,&quot;p&quot;],[&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;c&quot;],[&quot;a&quot;,&quot;j&quot;,&quot;j&quot;,&quot;j&quot;,&quot;c&quot;,&quot;c&quot;,&quot;f&quot;,&quot;f&quot;,&quot;h&quot;,&quot;h&quot;,&quot;h&quot;,&quot;h&quot;]]
list2 = []
for l in list1:
    c = collections.Counter(l)
    list2.append( [k for k,v in c.items() if v &gt; 1] )
print(list2)

Output:

[[&#39;a&#39;, &#39;b&#39;, &#39;v&#39;, &#39;x&#39;], [&#39;c&#39;], [&#39;j&#39;, &#39;c&#39;, &#39;f&#39;, &#39;h&#39;]]

huangapple
  • 本文由 发表于 2023年5月10日 12:56:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定