[英]Copy all string outputs from a loop (not in a list)
我有以下示例代碼:
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
我嘗試將文本附加到列表中,但無法獲得所需的相同 output。
我不想獲取循環的最后一個 output,而是想獲取以下 output:
Hi 1
Hello 1
Hi 2
Hello 2
Hi 3
Hello 3
試試這個,
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̵
或者試試這個
copy("\n".join(f'Hi {i}\nHello {i}' for i in [1,2,3]))
試試這個。
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
它應該給你你想要的 output。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.