DAOrmiston
DAOrmiston

Reputation: 11

How to apply a for-loop to each python list item?

I'm new to stackoverflow and I'm working on a python challenge where: Given a string, I must return the number of occurrences of each word in the string.

I have a code written and what I'd like is to have my for-loop continually run each word in the string, but it only runs for a "placeholder variable" I included to represent words in the string.

Here's the code:

d={}

vari='When I find myself in times of trouble Mother Mary comes to me Speaking words of wisdom let it be And in my hour of darkness she is standing right in front of me Speaking words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the broken hearted people living in the world agree There will be an answer let it be For though they may be parted there is still a chance that they will see There will be an answer let it be Let it be let it be let it be let it be There will be an answer let it be Let it be let it be let it be let it be Whisper words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the night is cloudy there is still a light that shines on me Shine until tomorrow let it be I wake up to the sound of music Mother Mary comes to me Speaking words of wisdom let it be Let it be let it be let it be yeah let it be There will be an answer let it be Let it be let it be let it be yeah let it be Whisper words of wisdom let it be'.split()

for c in vari:
    d['c']=vari.count('c');

for key,value in d.items():
    print key,
    print value

Sorry for the long string. Any thoughts would be appreciated.

Upvotes: 0

Views: 99

Answers (4)

user7870824
user7870824

Reputation:

Your code can be as compact as this:

result = {word:vari.split().count(word) for word in vari.split()}
print result

Upvotes: 1

foslock
foslock

Reputation: 3939

Given a string, I must return the number of occurrences of each word in the string.

This can be accomplished by splitting the string in a list of words, iterating over each word, keeping track of how many times it has previously been seen. The below examples assume that case of the word does matter. One can use the lower() method on the strings to turn it into a case-insensitive count.

Vanilla Approach

sentence = 'My very very long string'

words = sentence.split()
counts = {}
for word in words:
    if word not in counts:
        counts[word] = 1
    else:
        counts[word] += 1

print(counts)

This prints out the dictionary you want:

{'very': 2, 'My': 1, 'string': 1, 'long': 1}

Counter Class (from collections package)

If you don't mind importing a standard library, you can use the collections.Counter class instead of a native dict. It's values default to 0 for unassigned keys, making it useful for this exact use case.

import collections
counter = collections.Counter(words)
print(counter)

This is a dictionary-like object that contains the same information as above:

Counter({'very': 2, 'My': 1, 'string': 1, 'long': 1})

Upvotes: 2

ILostMySpoon
ILostMySpoon

Reputation: 2409

Instead of using the literal String 'c' as the key for each word, pass the variable c that is initialized at each step within your for loop as the key.

for c in vari:
    d[c]=vari.count(c)

Alternatively you can use collections.Counter:

from collections import Counter

words = vari.split()
wordCount = Counter(words)

Upvotes: 1

Michal K
Michal K

Reputation: 337

Remove quotes surrounding 'c':

d={}

vari='When I find myself in times of trouble Mother Mary comes to me Speaking words of wisdom let it be And in my hour of darkness she is standing right in front of me Speaking words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the broken hearted people living in the world agree There will be an answer let it be For though they may be parted there is still a chance that they will see There will be an answer let it be Let it be let it be let it be let it be There will be an answer let it be Let it be let it be let it be let it be Whisper words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the night is cloudy there is still a light that shines on me Shine until tomorrow let it be I wake up to the sound of music Mother Mary comes to me Speaking words of wisdom let it be Let it be let it be let it be yeah let it be There will be an answer let it be Let it be let it be let it be yeah let it be Whisper words of wisdom let it be'.split()

for c in vari:
    d[c]=vari.count(c);

for key,value in d.items():
    print key,
    print value

Upvotes: 0

Related Questions