Python counter words not letters

依然范特西╮ 提交于 2019-12-11 06:43:57

问题


I'm trying to create a program which reads in a text file and find the number of individual words. I have worked out most of it but I am stuck on trying to get the counter to pick out words not letters as it is currently doing.

import collections 

with open ("file.txt" ,"r") as myfile:
    data=myfile.read()
[i.split(" ") for i in data]

x=collections.Counter(data)

print (x)

My aim was to slip the list by whitespace which would result in each word being a object in the list. This however did not work.

Result:

Counter({' ': 1062, 'e': 678, 't': 544, 'o': 448, 'n': 435, 'a': 405, 'i': 401, 'r': 398,       's': 329, 'c': 268, 'm': 230, 'h': 216, 'u': 212, 'd': 190, 'l': 161, 'p': 148, 'f': 107, 'g': 75, 'y': 68, '\n': 65, ',': 61, 'b': 55, 'w': 55, 'v': 55, '.': 53, 'N': 32, 'A': 20, 'T': 19, '"': 18, ')': 17, '(': 17, 'C': 17, 'k': 16, "'": 16, 'I': 16, 'x': 15, '-': 14, 'E': 13, 'q': 12, 'V': 10, 'U': 9, ';': 7, '1': 6, 'j': 5, '4': 5, 'P': 5, 'D': 5, '9': 5, 'L': 4, 'z': 4, 'W': 4, 'O': 3, 'F': 3, '5': 3, 'J': 2, '3': 2, 'S': 2, 'R': 2, '0': 1, ':': 1, 'H': 1, '2': 1, '/': 1, 'B': 1, 'M': 1, '7': 1})

回答1:


Your list comprehension is never assigned and thus doesn't do anything.

Pass the split text to collections.Counter():

x = collections.Counter(data.split())

and I used str.split() without arguments to make sure you split on arbitrary width whitespace and include newlines when splitting as well; your Counter() has 65 newlines that need not be there, for example.

In context and a little more compact:

from collections import Counter

with open ("file.txt") as myfile:
    x = Counter(myfile.read().split())

print(x)


来源:https://stackoverflow.com/questions/22563929/python-counter-words-not-letters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!