比较列表中的所有元素
Compare all elements of list between themselves
如何计算列表中元组的出现次数,而不考虑元组项的顺序?例如;
the_list = [
(one, two, three),
(two, three, four),
(one, three, two),
(one, two, three),
(four, two, one),
]
在上面的列表中,我希望 (one, two, three)
和 (one, three, two)
相等,并算作 相同 的东西。
您可以在开始计数之前使用计数器对它们进行排序
thelist = [(1,2,3), (2,3,4), (1,3,2), (1,2,3), (4,2,1)]
from collections import Counter
Counter(tuple(sorted(x)) for x in thelist)
Counter({(1, 2, 3): 3, (2, 3, 4): 1, (1, 2, 4): 1})
如何计算列表中元组的出现次数,而不考虑元组项的顺序?例如;
the_list = [
(one, two, three),
(two, three, four),
(one, three, two),
(one, two, three),
(four, two, one),
]
在上面的列表中,我希望 (one, two, three)
和 (one, three, two)
相等,并算作 相同 的东西。
您可以在开始计数之前使用计数器对它们进行排序
thelist = [(1,2,3), (2,3,4), (1,3,2), (1,2,3), (4,2,1)]
from collections import Counter
Counter(tuple(sorted(x)) for x in thelist)
Counter({(1, 2, 3): 3, (2, 3, 4): 1, (1, 2, 4): 1})