如何计算一个单词出现在列python中的次数

时间:2017-09-15 03:52:37

标签: python string count

如何使用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()和Counter。仍然无法弄明白。有什么帮助吗?

谢谢!

3 个答案:

答案 0 :(得分: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)))

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

答案 1 :(得分: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

输出:

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

答案 2 :(得分:0)

为此,您可以使用python in-build function constructor(public events: Events) { /*========================================================= = Keep this block in any component you want to receive event response to = ==========================================================*/ // Event Handlers events.subscribe('menu:opened', () => { // your action here console.log('menu:opened'); }); events.subscribe('menu:closed', () => { // your action here console.log('menu:closed'); }); } /*===================================================== = Call these on respective events - I used them for Menu open/Close = ======================================================*/ menuClosed() { // Event Invoke this.events.publish('menu:closed', ''); } menuOpened() { // Event Invoke this.events.publish('menu:opened', ''); } }

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会以不同的方式考虑大写和小写。