我有一个句子列表,我需要代码使用从第一个单词开始的 4 次迭代多次分解成更小的短语
例如句子是:我是一个男人,我 12 岁
我需要程序将其转换为:
I
I am
I am a
I am a man
第一个循环结束,第 4 次迭代,继续第 2 个字
我可以通过编写一个名为 new_sen1 的单独函数来获得第一部分,该函数没有问题。我的第二个函数没有正确运行。这是我的代码:
sen = 'I am a man and I am 12 years old'
def new_sen2(sen):
split_sen = sen.split()
x = ' '
y = 0
i = 0
num_list = [0]
while num_list[-1] < 5:
split_sen.pop(0)
num_list.append(i)
while y < 5:
new_sen = x.join(split_sen[0:y])
print(new_sen)
y += 1
i += 1
我得到的结果是:
am
am a
am a man
am a man and
为什么我的 while 循环没有按照条件的规定再继续运行 4 次?感谢所有帮助,谢谢!
答案 0 :(得分:1)
sen = 'I am a man and I am 12 years old' #initial sentence (a)
convert_sen = sen.split(" ") #convert (a) to list = (b)
len_of_convert_sen= len(convert_sen) #determine length of (b)
item_in_convert_sen = 1 #initialise variable for iteration
while item_in_convert_sen < len_of_convert_sen:
word = convert_sen[:item_in_convert_sen]
convert_to_str =" ".join(map(str, word))
print(convert_to_str)
item_in_convert_sen +=1
答案 1 :(得分:0)
我不确定您的预期输出是什么,但也许这段代码正是您想要的:
sen = 'I am a man and I am 12 years old'
sen=sen.split(" ")
for i_word in range(len(sen)):
for l in range(i_word+1,min(i_word+5,len(sen))):
print(" ".join(sen[i_word:l]))
答案 2 :(得分:0)
我认为最好的方法是将句子字符串拆分为一个列表,然后一次打印出一个单词,在每个 for 循环中增加。
这可以通过
sen = 'I am a man and I am 12 years old'
words = sen.split()
for i in range(5):
print(" ".join(words[:i]))
答案 3 :(得分:0)
这是一个很长的解释,请耐心等待。
我假设您调用“new_sen2”是一种初始化。
def new_sen2(sen):
split_sen = sen.split() # split_sen is a list of strings from sen split by space
x = ' ' # x is a str of space
y = 0 # y is int 0
i = 0 # i is int 0
num_list = [0] # num_list is a list with one element which is int 0
我会将 while 循环中的 while 循环展开 5 次,以便于理解。
while num_list[-1] < 5: # 1st iteration num_list[-1] = 0 which is <5
split_sen.pop(0) # remove first element in split_sen, which is 'I'
num_list.append(i) # add 'I' to new_list. nw_list now = [0, 'I']
while y < 1: # 1st iteration y = 0 which is < 1
new_sen = x.join(split_sen[0:y]) # new sen = join split_sen[0:0] which equals '' (empty string)
print(new_sen) # prints empty string
y += 1 # add 1 to y, y = 1
while y < 2: # 2nd iteration y = 1 which is < 2
new_sen = x.join(split_sen[0:y]) # new sen = join split_sen[0:1] which equals 'am'
print(new_sen) # prints 'am' string
y += 1 # add 1 to y, y = 2
while y < 3: # 3rd iteration y = 2 which is < 3
new_sen = x.join(split_sen[0:y]) # new sen = join split_sen[0:2] which equals 'am a'
print(new_sen) # prints 'am a' string
y += 1 # add 1 to y, y = 3
while y < 4: # 4th iteration y = 3 which is < 4
new_sen = x.join(split_sen[0:y]) # new sen = join split_sen[0:3] which equals 'am a man'
print(new_sen) # prints 'am a man' string
y += 1 # add 1 to y, y = 4
while y < 5: # 5th iteration y = 4 which is < 5
new_sen = x.join(split_sen[0:y]) # new sen = join split_sen[0:3] which equals 'am a man and'
print(new_sen) # prints 'am a man and' string
y += 1 # add 1 to y, y = 5
i += 1 # add 1 to i, i = 1
然后返回到外部 while 循环并导致 TypeError 因为 num_list[-1] = 'I'
无法与 5 进行比较。因此您的程序在那里停止。
你可以这样做:
for i in range(len(sen_split)-3):
for j in range(1, 5):
print(' '.join(sen_split[i:i+j]))
这个输出是:
I
I am
I am a
I am a man
am
am a
am a man
am a man and
a
a man
a man and
a man and I
man
man and
man and I
man and I am
and
and I
and I am
and I am 12
I
I am
I am 12
I am 12 years