mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
32
ebook_src/USEFUL/basic_examples/example_counter.py
Normal file
32
ebook_src/USEFUL/basic_examples/example_counter.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
from collections import Counter
|
||||
|
||||
def Counter_example():
|
||||
''' it is a dictionary that maps the items to the number of occurrences '''
|
||||
seq1 = [1, 2, 3, 5, 1, 2, 5, 5, 2, 5, 1, 4]
|
||||
seq_counts = Counter(seq1)
|
||||
print(seq_counts)
|
||||
|
||||
''' we can increment manually or use the update() method '''
|
||||
seq2 = [1, 2, 3]
|
||||
seq_counts.update(seq2)
|
||||
print(seq_counts)
|
||||
|
||||
seq3 = [1, 4, 3]
|
||||
for key in seq3:
|
||||
seq_counts[key] += 1
|
||||
print(seq_counts)
|
||||
|
||||
''' also, we can use set operations such as a-b or a+b '''
|
||||
seq_counts_2 = Counter(seq3)
|
||||
print(seq_counts_2)
|
||||
print(seq_counts + seq_counts_2)
|
||||
print(seq_counts - seq_counts_2)
|
||||
|
||||
if __name__ == '__main__':
|
||||
Counter_example()
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue