【问题标题】:How to return the count of words from a list of words that appear in a list of lists?如何从出现在列表列表中的单词列表中返回单词的计数?
【发布时间】:2015-05-31 00:20:52
【问题描述】:

我有一个非常大的字符串列表,如下所示:

list_strings = ['storm', 'squall', 'overcloud',...,'cloud_up', 'cloud_over', 'plague', 'blight', 'fog_up', 'haze']

还有很多这样的列表:

lis_of_lis = [['the storm was good blight'],['this is overcloud'],...,[there was a plague stormicide]]

如何在lis_of_lis 的每个子列表中返回list_strings 中出现的所有单词的计数列表。例如,对于上面的示例,这将是所需的输出:[2,1,1]

例如:

['storm', 'squall', 'overcloud',...,'cloud_up', 'cloud_over', 'plague', 'blight', 'fog_up', 'haze']

['the storm was good blight']

计数为 2,因为 stormblight 出现在第一个子列表 (lis_of_lis) 中

['storm', 'squall', 'overcloud',...,'cloud_up', 'cloud_over', 'plague', 'blight', 'fog_up', 'haze']

['this is overcloud stormicide']

计数为1,因为overcloud 出现在第一个子列表(lis_of_lis)中

因为杀风暴剂没有出现在第一个列表中

['storm', 'squall', 'overcloud',...,'cloud_up', 'cloud_over', 'plague', 'blight', 'fog_up', 'haze']

[there was a plague]

计数为1,因为plague 出现在第一个子列表(lis_of_lis)中

因此是所需的输出[2,1,1]

所有答案的问题在于计算一个单词中的所有子字符串而不是完整单词

【问题讨论】:

  • [2,1,1] 将如何成为所需的输出?
  • storm and blight 匹配lis_of_lis 中的第一项,overcloud 是第二项中的单项,瘟疫是第三项中的单项:[2,1,1]
  • 我编辑了谢谢大家的帮助!

标签: python list python-2.7 data-structures


【解决方案1】:

您可以在列表理解中使用sum 函数:

[sum(1 for i in list_strings if i in sub[0]) for sub in lis_of_lis]

【讨论】:

  • @newWithPython 对不起,我省略了for 现在试试吧!
  • 您应该将第一个 sub 更改为 sub[0],因为 lis_of_lis 是一个列表列表。
  • 感谢您的帮助,这种方法的问题在于计算子字符串。知道如何计算完整的字符串吗?提前致谢
  • [sum(1 for word in list_strings if word in sentence) for sublist in lis_of_lis for sentence in sublist]
  • @ILostMySpoon 感谢您的提醒!我认为sub 的元素被分割了!
【解决方案2】:
result = []
for sentence in lis_of_lis:
    result.append(0)
    for word in list_strings:
        if word in sentence[0]:
            result[-1]+=1
print(result)

这是长版的

result = [sum(1 for word in list_strings if word in sentence[0])  for sentence in lis_of_lis]

这将为您的示例返回 [2,2,1]。

如果您只想要整个单词,请在单词/句子之前和之后添加空格:

result = []
for sentence in lis_of_lis:
    result.append(0)
    for word in list_strings:
        if ' '+word+' ' in ' '+sentence[0]+' ':
            result[-1]+=1
print(result)

或短版:

result = [sum(1 for word in list_strings if ' '+word+' ' in ' '+sentence[0]+' ')  for sentence in lis_of_lis]

这将为您的示例返回 [2,1,1]。

【讨论】:

  • 感谢您的帮助,这种方法的问题在于计算子字符串。知道如何计算完整的字符串吗?提前致谢
  • 例如,考虑followinf lis_of_lis = [['the storm was good blight'], ['this is overcloud stormicide'], ['there was a plague']]
  • 这是对所有子字符串的计数,因此输出已损坏。知道如何修复它吗?提前谢谢。
  • 感谢您的帮助。对不起,如果我不清楚,我编辑了问题并添加了更多细节。这样做的问题是计算子串我想计算所有单词的频率。
【解决方案3】:

这将创建一个字典,其中 list_string 中的单词作为键,值从 0 开始。然后遍历 lis_of_lis,将短语拆分为单词列表,遍历该列表,并检查它们是否是在字典里。如果是,则将 1 添加到相应的值。

    word_count = dict()
    for word in list_string:
        word_count[word] = 0

    for phrase in lis_of_lis:
        words_in_phrase = phrase.split()
        for word in words_in_phrase:
            if word in word_count:
                word_count[word] += 1

这将创建一个字典,其中单词为键,频率为值。我会留给您从该数据结构中获取正确的输出。

【讨论】:

    猜你喜欢
    • 2017-03-11
    • 1970-01-01
    • 2021-02-05
    • 2020-07-19
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 2013-12-25
    • 2014-07-06
    相关资源
    最近更新 更多