gpt4 book ai didi

python - 将文档的每一行分成 n 组

转载 作者:行者123 更新时间:2023-11-28 21:21:30 25 4
gpt4 key购买 nike

def ngram(n, k, document):
f = open(document, 'r')
for i, line in enumerate(f):
words = line.split() + line.split()
print words
return {}

对于前“我喜欢 Python 编程语言”和 n = 2是“我爱”、“爱”、“Python”、“Python编程”和“编程语言”;

我想存储在一个列表中,然后比较有多少相同。

最佳答案

您想要返回的内容并不完全清楚。假设一行说:

我喜欢 Python 编程语言

而且你不想在线上做任何事情。

from collections import deque
def linesplitter(line, n):
prev = deque(maxlen=n) # fixed length list
for word in line.split(): # iterate through each word
prev.append(word) # keep adding to the list
if len(prev) == n: # until there are n elements
print " ".join(prev) # then start printing
# oldest element is removed automatically

with open(document) as f: # 'r' is implied
for line in f:
linesplitter(line, 2) # or any other length!

输出:

I love
love the
the Python
Python programming
programming language

关于python - 将文档的每一行分成 n 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21249857/

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