使用 for 循环将 i 插入变量名?
Using for-loop to insert i into variable names?
我正在尝试使用 for 循环创建大量绘图。长话短说,我需要创建一个脚本来执行类似于此示例的操作:
text1="Hello"
text2="my"
text3="name"
text4="is"
text5="John"
for i in range(0,6):
print(text{i})
这将输出以下内容:
#Output:
'Hello'
'my'
'name'
'is'
'John'
这在 Python 中可行吗?
谢谢!
您需要一个列表,而不是一堆名称相似的变量。
texts = ["Hello", "my", "name", "is", "John"]
for t in texts:
print(t)
我正在尝试使用 for 循环创建大量绘图。长话短说,我需要创建一个脚本来执行类似于此示例的操作:
text1="Hello"
text2="my"
text3="name"
text4="is"
text5="John"
for i in range(0,6):
print(text{i})
这将输出以下内容:
#Output:
'Hello'
'my'
'name'
'is'
'John'
这在 Python 中可行吗? 谢谢!
您需要一个列表,而不是一堆名称相似的变量。
texts = ["Hello", "my", "name", "is", "John"]
for t in texts:
print(t)