Ad

Find Occurences From A List Of Strings

- 1 answer

I would like to create a function without external libraries that finds letters from a list of words (strings) and counts their occurrence only if the word has more than 3 characters Then prints them in order.

List with words

word_list = ['THE', 'ZEN', 'OF', 'PYTHON', 'BY', 'TIM', 'PETERS', 'BEAUTIFUL', 'IS', 'BETTER', 'THAN', 'UGLY', 'EXPLICIT', 'IS', 'BETTER', 'THAN', 'IMPLICIT', 'SIMPLE', 'IS', 'BETTER', 'THAN', 'COMPLEX', 'COMPLEX', 'IS', 'BETTER', 'THAN', 'COMPLICATED', 'FLAT', 'IS', 'BETTER', 'THAN', 'NESTED', 'SPARSE', 'IS', 'BETTER', 'THAN', 'DENSE', 'READABILITY', 'COUNTS', 'SPECIAL', 'CASES', 'ARENT', 'SPECIAL', 'ENOUGH', 'TO', 'BREAK', 'THE', 'RULES', 'ALTHOUGH', 'PRACTICALITY', 'BEATS', 'PURITY', 'ERRORS', 'SHOULD', 'NEVER', 'PASS', 'SILENTLY', 'UNLESS', 'EXPLICITLY', 'SILENCED', 'IN', 'THE', 'FACE', 'OF', 'AMBIGUITY', 'REFUSE', 'THE', 'TEMPTATION', 'TO', 'GUESS', 'THERE', 'SHOULD', 'BE', 'ONE', 'AND', 'PREFERABLY', 'ONLY', 'ONE', 'OBVIOUS', 'WAY', 'TO', 'DO', 'IT', 'ALTHOUGH', 'THAT', 'WAY', 'MAY', 'NOT', 'BE', 'OBVIOUS', 'AT', 'FIRST', 'UNLESS', 'YOURE', 'DUTCH', 'NOW', 'IS', 'BETTER', 'THAN', 'NEVER', 'ALTHOUGH', 'NEVER', 'IS', 'OFTEN', 'BETTER', 'THAN', 'RIGHT', 'NOW', 'IF', 'THE', 'IMPLEMENTATION', 'IS', 'HARD', 'TO', 'EXPLAIN', 'ITS', 'A', 'BAD', 'IDEA', 'IF', 'THE', 'IMPLEMENTATION', 'IS', 'EASY', 'TO', 'EXPLAIN', 'IT', 'MAY', 'BE', 'A', 'GOOD', 'IDEA', 'NAMESPACES', 'ARE', 'ONE', 'HONKING', 'GREAT', 'IDEA', '', 'LETS', 'DO', 'MORE', 'OF', 'THOSE']

Desired output:

Words with more than 3 letters

1 BETTER shows up 8 times
2 THAN shows up 7 times
.
.
.
Ad

Answer

A simple way to do this with built-in python functions:

keys = set(word_list)

values = [word_list.count(key) for key in keys]

for k, v in zip(keys, values):
    print('item', k, 'has count', v)

Output:

item EASY has count 1
item IS has count 10
item DENSE has count 1
item EXPLICITLY has count 1
item FIRST has count 1
item THE has count 6
item DUTCH has count 1
item ONE has count 3
item BEAUTIFUL has count 1
item TO has count 5
item LETS has count 1
item BREAK has count 1
item READABILITY has count 1
item THAT has count 1
item GREAT has count 1
item IF has count 2
item NOW has count 2
item GOOD has count 1
item ALTHOUGH has count 3
item WAY has count 2
item MORE has count 1
item NESTED has count 1
item SPARSE has count 1
item AND has count 1
item ERRORS has count 1
item ZEN has count 1
item BY has count 1
item SILENCED has count 1
item ITS has count 1
item BETTER has count 8
item OBVIOUS has count 2
item ONLY has count 1
item THOSE has count 1
item ARENT has count 1
item REFUSE has count 1
item EXPLICIT has count 1
item BAD has count 1
item COMPLEX has count 2
item SILENTLY has count 1
item BE has count 3
item COMPLICATED has count 1
item PETERS has count 1
item SHOULD has count 2
item PREFERABLY has count 1
item UNLESS has count 2
item RULES has count 1
item NAMESPACES has count 1
item THERE has count 1
item OF has count 3
item EXPLAIN has count 2
item IMPLEMENTATION has count 2
item HARD has count 1
item IN has count 1
item COUNTS has count 1
item NOT has count 1
item A has count 2
item YOURE has count 1
item PURITY has count 1
item NEVER has count 3
item IMPLICIT has count 1
item DO has count 2
item ARE has count 1
item BEATS has count 1
item HONKING has count 1
item AMBIGUITY has count 1
item PRACTICALITY has count 1
item RIGHT has count 1
item ENOUGH has count 1
item MAY has count 2
item UGLY has count 1
item SIMPLE has count 1
item TIM has count 1
item IT has count 2
item CASES has count 1
item FLAT has count 1
item FACE has count 1
item THAN has count 8
item AT has count 1
item TEMPTATION has count 1
item PYTHON has count 1
item SPECIAL has count 2
item PASS has count 1
item IDEA has count 3
item OFTEN has count 1
item GUESS has count 1
Ad
source: stackoverflow.com
Ad