如何将for循环应用于每个python列表项?

时间:2017-06-28 00:29:43

标签: python dictionary for-loop

我是stackoverflow的新手,我正在进行python挑战,其中:给定一个字符串,我必须返回字符串中每个单词的出现次数。

我编写了一个代码,我喜欢的是让我的for循环连续运行字符串中的每个单词,但它只运行一个"占位符变量"我包括在字符串中表示单词。

以下是代码:

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

抱歉长字符串。任何想法将不胜感激。

4 个答案:

答案 0 :(得分:2)

  

给定一个字符串,我必须返回字符串中每个单词的出现次数。

这可以通过拆分字符串列表中的字符串来实现,迭代每个单词,跟踪之前已经看过多少次。 下面的示例假设单词的大小写很重要。可以在字符串上使用lower()方法将其转换为不区分大小写的计数。

香草方法

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)

这会打印出你想要的字典:

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

计数器类(来自集合包)

如果您不介意导入标准库,则可以使用collections.Counter类而不是本机dict。对于未分配的键,它的值默认为0,这对于这个确切的用例非常有用。

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

这是一个类似字典的对象,包含与上面相同的信息:

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

答案 1 :(得分:1)

不是使用文字字符串c作为每个单词的键,而是将for循环中每个步骤初始化的变量for c in vari: d[c]=vari.count(c) 作为键传递。

collections.Counter

或者,您可以使用from collections import Counter words = vari.split() wordCount = Counter(words)

@Echo Off
If "%~1"=="" Exit/B
Set "fn=%~1"
Echo Checking %fn%...
SetLocal EnableDelayedExpansion
If Not "x%fn:-source=%"=="x%fn%" (
    Set "outfile=%fn:-source=%"
    Echo Output to !outfile!
)

答案 2 :(得分:1)

您的代码可以像这样紧凑:

md-select

答案 3 :(得分:0)

删除围绕' 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