我正试图统计列表中元素的出现次数,如果这些元素也是列表。顺序也很重要。
[PSEUDOCODE]
lst = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['a', 'b', 'c'], ['c', 'b', 'a'] ]
print( count(lst) )
> { ['a', 'b', 'c'] : 2, ['d', 'e', 'f']: 1, ['c', 'b', 'a']: 1 }
一个重要因素是['a', 'b', 'c'] != ['c', 'b', 'a']
我已经尝试了:
from collections import counter
print( Counter([tuple(x) for x in lst]) )
print( [[x, list.count(x)] for x in set(lst)] )
这两种方式都导致了 ['a', 'b', 'c'] = ['c', 'b', 'a']
这个结果,这是我不想要的。
我还尝试过:
from collections import counter
print( Counter( lst ) )
这样做只会导致错误,因为在字典中无法使用lists
作为keys
。
有没有其他方法来实现这个功能?
tuple(['a', 'b', 'c']) == tuple(['c', 'b', 'a'])
gives meFalse
- Savir