【问题标题】:How to return the word count given one list of strings and one list of lists?给定一个字符串列表和一个列表列表,如何返回字数?
【发布时间】:2015-03-26 23:52:30
【问题描述】:

假设我有一长串带有标点符号、空格等的列表,如下所示:

list_1 = [[the guy was plaguy but unable to play football, but he was able to play tennis],[That was absolute cool],...,[This is an implicit living.]]

我还有一个长长的列表是这样的:

list_2 =['unable', 'unquestioning', 'implicit',...,'living', 'relative', 'comparative']

对于list_1 的每个子列表,如何提取出现在list_2 中的所有单词的计数或频率?例如给定上面的列表:

list_2 =['unable', 'unquestioning', 'implicit',...,'living', 'relative', 'comparative']

[the guy was unable to play football, but he was able to play tennis]

由于无法出现在list_2 的上一个子列表中,因此该列表的计数为1。

list_2 =['unable', 'unquestioning', 'implicit',...,'living', 'relative', 'comparative']

[That was absolute cool]

由于之前的子列表中没有出现list_2的单词,所以计数为0。

list_2 =['unable', 'unquestioning', 'implicit',...,'living', 'relative', 'comparative']

[This is an implicit living.]

由于implicit 和 living 出现在list_2 的上一个子列表中,因此该列表的计数为2。

比期望的输出是[1,0,2]。

知道如何处理此任务以返回计数列表吗?在此先感谢各位。

例如:

>>> [sum(1 for word in list_2 if word in sentence) for sublist in list_1 for sentence in sublist]

错了,因为混淆了guy 和playguy 这两个词。知道如何解决这个问题吗?

【问题讨论】:

  • 如果您将列表或字符串转换为单词集,那么剩下的问题就变得容易解决了。一般来说,您应该尝试发布一些需要修复/改进的特定问题的代码。
  • 如果输入列表是['this is is a test']和['is'],你想要的输出是[1]还是[2]?
  • 感谢您的反馈@TomDalton [1]

标签: python arrays list tuples counter


【解决方案1】:

将内置函数 sum 与列表理解一起使用

>>> list_1 = [['the guy was unable to play football, but he was able to play tennis'],['That was absolute cool'],['This is implicit living.']]
>>> list_2 =['unable', 'unquestioning', 'implicit','living', 'relative', 'comparative']   
>>> [sum(1 for word in list_2 if word in sentence) for sublist in list_1 for sentence in sublist]

[1, 0, 2]

【讨论】:

  • 感谢您的帮助。这种方法的问题在于,在我的实际列表中,例如plaguy 和guy 这个过程混淆了plaguy 和`guy 是不同的词这一事实。知道如何解决这个问题吗?。
  • 我不明白这个问题。无论您匹配的单词列表如何,计数都将保持不变。
  • 问题是,当我使用上面的列表列表和更大的字符串列表进行测试时,数字不匹配。有什么办法可以把大名单发给你吗?
【解决方案2】:

我宁愿使用正则表达式。首先,因为您需要匹配整个单词,这与其他字符串搜索方法相比很复杂。而且,即使它看起来像火箭筒,它通常也非常有效。

您首先从list_2 生成一个正则表达式,然后使用它搜索list_1 的句子。正则表达式的构造如下:"(\bword1\b|\bword2\b|...)",这意味着 “整个 word1 或整个 word2 或...”。 \b 表示匹配单词的开头或结尾。

我假设您想要 list_1 的每个子列表而不是每个子列表的每个句子的结果。

_regex = re.compile(r"(\b{}\b)".format(r"\b|\b".join(list_2)))
word_counts = [ 
    sum(
        sum(1 for occurence in _regex.findall(sentence))
        for sentence in sublist
    ) for sublist in list_1
]

Here you can find a whole sample code 与普通字符串搜索的性能比较,知道匹配整个单词需要更多的工作,因此效率会更低。

【讨论】:

  • 感谢您的支持。这实际上奏效了,知道如何只用一个列表而不是一个函数来解决这个问题吗?谢谢!
  • @newWithPython:我编辑了代码,但我不确定这就是您要问的。如果不是,你能解释一下你的意思吗?
  • 对不起...这个任务令人困惑。这种方法的问题在于,例如,假设我通过了这个列表:list_2 =['unable', 'unquestioning', 'implicit',...,'living', 'relative', 'comparative, 'cool', uncool],然后我使用了另一个列表,它实际上是我想要计算出现在的单词的频率的列表:list_1 = [[the guy was plaguy but unable to play football, but he was able to play tennis],[That was absolute cool],...,[This is an implicit living.]]。
  • 对不起,我很难理解您要解释的内容。您是否正在尝试获取每个单词的出现次数?
  • 我的意思是,你想要 "list_2 中所有单词出现在 list_1 中的次数" 还是想要 "单词出现的次数list_2中的1出现在list_1中,list_2中的单词2出现在list_1中的次数,以此类推" ?
【解决方案3】:

诀窍是使用 split() 方法和列表推导。如果只用空格分隔:

list_1 = ["the guy was unable to play football but he was able to play tennis", "That was absolute cool", "This is implicit living"]

list_2 =['unable', 'unquestioning', 'implicit','living', 'relative', 'comparative']

print([sum(sum(1 for j in list_2 if j in i.split()) for i in k for k) inlist_1])

但是,如果你想使用所有非字母数字来标记,你应该使用re:

import re

list_1 = ["the guy was unable to play football,but he was able to play tennis", "That was absolute cool", "This is implicit living"]
list_2 =['unable', 'unquestioning', 'implicit','living', 'relative', 'comparative']

print(sum([sum(1 for j in list_2 if re.split("\W",i)) for i in k) for k in list_1])

\W 字符集都是非字母数字的。

【讨论】:

  • 我收到了这个错误:Traceback (most recent call last): File "/Users/user/PycharmProjects/PAN-pruebas/urgente.py", line 11, in <module> res = ([sum(i.split().count(j) for j in list_2) for i in list_1]) File "/script.py", line 11, in <genexpr> res = ([sum(i.split().count(j) for j in list_2) for i in list_1]) AttributeError: 'list' object has no attribute 'split'
  • @newWithPython 哦,我认为包含在列表中的每个字符串都是错字。有什么理由让它们被这样包裹吗?这些列表之一中可以有多个字符串吗?我会接受我的回答。
  • 每个子列表都是一个句子,这就是为什么我一直在烦恼。
  • @newWithPython 可以,但是每个子列表可以有多个字符串吗?
  • 是的,可以有多个。感谢您的提要
猜你喜欢
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多