2017-09-15 165 views
0

如何计算一个单词在一列中的字符串出现在Python中的次数?例如:如何计算单词在一列中出现的次数,python

file|context 
----|------- 
1 | Hello world 
2 | Round and round 

我想计算的话发生:

file| context   | word_count 
----|-----------------|--------------------- 
1 | Hello world  | {'hello':1,'world':1} 
2 | Round and round | {'round':2,'and':1} 

我一直停留在它了整整一天,并试图用value_counts()和计数器。仍然无法弄清楚。任何帮助?

谢谢!

+0

你是如何尝试使用Counter和value_counts()? –

+0

你正在展示什么样的数据结构?如果您正在讨论解析文本表而不是像pandas'数据框这样的工作,你会得到非常不同的答案。可能会添加适当的标签('string'和'count'在这里非常无用)。 – Blckknght

回答

2

您可以在分割字符串的小写版本使用collections.Counter

from collections import Counter 

s = 'Round and round' 
counts = Counter(s.lower().split()) 
print(dict(counts)) 

输出:

 
{'and': 1, 'round': 2} 

接下来,你需要适应这与您的数据的工作。数据格式似乎使用固定宽度的字段,这样的背景下开始列在位置7.假设数据来自一个文件:

with open('data') as f: 
    next(f) # skip the header 
    next(f) # skip the border 
    # print new header and border 

    for line in f: 
     counts = Counter(line[6:].lower().split()) 
     print('{} | {}'.format(line, dict(counts))) 

还有一些工作要做计数正确格式化为输出列。

+0

感谢您的信息。它帮助了很多! – Lily

0

下面给出了一个字次的数的计数出现在字符串

str = "Round and round" 
dict1={} 
for eachStr in str.split(): 
    if eachStr.lower() in dict1.keys(): 
     count = dict1[eachStr] 
     count = count + 1 
     dict1[eachStr.lower()] = count 
    else: 
     dict1[eachStr.lower()] = 1 
print dict1 

OUTPUT:

{'and': 1, 'round': 2} 
0

您可以使用Python中,构建功能Counter用于这一目的。

In [5]: from collections import Counter 

In [6]: string = 'Hello world' 

In [9]: count = Counter(string.lower().split()) 

In [10]: print(dict(count)) 
{'world': 1, 'hello': 1} 

转换成的话,因为lowercase考虑Counter大写和小写的不同。

+1

谢谢你的帮助! – Lily

相关问题