我正在尝试获取maxlist中每个列表的最频繁值。然而,每次我尝试这些函数或教程时,我要么显示完整的列表,要么显示‘对象列表不可调用’。
最后,我的目标是从maxlist中的每个列表中获取最频繁的值(这里是'2'),并将新的值放入一个名为‘result’的新列表中

发布于 2020-07-05 00:58:12
我想你是在找这个。替换为您的输入
from collections import Counter
l = [[0,0,1],
[1,1,2]]
list_ = []
for ele in l:
list_.append(Counter(ele).most_common(1)[0][0])
print(list_)输出:
[0, 1]发布于 2020-07-05 01:01:17
另一种解决方案是使用numpy
import numpy as np
l = [[0,0,1],
[1,1,2]]
nums, counts = np.unique(l, return_counts = True)
print(nums[np.argwhere(counts == counts.max())])https://stackoverflow.com/questions/62731890
复制相似问题