如何计算页面上所有单词的所有唯一实例?(Python/Selenium)
[Python/Selenium]如何计算页面上所有单词的所有唯一实例如何计算页面上所有单词的所有唯一实例?(Python/Selenium),python,selenium,Python,Selenium,[Python/Selenium]如何计算页面上所有单词的所有唯一实例 例如,在这个页面上,我想创建一个脚本来注册页面上的所有单词,然后计算每个单词出现的实例数。我不需要完整的代码分解,只需要指向正确的方向。如果可以将整个页面存储在一个字符串变量中,那么您可以使用带有FreqDist函数的包,这正好完成了任务。如果可以将整个页面存储在一个字符串变量中,那么您可以使用带有FreqDist函数的包,您可以使用集合模块中的计数器,如下所示 from collections import Counter
例如,在这个页面上,我想创建一个脚本来注册页面上的所有单词,然后计算每个单词出现的实例数。我不需要完整的代码分解,只需要指向正确的方向。如果可以将整个页面存储在一个字符串变量中,那么您可以使用带有FreqDist函数的包,这正好完成了任务。如果可以将整个页面存储在一个字符串变量中,那么您可以使用带有FreqDist函数的包,您可以使用
集合
模块中的计数器
,如下所示
from collections import Counter
sentence = "For example, on this page, I want to create a script that registers all words on the page and then counts how many instances of each word appears. I don't need the full code breakdown, just to be pointed in the right direction."
word_count = Counter(sentence .split())
print(word_count)
这将输出:
Counter({'the': 3, 'on': 2, 'I': 2, 'to': 2, 'For': 1, 'example,': 1, 'this': 1, 'page,': 1, 'want': 1, 'create': 1, 'a': 1, 'script': 1, 'that': 1, 'registers': 1, 'all': 1, 'words': 1, 'page': 1, 'and': 1, 'then': 1, 'counts': 1, 'how': 1, 'many': 1, 'instances': 1, 'of': 1, 'each': 1, 'word': 1, 'appears.': 1, "don't": 1, 'need': 1, 'full': 1, 'code': 1, 'breakdown,': 1, 'just': 1, 'be': 1, 'pointed': 1, 'in': 1, 'right': 1, 'direction.': 1})
如果需要,可以通过以下方式将输出进一步转换为字典:
word_count = dict(Counter(sentence .split()))
print(word_count)
哪个会输出
{'For': 1, 'example,': 1, 'on': 2, 'this': 1, 'page,': 1, 'I': 2, 'want': 1, 'to': 2, 'create': 1, 'a': 1, 'script': 1, 'that': 1, 'registers': 1, 'all': 1, 'words': 1, 'the': 3, 'page': 1, 'and': 1, 'then': 1, 'counts': 1, 'how': 1, 'many': 1, 'instances': 1, 'of': 1, 'each': 1, 'word': 1, 'appears.': 1, "don't": 1, 'need': 1, 'full': 1, 'code': 1, 'breakdown,': 1, 'just': 1, 'be': 1, 'pointed': 1, 'in': 1, 'right': 1, 'direction.': 1}
您可以使用
集合
模块中的计数器
,如下所示
from collections import Counter
sentence = "For example, on this page, I want to create a script that registers all words on the page and then counts how many instances of each word appears. I don't need the full code breakdown, just to be pointed in the right direction."
word_count = Counter(sentence .split())
print(word_count)
这将输出:
Counter({'the': 3, 'on': 2, 'I': 2, 'to': 2, 'For': 1, 'example,': 1, 'this': 1, 'page,': 1, 'want': 1, 'create': 1, 'a': 1, 'script': 1, 'that': 1, 'registers': 1, 'all': 1, 'words': 1, 'page': 1, 'and': 1, 'then': 1, 'counts': 1, 'how': 1, 'many': 1, 'instances': 1, 'of': 1, 'each': 1, 'word': 1, 'appears.': 1, "don't": 1, 'need': 1, 'full': 1, 'code': 1, 'breakdown,': 1, 'just': 1, 'be': 1, 'pointed': 1, 'in': 1, 'right': 1, 'direction.': 1})
如果需要,可以通过以下方式将输出进一步转换为字典:
word_count = dict(Counter(sentence .split()))
print(word_count)
哪个会输出
{'For': 1, 'example,': 1, 'on': 2, 'this': 1, 'page,': 1, 'I': 2, 'want': 1, 'to': 2, 'create': 1, 'a': 1, 'script': 1, 'that': 1, 'registers': 1, 'all': 1, 'words': 1, 'the': 3, 'page': 1, 'and': 1, 'then': 1, 'counts': 1, 'how': 1, 'many': 1, 'instances': 1, 'of': 1, 'each': 1, 'word': 1, 'appears.': 1, "don't": 1, 'need': 1, 'full': 1, 'code': 1, 'breakdown,': 1, 'just': 1, 'be': 1, 'pointed': 1, 'in': 1, 'right': 1, 'direction.': 1}