Oct-05-2020, 09:22 AM
There is ambiguity in this task. What should happen if 'a' encountered (totally realistic scenario in english)? Also, should 'he' and 'He' be considered as different?
This solution is needlessly complicated. As mentioned in SO post there is Counter in collections built-in module which is specifically for counting. So code can be as simple as 'count two first letters of word for every word on every line':
This solution is needlessly complicated. As mentioned in SO post there is Counter in collections built-in module which is specifically for counting. So code can be as simple as 'count two first letters of word for every word on every line':
from collections import Counter # Content of the file: Here is an example from this text and its for show! with open('two_chars_count.csv', 'r') as f: count = Counter(word[:2].lower() for line in f for word in line.split()) print(*(f'{k}: {v}' for k, v in count.items()), sep='\n') he: 1 is: 1 an: 2 ex: 1 fr: 1 th: 1 te: 1 it: 1 fo: 1 sh: 1
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.