gpt4 book ai didi

python - 统计频率,如何每次取两个单词?

转载 作者:太空宇宙 更新时间:2023-11-04 00:59:03 25 4
gpt4 key购买 nike

["this","example"]:1  , ["is","silly"]:1  ....

类似的东西。我可以处理单个单词的情况,但是您如何访问两个元素并让它们成为键?

with open(sys.argv[1], 'rb') as f:
word_list = f.read().lower()

unwanted = ['(', ')', '\\', '"', '\'','.',';',':','!']

for c in unwanted:
word_list = word_list.replace(c," ")

words = word_list.split()

fdic = {}

for word in words:

# form dictionary how can you let key be pair of item?
fdic[word] = fdic.get(word,0) + 1

最佳答案

您可以使用遍历原始单词列表的列表推导式来获取二元组列表:

bigrams = [word_list[i] + " " + wordlist[i+1] for i in range(len(wordlist)-1)]

关于python - 统计频率,如何每次取两个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com