【发布时间】:2019-03-11 12:50:37
【问题描述】:
example = ['apple', 'pear', 'apple']
我怎样才能从上面得到下面的
result = [(apple ,2), (pear, 1)]
我只知道怎么用Counter,但是不知道怎么把结果转成上面的格式。
元组命令不起作用:
>>> tuple(Counter(example))
('apple', 'pear')
【问题讨论】:
标签: python python-3.x dictionary tuples counter