我在HiveQL数据库中有十亿个字符串。我正在将它们加载到Python中。字符串不是以空格分隔的,如下所示:
"mynameisanon"
...
"helloworld"我想数一下每个字符串中的单词数。但首先,我需要一本字典。我不知道如何获得单词字典,但假设我有以下字典:
{ hello, world, my, name, is}则该函数的工作方式如下:
Input: mynameisanon
Output: 3
Input: helloworld
Output: 2最后,我想要一张熊猫桌子。
发布于 2019-07-15 06:38:49
正如我在评论中提到的,在一般情况下,这不可能是唯一的,但假设有一个字典来说明这一点:
(没有经过特别好的测试):
strings = ["mynameisanon", "helloworld"]
words = ["hello", "world", "my", "name", "is"]
for string in strings:
count = 0
max_interval = len(string)
for interval_length in range(1,max_interval+1):
for interval_start in range(0, len(string)+1-interval_length):
interval = string[interval_start:(interval_start+interval_length)]
if interval in words:
count += 1
print(string)
print(count)这假设单词可以小到一个字母,长到整个字符串,并检查介于这些值之间的所有长度的单词
https://stackoverflow.com/questions/57031524
复制相似问题