gpt4 book ai didi

Python:如何合并显示为句子并显示行号

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

import re
line = "the heart was made to be broken"
line_split2 = re.split(r'[ \t\n\r, ]+',line)

def chunks(line_split2, n):
for i in xrange(0, len(line_split2), n):
yield line_split2[i:i+n]

separate_word = list(chunks(line_split2, 3))

import pprint
pprint.pprint(separate_word)

count = 0
for lines in separate_word:
count = count + 1
print count

我正在尝试合并列表以显示为句子并在它们前面显示行号。

1 the heart was
2 made to be
3 broken

有什么建议吗?

最佳答案

你可以使用enumerate():

s = ['the heart was', 'made to be', 'broken']

for i, line in enumerate(s, 1):
print '%d %s' %(i, line)

1 the heart was
2 made to be
3 broken

有关枚举的更多信息,请参阅 http://docs.python.org/library/functions.html#enumerate

关于Python:如何合并显示为句子并显示行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10465696/

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