["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/