【发布时间】:2020-01-10 16:14:26
【问题描述】:
我有一个表格列表:
my_list = [[8, [16, 32], [32, 16, 8], 0],
[16, [16, 32], [32, 16, 8], 0],
[16, [32, 64], [32, 16, 8], 0],
[8, [16, 32], [32, 16, 8], 0]]
我想提取最频繁的项目,即:
most_freq_item = [8, [16, 32], [32, 16, 8], 0]
我尝试将列表转换为 numpy,然后使用 np.unique:
import numpy as np
list_as_np = np.asarray(my_list)
unq, cnt = np.unique(list_as_np, axis=0, return_counts=True)
但这会引发TypeError: The axis argument to unique is not supported for dtype object,因为np.asarray 确实创建了一个对象,而不是一个正确的np.ndarray。
有什么建议吗?非常感谢!
【问题讨论】:
标签: python-3.x list numpy