python 学习日记2 (counter,set)

counter:

计数器(Counter)是一个容器,用来跟踪值出现了多少次。

排序加去重

代码示例:

from collections import Counter

mylist=['tom','adel','alex','adel','tom','tom','adel','tom','alex']

myset = Counter(mylist)

print myset

运行结果:

[('tom', 4), ('adel', 3), ('alex', 2)]

set:

set()函数返回列表中不同的字符串组成的集合

去重!

代码示例:

mylist=['tom','adel','alex','adel','tom','tom','adel','tom','alex']

myset =set(mylist)

for item in myset:

print "name:%s num:%d" %(item,mylist.count(item))

运行结果:

name:adel num:3

name:alex num:2

name:tom num:4

你可能感兴趣的:(python 学习日记2 (counter,set))