繁体   English   中英

Dictionary只返回for循环中的最后一个键值对

[英]Dictionary returns only last key value pairs inside for loop

我有一个字符串列表:

A = [
    'philadelphia court excessive disappointed court hope hope',
    'hope hope jurisdiction obscures acquittal court',
    'mention hope maryland signal held mention problem internal reform life bolster level grievance'
    ]

和另一个列表:

B = ['court', 'hope', 'mention', 'life', 'bolster', 'internal', 'level']

我想根据字符串A列表中列表单词B出现次数创建字典。 就像是,

C = [
        {'count':2,'hope':2,'mention':0,'life':0,'bolster':0,'internal':0,'level':0},
        {'count':1,'hope':2,'mention':0,'life':0,'bolster':0,'internal':0,'level':0},
        {'count':0,'hope':1,'mention':2,'life':1,'bolster':1,'internal':1,'level':1}
    ]

我喜欢什么,

dic={}
for i in A:
    t=i.split()
    for j in B:
        dic[j]=t.count(j)

但是,它只返回最后一对字典,

print (dic)

{'court': 0,
 'hope': 1,
 'mention': 2,
 'life': 1,
 'bolster': 1,
 'internal': 1,
 'level': 1}

您不是像示例输出中那样创建一个dicts列表,而是创建一个单独的dict(并在每次检查一个短语时覆盖单词计数)。 您可以使用re.findall来计算每个短语中的单词出现次数(如果您的任何短语包含单词后跟标点符号,例如“希望?”),则可以不失败。

import re

words = ['court', 'hope', 'mention', 'life', 'bolster', 'internal', 'level']
phrases = ['philadelphia court excessive disappointed court hope hope','hope hope jurisdiction obscures acquittal court','mention hope maryland signal held mention problem internal reform life bolster level grievance']

counts = [{w: len(re.findall(r'\b{}\b'.format(w), p)) for w in words} for p in phrases]

print(counts)
# [{'court': 2, 'hope': 2, 'mention': 0, 'life': 0, 'bolster': 0, 'internal': 0, 'level': 0}, {'court': 1, 'hope': 2, 'mention': 0, 'life': 0, 'bolster': 0, 'internal': 0, 'level': 0}, {'court': 0, 'hope': 1, 'mention': 2, 'life': 1, 'bolster': 1, 'internal': 1, 'level': 1}]

两个问题:你是初始化dic在错误的地方,而不是收集那些dic在列表秒。 这是修复:

C = []    
for i in A:
    dic = {}
    t=i.split()
    for j in B:
        dic[j]=t.count(j)
    C.append(dic)
# Result:
[{'court': 2, 'hope': 2, 'mention': 0, 'life': 0, 'bolster': 0, 'internal': 0, 'level': 0},
{'court': 1, 'hope': 2, 'mention': 0, 'life': 0, 'bolster': 0, 'internal': 0, 'level': 0},
{'court': 0, 'hope': 1, 'mention': 2, 'life': 1, 'bolster': 1, 'internal': 1, 'level': 1}]

您始终使用dict[j]=t.count(j)覆盖dict dic的现有值。 您可以为每个i创建一个新的dict,并将其附加到以下列表:

dic=[]
for i in A:
    i_dict = {}
    t=i.split()
    for j in B:
        i_dict[j]=t.count(j)
    dic.append(i_dict)
print(dic)

要避免覆盖现有值,请检查该条目是否已在字典中。 尝试添加:

if j in b:
    dic[j] += t.count(j)
else:
    dic[j] = t.count(j)

试试这个,

from collections import Counter

A = ['philadelphia court excessive disappointed court hope hope',
     'hope hope jurisdiction obscures acquittal court',
     'mention hope maryland signal held mention problem internal reform life bolster level grievance']

B = ['court', 'hope', 'mention', 'life', 'bolster', 'internal', 'level']

result = [{b: dict(Counter(i.split())).get(b, 0) for b in B} for i in A]
print(result)

输出:

[{'court': 2, 'hope': 2, 'mention': 0, 'life': 0, 'bolster': 0, 'internal': 0, 'level': 0}, {'court': 1, 'hope': 2, 'mention': 0, 'life': 0, 'bolster': 0, 'internal': 0, 'level': 0}, {'court': 0, 'hope': 1, 'mention': 2, 'life': 1, 'bolster': 1, 'internal': 1, 'level': 1}]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM