Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sentence = "hello, this is a sentence"
- # method 1 using get()
- mydict= {}
- for ch in sentence:
- ctr = mydict.get(ch, 0)
- mydict[ch] = ctr + 1
- print(mydict)
- # ------------------------
- # method 2 not using get()
- mydic2 = {}
- for ch in sentence:
- if ch in mydic2:
- mydic2[ch] = 1 + mydic2[ch]
- else:
- mydic2[ch] = 1
- print(mydic2)
- # -------------------------)
- # method 3 using set() and count()
- mydic3 = {}
- aset = set(sentence)
- for ch in aset:
- ctr = sentence.count(ch)
- mydic3[ch] = ctr
- print(mydic3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement