我有一个非常大的字符串列表如下:
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]]
如何返回出现在list_strings
中lis_of_lis
的每个子列表上的所有单词的计数列表。例如,对于上面的示例,这将是所需的输出:[2,1,1]
例如:
['storm', 'squall', 'overcloud',...,'cloud_up', 'cloud_over', 'plague', 'blight', 'fog_up', 'haze']
['the storm was good blight']
计数是2,因为storm
和blight
出现在第一个子列表中(lis的lis)
['storm', 'squall', 'overcloud',...,'cloud_up', 'cloud_over', 'plague', 'blight', 'fog_up', 'haze']
['this is overcloud stormicide']
计数是1
,因为overcloud
出现在第一个子列表(lis的lis)
因为杀风暴剂没有出现在第一个列表中
['storm', 'squall', 'overcloud',...,'cloud_up', 'cloud_over', 'plague', 'blight', 'fog_up', 'haze']
[there was a plague]
计数是1
,因为plague
出现在第一个子列表(lis的lis)
因此是期望的输出[2,1,1]
所有答案的问题是计算一个单词中的所有子字符串而不是整个单词
哪个是长版的
这将返回[2,2,1]作为示例。你知道吗
如果只需要整词,请在单词/句子前后加空格:
或短版本:
这将返回[2,1,1]作为示例。你知道吗
可以在列表中使用
sum
函数:这将创建一个字典,其中list\u字符串中的单词作为键,值从0开始。然后它遍历列表,将短语分解成一个单词列表,遍历该列表,并检查它们是否在字典中。如果是,则在相应的值上加1。你知道吗
这将创建一个字典,其中单词作为键,频率作为值。我将让您从数据结构中获得正确的输出。你知道吗
相关问题 更多 >
编程相关推荐