1

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
.
.
.
devblack.exe
  • 428
  • 4
  • 17
  • 1
    Does this answer your question? [How can I count the occurrences of a list item?](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item) – DjaouadNM Jan 19 '22 at 18:31

3 Answers3

2

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
Synthase
  • 5,849
  • 2
  • 12
  • 34
  • Can you explain where you cover OP's request *" counts their occurrence only if the word has more than 3 characters"* in your offered answer? – Mario Jan 20 '22 at 00:00
  • @Mario I didn't see that part tbh. However this is the accepted answer. So I guess the OP is happy with it. – Synthase Jan 20 '22 at 07:48
  • Sure, but then this would be another duplication like the others already existed. check these old posts: [1](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item), [2](https://stackoverflow.com/questions/2392929/how-to-get-unique-values-with-respective-occurrence-count-from-a-list-in-python), [3](https://stackoverflow.com/questions/44418916/count-of-each-unique-element-in-a-list), [4](https://stackoverflow.com/questions/42377542/count-distinct-values-in-a-list-of-lists-python), ... – Mario Jan 20 '22 at 08:54
  • [5](https://stackoverflow.com/questions/23240969/python-count-repeated-elements-in-the-list), [6](https://stackoverflow.com/questions/65709180/identify-and-count-repeated-words-in-the-list), .... so the end condition can make this post different from the others. – Mario Jan 20 '22 at 08:56
1

It seems to find a way to count the number of unique values in the list. Despite you can use a combination of set() (which holds the unique values) and len() functions as it is explained in this answer, but you can use:

#print(len(set(word_list)))
#86

counts_unique_values = dict(zip(list(word_list),[list(word_list).count(i) for i in list(word_list)])) 
print(counts_unique_values)

output:

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

Option2: you can convert the list into pandas dataframe by calling directly pd.DataFrame() and use value_counts() to count distinct values in single column:

import pandas as pd
#df = pd.DataFrame(word_list)
df = pd.DataFrame({'Text': word_list})
df.value_counts()

output:

Text    
IS          10
BETTER       8
THAN         8
THE          6
TO           5
            ..
ITS          1
YOURE        1
IN           1
IMPLICIT     1
             1
Length: 86, dtype: int64

to cover:

...and counts their occurrence only if the word has more than 3 characters Then prints them in order

you might use something like below:

for word in word_list:
    if len(word) > 3:
        #print()
Mario
  • 1,631
  • 2
  • 21
  • 51
0

you can use:

more_than_3 = [word for word in word_list if len(word) >=3]
more_than_3 .count("BETTER")

output:

8

or:

from collections import Counter
more_than_3 = [word for word in word_list if len(word) >=3]

Counter(more_than_3)

output:

Counter({'IS': 10, 'BETTER': 8, 'THAN': 8, 'THE': 6, 'TO': 5, 'OF': 3, 'ALTHOUGH': 3, 'NEVER': 3, 'BE': 3, 'ONE': 3, 'IDEA': 3, 'COMPLEX': 2, 'SPECIAL': 2, 'SHOULD': 2, 'UNLESS': 2, 'OBVIOUS': 2, 'WAY': 2, 'DO': 2, 'IT': 2, 'MAY': 2, 'NOW': 2, 'IF': 2, 'IMPLEMENTATION': 2, 'EXPLAIN': 2, 'A': 2, 'ZEN': 1, 'PYTHON': 1, 'BY': 1, 'TIM': 1, 'PETERS': 1, 'BEAUTIFUL': 1, 'UGLY': 1, 'EXPLICIT': 1, 'IMPLICIT': 1, 'SIMPLE': 1, 'COMPLICATED': 1, 'FLAT': 1, 'NESTED': 1, 'SPARSE': 1, 'DENSE': 1, 'READABILITY': 1, 'COUNTS': 1, 'CASES': 1, 'ARENT': 1, 'ENOUGH': 1, 'BREAK': 1, 'RULES': 1, 'PRACTICALITY': 1, 'BEATS': 1, 'PURITY': 1, 'ERRORS': 1, 'PASS': 1, 'SILENTLY': 1, 'EXPLICITLY': 1, 'SILENCED': 1, 'IN': 1, 'FACE': 1, 'AMBIGUITY': 1, 'REFUSE': 1, 'TEMPTATION': 1, 'GUESS': 1, 'THERE': 1, 'AND': 1, 'PREFERABLY': 1, 'ONLY': 1, 'THAT': 1, 'NOT': 1, 'AT': 1, 'FIRST': 1, 'YOURE': 1, 'DUTCH': 1, 'OFTEN': 1, 'RIGHT': 1, 'HARD': 1, 'ITS': 1, 'BAD': 1, 'EASY': 1, 'GOOD': 1, 'NAMESPACES': 1, 'ARE': 1, 'HONKING': 1, 'GREAT': 1, '': 1, 'LETS': 1, 'MORE': 1, 'THOSE': 1})
Tal Folkman
  • 2,368
  • 1
  • 7
  • 21
  • Can you explain where you cover OP's request *"... counts their occurrence only if the word has more than 3 characters...."* in your offered answer? – Mario Jan 20 '22 at 08:58