# 找出次数最多的元素-Counter

代码：

```python
from collections import Counter

words=['look','info','my','AAA','look','my','AAA','the','AAA','the','AAA','the','eyes','not','BBB','the',
      'AAA','dont','BBB','around','the','AAA','look','into','BBB','AAA','under']

words_counts=Counter(words)
#最多频率的3个元组
top_3=words_counts.most_common(3)
print(words_counts)
print()
print('top_3:\n',top_3)
```

结果：

```python
Counter({'AAA': 7, 'the': 5, 'look': 3, 'BBB': 3, 'my': 2, 'info': 1, 'eyes': 1, 'not': 1, 'dont': 1, 'around': 1, 'into': 1, 'under': 1})

top_3:
 [('AAA', 7), ('the', 5), ('look', 3)]
```

视频：

```
none
```

## Counter()其它操作

代码：

```python
from collections import Counter

print('c.subtract(d)')
c = Counter(a=4, b=2, c=0, d=-2)
print(c)
d = Counter(a=1, b=2, c=3, d=4,e=11)
print(d)
c.subtract(d)#相减
print(c)


print('c+d','-'*50)
c = Counter(a=4, b=2, c=0, d=-2,e=11)
print(c)
d = Counter(a=1, b=2, c=3, d=4)
print(d)
print(c + d) #相加，舍去非正数

print('c-d','-'*50)
c = Counter(a=4, b=2, c=0, d=-2,e=11)
print(c)
d = Counter(a=1, b=2, c=3, d=4)
print(d)
print(c - d) #相减，舍去非正数

print('c&d','-'*50)
c = Counter(a=4, b=2, c=0, d=-2,e=11)
print(c)
d = Counter(a=1, b=2, c=3, d=4)
print(c & d) #交集，取小的那个，舍去非正数

print('c|d','-'*50)
c = Counter(a=4, b=2, c=0, d=-2,e=11)
print(c)
d = Counter(a=1, b=2, c=3, d=4)
print(d)
print(c | d) #并集，取大的那个，舍去非正数


print('c.update(d)','-'*50)
c = Counter(a=4, b=2, c=0, d=-2,e=11)
print(c)
d = Counter(a=1, b=2, c=3, d=4)
print(d)
c.update(d)#更新Counter，对于已有的元素计数加对应新的数量，对没有的元素进行添加
print(c)


print('-'*50)
print(c.items())
print(list(c))
print(set(c))
print(dict(c))
print(c.values())
```

结果：

```python
c.subtract(d)
Counter({'a': 4, 'b': 2, 'c': 0, 'd': -2})
Counter({'e': 11, 'd': 4, 'c': 3, 'b': 2, 'a': 1})
Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6, 'e': -11})
c+d --------------------------------------------------
Counter({'e': 11, 'a': 4, 'b': 2, 'c': 0, 'd': -2})
Counter({'d': 4, 'c': 3, 'b': 2, 'a': 1})
Counter({'e': 11, 'a': 5, 'b': 4, 'c': 3, 'd': 2})
c-d --------------------------------------------------
Counter({'e': 11, 'a': 4, 'b': 2, 'c': 0, 'd': -2})
Counter({'d': 4, 'c': 3, 'b': 2, 'a': 1})
Counter({'e': 11, 'a': 3})
c&d --------------------------------------------------
Counter({'e': 11, 'a': 4, 'b': 2, 'c': 0, 'd': -2})
Counter({'b': 2, 'a': 1})
c|d --------------------------------------------------
Counter({'e': 11, 'a': 4, 'b': 2, 'c': 0, 'd': -2})
Counter({'d': 4, 'c': 3, 'b': 2, 'a': 1})
Counter({'e': 11, 'a': 4, 'd': 4, 'c': 3, 'b': 2})
c.update(d) --------------------------------------------------
Counter({'e': 11, 'a': 4, 'b': 2, 'c': 0, 'd': -2})
Counter({'d': 4, 'c': 3, 'b': 2, 'a': 1})
Counter({'e': 11, 'a': 5, 'b': 4, 'c': 3, 'd': 2})
--------------------------------------------------
dict_items([('a', 5), ('b', 4), ('c', 3), ('d', 2), ('e', 11)])
['a', 'b', 'c', 'd', 'e']
{'c', 'e', 'b', 'a', 'd'}
{'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 11}
dict_values([5, 4, 3, 2, 11])
```

视频：

```
none
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/python/2.1.3-zhao-chu-ci-shu-zui-duo-dao-yuan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
