从 Python 上的 For 循环接收到的重复值

问题描述 投票:0回答:0

我在 Python 中运行一个 for 循环,它多次出现相同的值。一直在尝试一切,但我无法找到我的错误所在。

我正在尝试使用以下代码将文本分成长度为 100 的块:

clean_file_body_string 是我的文本 上下文,我的文件有将近 500k 个字符。 我注意到“打印(元)”和我的文件上的重复值

from tqdm.auto import tqdm  # this is our progress bar

batch_size = math.ceil(len(clean_file_body_string)/100)

for i in tqdm(range(0, len(clean_file_body_string), 100)):
    # set end position of each batch to take only what is needed
    i_end = min(i+batch_size, len(clean_file_body_string))
    # get batch of lines and IDs
    #Next code is takes the text and puts it into chunks
    lines_batch = [clean_file_body_string[i:i+100] for i in range(0, len(clean_file_body_string), 100)]
    ids_batch = [str(n) for n in range(i, i_end)]
    meta = [{'text': lines_batch} for i in range(0, len(text_chunks), 100)]
    print(meta)

一直在尝试不同的方法,但这段代码似乎更简单,而且是我几乎成功实现的唯一一种方法。

考虑到我还在学习 python。

python
© www.soinside.com 2019 - 2024. All rights reserved.