我试图编写一个程序,要求用户输入一个位置列表,然后按顺序输入每个位置的一个单词并创建一个带有单词的句子,但我不知道如何连接2个输入一起形成一个句子
例如,如果我输入位置:
a
然后我输入单词:
form
输出应为:
1 2 3 4 5 1 2 3 4 5
到目前为止,我只知道如何让用户编写位置列表和这样的单词:
This is a repeated sentence
但我不知道如何连接2列表。有人可以帮助我吗?
答案 0 :(得分:0)
使用join()和split()
a = 'This is a repeated sentence'
b = a.split()
po = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
" ".join(b[i-1] for i in po)
结果
'This is a repeated sentence This is a repeated sentence'
答案 1 :(得分:0)
假设您有两个列表
positions = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
sentence = "This is a repeated sentence"
// create a mapping between positions and words
mapping = {}
words = sentence.split()
for (position, word) in zip(positions, words):
mapping[position] = word
// output according to the lists of positions
output = [mapping[position] for position in positions]
print(' '.join(output))
输出:
This is a repeated sentence This is a repeated sentence