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

标签 python

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

相关文章:

python - Pygame 播放列表在后台连续播放

Python 进度条

python - 类继承,使用新参数重新定义 __init__

python - 列表理解列表列表

python - "coverage run app.py"在 docker 容器内未创建 .coverage 文件?

python - sys.stdout.write 对于 Windows 上的二进制文件无法正常工作

python - Airflow 中的 op_kwargs 和 templates_dict od PythonOperator 有什么区别?

python - 在 Python 中给一个类起别名

python - 导入错误: bad magic number in 'dateparser' : b'\x03\xf3\r\n'

Python rpi GPIO输出控制基于GPIO输入和一天中的时间