Moh*_*ani 6 python numpy list python-3.x numpy-ndarray
我有一个2D列表:
arr = [['Mohit', 'shini','Manoj','Mot'],
['Mohit', 'shini','Manoj'],
['Mohit', 'Vis', 'Nusrath']]
Run Code Online (Sandbox Code Playgroud)
我想找到2D列表中最常见的元素.在上面的示例中,最常见的字符串是'Mohit'.
我知道我可以使用两个for循环和一个字典来执行此操作,但使用numpy或任何其他库是否有更有效的方法?
嵌套列表可以有不同的长度
有人也可以添加他们的方法的时间吗?找到禁食的方法.它也可能不是非常有效的警告.
编辑
这些是我系统上不同方法的时间:
#timegb
%%timeit
collections.Counter(chain.from_iterable(arr)).most_common(1)[0][0]
5.91 µs ± 115 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#Kevin Fang and Curious Mind
%%timeit
flat_list = [item for sublist in arr for item in sublist]
collections.Counter(flat_list).most_common(1)[0]
6.42 µs ± 501 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%%timeit
c = collections.Counter(item for sublist in arr for item in sublist).most_common(1)c[0][0]
6.79 µs ± 449 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#Mayank Porwal
def most_common(lst):
return max(set(lst), key=lst.count)
%%timeit
ls = list(chain.from_iterable(arr))
most_common(ls)
2.33 µs ± 42.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
#U9-Forward
%%timeit
l=[x for i in arr for x in i]
max(l,key=l.count)
2.6 µs ± 68.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Run Code Online (Sandbox Code Playgroud)
Mayank Porwal的方法在我的系统上运行速度最快.
itertools.chain.from_iterableCounter.演示:
\n\n>>> from itertools import chain\n>>> from collections import Counter\n>>> \n>>> lst = [[\'Mohit\', \'shini\',\'Manoj\',\'Mot\'],\n...: [\'Mohit\', \'shini\',\'Manoj\'],\n...: [\'Mohit\', \'Vis\', \'Nusrath\']]\n...: \n>>> Counter(chain.from_iterable(lst)).most_common(1)[0][0]\n\'Mohit\'\nRun Code Online (Sandbox Code Playgroud)\n\n细节:
\n\n>>> list(chain.from_iterable(lst))\n[\'Mohit\',\n \'shini\',\n \'Manoj\',\n \'Mot\',\n \'Mohit\',\n \'shini\',\n \'Manoj\',\n \'Mohit\',\n \'Vis\',\n \'Nusrath\']\n>>> Counter(chain.from_iterable(lst))\nCounter({\'Manoj\': 2, \'Mohit\': 3, \'Mot\': 1, \'Nusrath\': 1, \'Vis\': 1, \'shini\': 2})\n>>> Counter(chain.from_iterable(lst)).most_common(1)\n[(\'Mohit\', 3)]\nRun Code Online (Sandbox Code Playgroud)\n\n一些时间安排:
\n\n>>> lst = lst*100\n>>> %timeit Counter(chain.from_iterable(lst)).most_common(1)[0][0] # timgeb\n53.7 \xc2\xb5s \xc2\xb1 411 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 10000 loops each)\n>>> %timeit max([x for i in lst for x in i], key=l.count) # U9-Forward\n207 \xc2\xb5s \xc2\xb1 389 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000 loops each)\n>>> %timeit Counter([x for sublist in lst for x in sublist]).most_common(1)[0][0] # Curious_Mind/Kevin Fang #1\n75.2 \xc2\xb5s \xc2\xb1 2.6 \xc2\xb5s per loop (mean \xc2\xb1 std. dev. of 7 runs, 10000 loops each)\n>>> %timeit Counter(item for sublist in lst for item in sublist).most_common(1)[0][0] # Kevin Fang #2\n95.2 \xc2\xb5s \xc2\xb1 2.07 \xc2\xb5s per loop (mean \xc2\xb1 std. dev. of 7 runs, 10000 loops each)\n>>> %timeit flat = list(chain.from_iterable(lst)); max(set(flat), key=flat.count) # Mayank Porwal\n98.4 \xc2\xb5s \xc2\xb1 178 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 10000 loops each)\nRun Code Online (Sandbox Code Playgroud)\n\n(请注意,Kevin Fang 的第二个解决方案比第一个解决方案慢一点,但内存效率更高。)
\n| 归档时间: |
|
| 查看次数: |
449 次 |
| 最近记录: |