如何将print循环函数从字典转换为最终字符串

2024-09-30 06:20:25 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使打印循环加在一起形成一个带空格的字符串?你知道吗

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]," ",)

当我只是想把指纹保存在一个字符串中


Tags: and字符串rightisnotitthiswords
2条回答
sentence =  " ".join([words[i-1] for i in positions]) #?

收益率:

'this is not howr this is needed and it is wrong and not right :('

@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 = ' ')

print(end = ' ')的命名参数意味着print语句将以' '结束,而不是通常的'\n'。你知道吗

相关问题 更多 >

    热门问题