Skip to content

Instantly share code, notes, and snippets.

@guestPK1986
Last active September 15, 2020 21:06
Show Gist options
  • Save guestPK1986/12438eb0c3f3ca4f3f06b84d362d2b3b to your computer and use it in GitHub Desktop.
Save guestPK1986/12438eb0c3f3ca4f3f06b84d362d2b3b to your computer and use it in GitHub Desktop.
Counter_defaultdict_OrderedDict.py
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