Created
March 28, 2020 08:58
-
-
Save mayankdiatm/46596533888a52b6c2db4e8e29066fb1 to your computer and use it in GitHub Desktop.
Python Useful Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
collections.Counter() | |
A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values. | |
>>> from collections import Counter | |
>>> | |
>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3] | |
>>> print Counter(myList) | |
Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1}) | |
>>> | |
enumerate() | |
adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment