# Counter模块:计算元素个数 # 注意:不能处理多维的数据结构 >>> s1 = "uwerhbbsmndfwehajksdhfjsss" # 处理字符串 >>> dict(Counter(s1)) # 返回每个元素出现的个数,以字典的形式返回 {'u': 1, 'w': 2, 'e': 2, 'r': 1, 'h': 3, 'b': 2, 's': 5, 'm': 1, 'n': 1, 'd': 2, 'f': 2, 'a': 1, 'j': 2, 'k': 1} >>> list(Counter(s1)) # 返回key组成的列表 ['u', 'w', 'e', 'r', 'h', 'b', 's', 'm', 'n', 'd', 'f', 'a', 'j', 'k'] >>> l1 = [1,2,3,'hello'] # 处理列表 >>> dict(Counter(l1)) {1: 1, 2: 1, 3: 1, 'hello': 1} >>> list(Counter(l1)) [1, 2, 3, 'hello'] >>> l2 = [12,34,"wer",[11,22,3],"python"] # 注意:无法处理多维列表的 >>> dict(Counter(l2)) TypeError: unhashable type: 'list' >>> t1 = ("hello", "world", 'hello', 'python') # 处理元组,也是无法处理多维的元组 >>> >>> dict(Counter(t1)) {'hello': 2, 'world': 1, 'python': 1} >>> list(Counter(t1)) ['hello', 'world', 'python'] >>> d1 = {'ab':3, 'c':5, (1,2):"d"} # 处理字典 >>> dict(Counter(d1)) {'ab': 3, 'c': 5, (1, 2): 'd'} >>> list(Counter(d1)) ['ab', 'c', (1, 2)] # most_common(num) 按照元素出现的次数进行从高到低的排序,返回前num个元素的字典 >>> Counter(s1).most_common() # 字符串 [('s', 5), ('h', 3), ('w', 2), ('e', 2), ('b', 2), ('d', 2), ('f', 2), ('j', 2), ('u', 1), ('r', 1), ('m', 1), ('n', 1), ('a', 1), ('k', 1)] >>> Counter(s1).most_common(2) [('s', 5), ('h', 3)] >>> Counter(l1).most_common() # 处理列表 [(1, 1), (2, 1), (3, 1), ('hello', 1)] >>> Counter(l1).most_common(2) [(1, 1), (2, 1)] >>> Counter(t1).most_common() # 处理元组 [('hello', 2), ('world', 1), ('python', 1)] >>> Counter(t1).most_common(1) [('hello', 2)] >>> d1 {'ab': 3, 'c': 5, (1, 2): 'd'} # key为一个序列时不可以使用这个方法的 >>> Counter(d1).most_common() TypeError: '<' not supported between instances of 'int' and 'str' >>> d2 = {'ab':3, 'c':5, 12:23} [(12, 23), ('c', 5), ('ab', 3)] >>> Counter(d2).most_common(1) [(12, 23)] # elements返回经过计算器Counter后的元素,返回的是一个迭代器 >>> Counter(s1).elements() # 处理字符串 <itertools.chain object at 0x0000026519B81048> >>> "".join(Counter(s1).elements()) 'uwweerhhhbbsssssmnddffajjk' >>> Counter(l1).elements() # 处理列表 <itertools.chain object at 0x0000026519B81048> >>> for i in Counter(l1).elements(): ... print(i) ... 1 2 3 hello >>> Counter(t1).elements() # 处理元组 <itertools.chain object at 0x0000026519B73D88> >>> iter1 = Counter(t1).elements() >>> iter1.__next__() 'hello' >>> iter1.__next__() 'hello' >>> iter1.__next__() 'world' >>> iter1.__next__() 'python' >>> iter1.__next__() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>> d2 # 处理字典 {'ab': 3, 'c': 5, 12: 23} >>> Counter(d2).elements() <itertools.chain object at 0x0000026519B81048> >>> list(Counter(d2).elements()) ['ab', 'ab', 'ab', 'c', 'c', 'c', 'c', 'c', 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12] #update更新,做加法,加上对应的个数 >>> s2 = Counter(s1) # 处理字符串 >>> s2.update('XXXXSSS') # 这个方法并没改变原来的字符串,而是重新创建了一个字符串 >>> id(s2) 2633245598056 >>> id(s1) 2633246378304 >>> dict(s2) {'u': 1, 'w': 2, 'e': 2, 'r': 1, 'h': 3, 'b': 2, 's': 5, 'm': 1, 'n': 1, 'd': 2, 'f': 2, 'a': 1, 'j': 2, 'k': 1, 'X': 4, 'S': 3} >>> l1 # 处理列表,不是改变原来的列表,而是重新创建了一个列表 [1, 2, 3, 'hello'] >>> l2 = Counter(l1) >>> id(l2) 2633246372872 >>> id(l1) 2633246352008 >>> l2.update([99,77,"world"]) >>> l2 Counter({1: 1, 2: 1, 3: 1, 'hello': 1, 99: 1, 77: 1, 'world': 1}) >>> t2 = Counter(t1) #处理元组,不是改变原来的元组,而是重新创建一个元组 >>> id(t1) 2633245381080 >>> id(t2) 2633246373064 >>> t1 ('hello', 'world', 'hello', 'python') >>> t2.update('11', '11', '22', '22') #只接受一个参数 TypeError: expected at most 1 arguments, got 4 >>> t2.update(('11', '11', '22', '22')) >>> dict(t2) {'hello': 2, 'world': 1, 'python': 1, '11': 2, '22': 2} >>> list(t2) ['hello', 'world', 'python', '11', '22'] >>> d2 {'ab': 3, 'c': 5, 12: 23} >>> d3 = Counter(d2) >>> id(d2) 263324639992 >>> id(d3) 2633246373256 >>> dict(d3) {'ab': 3, 'c': 5, 12: 56} >>> list(d3) ['ab', 'c', 12] # 可以使用字典里面的方法:key,values,items,get。。。 >>> Counter(s1).items() dict_items([('u', 1), ('w', 2), ('e', 2), ('r', 1), ('h', 3), ('b', 2), ('s', 5), ('m', 1), ('n', 1), ('d', 2), ('f', 2), ('a', 1), ('j', 2), ('k', 1)]) >>> list(Counter(l1).keys()) [1, 2, 3, 'hello'] >>> tuple(Counter(t1).values()) (2, 1, 1) >>> dict(Counter(d2)).get(12) 23