我正在使用 python 进行频率字数统计,单进程版本:
#coding=utf-8
import string
import time
from collections import Counter
starttime = time.clock()
origin = open("document.txt", 'r').read().lower()
for_split = [',','\n','\t','\'','.','\"','!','?','-', '~']
#the words below will be ignoered when counting
ignored = ['the', 'and', 'i', 'to', 'of', 'a', 'in', 'was', 'that', 'had',
'he', 'you', 'his','my', 'it', 'as', 'with', 'her', 'for', 'on']
i=0
for ch in for_split:
origin = string.replace(origin, ch, ' ')
words = string.split(origin)
result = Counter(words).most_common(40)
for word, frequency in result:
if not word in ignored and …
Run Code Online (Sandbox Code Playgroud) 嘿,我对以下我编写的python代码有疑问:
#create a list of elements
#use a dictionary to find out the frequency of each element
list = [1,2,6,3,4,5,1,1,3,2,2,5]
list.sort()
dict = {i: list.count(i) for i in list}
print(dict)
Run Code Online (Sandbox Code Playgroud)
在字典压缩方法中,"for i in list"是提供给方法的序列吗?所以它需要1,2,3,4 ..作为键.我的问题是为什么不花1次?因为我已经说过"for i in list",所以不必将列表中的每个元素都作为键吗?
(我是python的新手,所以对我来说很容易!)