Last active
September 15, 2020 21:06
-
-
Save guestPK1986/12438eb0c3f3ca4f3f06b84d362d2b3b to your computer and use it in GitHub Desktop.
Counter_defaultdict_OrderedDict.py
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
from collections import Counter, defaultdict, OrderedDict | |
li = [1,2,3,4,5,6,7,7,7] | |
sentance = "blah blah blah thinking about python" | |
print(Counter(li)) # will return Counter({7: 3, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1}) | |
print(Counter(sentance)) # will return Counter({'h': 5, ' ': 5, 'b': 4, 'a': 4, 'l': 3, 't': 3, 'n': 3, 'i': 2, 'o': 2, 'k': 1, 'g': 1, 'u': 1, 'p': 1, 'y': 1}) | |
dictionary = defaultdict(lambda: "does not exist", {"a":1, "b": 2}) | |
print(dictionary['c']) # will return 'does not exist' | |
#Ordered dictionary | |
d = OrderedDict() | |
d['a'] = 1 | |
d['b'] = 2 | |
d2 = OrderedDict() | |
d2['b'] = 2 | |
d2['a'] = 1 | |
print(d2 == d) # will return False | |
#regular dictionary | |
d3 = {'c':100} | |
d3['a'] = 1 | |
d3['b'] = 2 | |
d4 = {'c':100} | |
d4['b'] = 2 | |
d4['a'] = 1 | |
print(d3 == d4) # will return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment