Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在嵌套列表中计算频率_Python_List - Fatal编程技术网

Python 在嵌套列表中计算频率

Python 在嵌套列表中计算频率,python,list,Python,List,我试图使用嵌套列表中的词典计算单词的频率。每个嵌套列表是一个分解成每个单词的句子。另外,我想删除句子开头的专有名词和小写单词。甚至可以使用专有名词吗 x = [["Hey", "Kyle","are", "you", "doing"],["I", "am", "doing", "fine"]["Kyle", "what", "time" "is", "it"] from collections import Counter def computeFrequencies(x): coun

我试图使用嵌套列表中的词典计算单词的频率。每个嵌套列表是一个分解成每个单词的句子。另外,我想删除句子开头的专有名词和小写单词。甚至可以使用专有名词吗

x = [["Hey", "Kyle","are", "you", "doing"],["I", "am", "doing", "fine"]["Kyle", "what", "time" "is", "it"]

from collections import Counter
def computeFrequencies(x):
    count = Counter()
    for listofWords in L:
        for word in L:
            count[word] += 1
    return count
它返回一个错误:不可损坏类型:“列表”

我想在字典周围不带计数器()的情况下准确返回:

{"hey": 1, "how": 1, "are": 1, "you": 1, "doing": 2, "i": , "am": 1, "fine": 1, "what": 1, "time": 1, "is": 1, "it": 1}

由于您的数据是嵌套的,您可以使用
链将其展平

from itertools import chain
from collections import Counter
print Counter(chain.from_iterable(x))
# Counter({'doing': 2, 'Kyle': 2, 'what': 1, 'timeis': 1, 'am': 1, 'Hey': 1, 'I': 1, 'are': 1, 'it': 1, 'you': 1, 'fine': 1})
my_counter = {}
for line in x:
    for word in line:
        my_counter[word] = my_counter.get(word, 0) + 1
print my_counter
from collections import defaultdict
my_counter = defaultdict(int)
for line in x:
    for word in line:
        my_counter[word] += 1

print my_counter
如果要使用生成器表达式,则可以执行以下操作

from collections import Counter
print Counter(item for items in x for item in items)
如果您想在不使用计数器的情况下执行此操作,则可以使用这样的普通字典

from itertools import chain
from collections import Counter
print Counter(chain.from_iterable(x))
# Counter({'doing': 2, 'Kyle': 2, 'what': 1, 'timeis': 1, 'am': 1, 'Hey': 1, 'I': 1, 'are': 1, 'it': 1, 'you': 1, 'fine': 1})
my_counter = {}
for line in x:
    for word in line:
        my_counter[word] = my_counter.get(word, 0) + 1
print my_counter
from collections import defaultdict
my_counter = defaultdict(int)
for line in x:
    for word in line:
        my_counter[word] += 1

print my_counter
您还可以使用
collections.defaultdict
,如下所示

from itertools import chain
from collections import Counter
print Counter(chain.from_iterable(x))
# Counter({'doing': 2, 'Kyle': 2, 'what': 1, 'timeis': 1, 'am': 1, 'Hey': 1, 'I': 1, 'are': 1, 'it': 1, 'you': 1, 'fine': 1})
my_counter = {}
for line in x:
    for word in line:
        my_counter[word] = my_counter.get(word, 0) + 1
print my_counter
from collections import defaultdict
my_counter = defaultdict(int)
for line in x:
    for word in line:
        my_counter[word] += 1

print my_counter
好的,如果您只想将
计数器
对象转换为
dict
对象(我认为这根本不必要,因为
计数器
实际上是一个字典。您可以像普通字典对象一样访问键值、迭代、删除和更新
计数器
对象),您可以使用


问题是您在
L
上迭代了两次

更换内环:

for word in L:
与:


不过,如果你想成为“pythonic”,请查看@thefourtheye的解决方案。

循环中的
L
是什么。我很抱歉没有改变它@第四,很难明确地排除专有名词和模糊词。例如,您可以使用字典中的单词列表,但“kyle”在字典中被称为“一条狭窄的海峡”。如何去掉它周围的Counter()@如果你想要一个dict,你也可以只做
dict(Counter(chain.from_iterable(x))
@bsoist我不明白OP在问什么。谢谢你的澄清:)我在回答中也包括了这一点。谢谢,但实际上,这可能不是他的意思,所以我们应该澄清一下,计数器的行为类似于字典,你可以按键引用。@bsoist我试着解释得更多一些。请检查。+1我不知道
L
是什么。我在问题中留下了评论,但你已经明白了:)