[英]Copy all string outputs from a loop (not in a list)
I have the following sample code:我有以下示例代码:
from pyperclip import copy
numbers = [1,2,3]
for i in range (1,len(numbers)):
text1 = 'Hi' + str(i) + '\n'
text2 = 'Hello' + str(i) + '\n'
copy(text1 + text2)
# Hi 3
# Hello 3
I tried appending the text into a list, but I'm not able to get the same output as I want.我尝试将文本附加到列表中,但无法获得所需的相同 output。
Instead of getting the last output of the loop, I want to get the following output:我不想获取循环的最后一个 output,而是想获取以下 output:
Hi 1
Hello 1
Hi 2
Hello 2
Hi 3
Hello 3
Give this a try,试试这个,
from pyperclip import copy
numbers = [1, 2, 3]
tmp = []
for i in range(1, len(numbers)):
text1 = 'Hi' + str(i) + '\n'
text2 = 'Hello' + str(i) + '\n'
tmp.append(text1 + text2) # <-- accumulate all the values
copy("".join(tmp)) # <-- Copy outside of the loop̵
Or give this a try或者试试这个
copy("\n".join(f'Hi {i}\nHello {i}' for i in [1,2,3]))
Try this out.试试这个。
numbers = [1,2,3]
text_array = []
for i in numbers:
text1 = "Hi " + str(i)
text2 = "Hello " + str(i)
text_array.append(text1)
text_array.append(text2)
for i in text_array:
print i
It should give you the output you wanted.它应该给你你想要的 output。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.