如何将打印循环添加到一起形成带空格的字符串?
positions = [1, 2, 3, 4, 1, 2, 5, 6, 7, 2, 8, 6, 3, 9, 10]
words = ['this', 'is', 'not', 'howr', 'needed', 'and', 'it', 'wrong', 'right', ':(']
poswords = dict(zip(positions, words))
print(poswords, words)
for i in positions:
print(poswords[i]," ",)
当我只想将打印件保存在字符串
中时答案 0 :(得分:1)
sentence = " ".join([words[i-1] for i in positions]) #?
的产率:
'这不是需要的,这是错误的,不对的:('
答案 1 :(得分:0)
@ AShelly的答案是理想的解决方案,但如果您更喜欢使用循环,那么您可以使用以下内容:
positions = [1, 2, 3, 4, 1, 2, 5, 6, 7, 2, 8, 6, 3, 9, 10]
words = ['this', 'is', 'not', 'howr', 'needed', 'and', 'it', 'wrong', 'right', ':(']
for i in positions:
print(words[i-1], end = ' ')
打印的命名参数(end = ' '
)表示print
语句将以' '
而不是通常的'\n'
结尾。