Python Count Occurrences of Letters, Words and Numbers in Strings and Lists

preview_player
Показать описание
In this Python tutorial, we will go over how to count occurrences of specific letters, words and numbers in strings and lists.
Рекомендации по теме
Комментарии
Автор

For more examples on different ways to get counts, please see tutorial titled: 'Python Grouping Data Using Itertools Groupby'

To return the number of items in a container use len(), use set() for unique

BELOW ARE A FEW WAY TO GET TALLY COUNTS

s1 = 'Earth Earth Earth Earth Earth Mars Mars Mars Mars Jupiter Jupiter Jupiter'

l1 = ['Earth', 'Earth', 'Earth', 'Earth', 'Earth', 'Mars', 'Mars', 'Mars', 'Mars', 'Jupiter', 'Jupiter', 'Jupiter']

# TALLY COUNT OF LETTERS IN STRING
def count_letters(l):
d = {}
for i in l:
if i not in d:
d[i] = 1
else:
d[i] += 1
count_sorted = sorted(d.items(), key = lambda t: t[1], reverse=True)
print(count_sorted)

count_letters(s1)
'''
prints
[('r', 12), (' ', 11), ('a', 9), ('t', 8), ('E', 5), ('h', 5), ('M', 4), ('s', 4), ('J', 3), ('u', 3), ('p', 3), ('i', 3), ('e', 3)]
'''

# TALLY COUNT OF WORDS IN STRING
def count_words(l):
d = {}
for i in l.split():
if i not in d:
d[i] = 1
else:
d[i] += 1
count_sorted = sorted(d.items(), key = lambda t: t[1], reverse=True)
print(count_sorted)

count_words(s1)
'''
prints
[('Earth', 5), ('Mars', 4), ('Jupiter', 3)]
'''

# TALLY COUNT OF LETTERS IN LIST
def count_letters_list(l):
d = {}
# access letters of words in list
l = [i for word in l1 for i in word]
for i in l:
if i not in d:
d[i] = 1
else:
d[i] += 1
count_sorted = sorted(d.items(), key = lambda t: t[1], reverse=True)
print(count_sorted)

count_letters_list(l1)
'''
prints
[('r', 12), ('a', 9), ('t', 8), ('E', 5), ('h', 5), ('M', 4), ('s', 4), ('J', 3), ('u', 3), ('p', 3), ('i', 3), ('e', 3)]
'''

# TALLY COUNT OF WORDS IN LIST
def count_words_list(l):
d = {}
for i in l:
if i not in d:
d[i] = 1
else:
d[i] += 1
count_sorted = sorted(d.items(), key = lambda t: t[1], reverse=True)
print(count_sorted)

count_words_list(l1)
'''
prints
[('Earth', 5), ('Mars', 4), ('Jupiter', 3)]
'''

# EXAMPLES USING COLLECTIONS MODULE
import collections

# count letters
l = ['a', 'b', 'b', 'c', 'c', 'c']
c = collections.Counter(l)
print(dict(c))
'''
prints {'a': 1, 'b': 2, 'c': 3}
where key is letter and value is count for that letter
'''

# count letters in words
letters = []
words = ['hello', 'hello']
for i in words:
for j in i:
letters.append(j)
count = collections.Counter(letters)
print(dict(count))
'''
prints
{'h': 2, 'e': 2, 'l': 4, 'o': 2}
'''

RyanNoonan
Автор

Thankyou so much I was stuck on this problem for over a week and this video helped me!

danielarbeauty
Автор

Dear Ryan, I'm a python noob and this really helped my current situation, thanks!

blackpeppericecream
Автор

omg this makes more sense to me i dunno why my prof made this so complex to understand thank you so much :)

krutarthpatel
Автор

thanks for your tutorial ... subscribed!

pauloandremotos
Автор

Hello...I was searching a code which was really easy and understandable. This coding is easy..Thankyou so much sir..

fadilmuhfath
Автор

Good job Ryan. Which editor are you using pse Ryan?

magotelecom
Автор

I was wondering about how we can count more than just one number in a list

mews
Автор

Write a function that find the frequency occurrence of a letter in a sentence. The function should return an integer. (Do not use the str .count() default python function)

FlamingoVlogs
Автор

Thanks for this, helped me understand a homework assignment! Still looking for a method to count instances of multiple characters inside a single call though...

seanearnest
Автор

Hi, i would like to undestand how to create a fuction that receives a string as a input then returns a dictionary with the frequency of each word in that string. Also, is it possible to do this return to remove pontuaction at the end of the words? Like ", " "!" "?"...

arturcarreira
Автор

Is there any way to do this by using loop. Thanks in advance :)

baqarali
Автор

how do you take a user input so then the code can see how many of the word the user put in?

yusufmohammed
Автор

Not sure if your still checking this video's comments, but I would like to say the speed in that typing. O.O are you using regular keyboard?!?

juanmelecio
Автор

How would you get it to count how many words are in the whole string?

bendyalien
Автор

can you use this to count symbols???? cause I've done it with numbers and it worked and that really helped but it doesn't seam to work for symbols and i was wondering if i had coded something wrong just so u know how this helped with the numbers i was searching for hours to find the solution and i was so happy when i did thank

isabelladewhurst
Автор

Hello sir, I didn't give a gap between single quotes.. I got count as 40 and can you tell me what does it mean ?what is 40 indicating here? If i count the number of letters and spaces it's totally 39 and not 40 but the output is 40 please explain sir...

tanu
Автор

Why did you use format at the first example?

bilgeersayn
Автор

Thanks this was helpful but am doing an exercise where we are counting the number of occurrences of string ‘bob’ and maybe a string like ‘bobobadcfeg’ if we use this code it will bring one but the exercise says it’s two plz help

Stxrry_it
Автор

But how do I count the number of a's AND b's inside the string?

DutchDread
welcome to shbcf.ru