2

我是 Python 新手,所以提前道歉。这是我遇到问题的代码:

x = 1

v1 = "First"
v2 = "Second"
v3 = "Third"
v4 = "Forth"
v5 = "Fifth"
v6 = "Sixth"
v7 = "Seventh"
v8 ="Eighth"
v9 = "Ninth"
v10 = "Tenth"

for x in range (1,11):
    print("v"+str(x))

这是返回的结果:

v1
v2
v3
v4
v5
v6
v7
v8
v9
v10

这就是我希望返回的结果是:

First
Second
Third
Fourth
Fifth
Sixth
Seventh
Eighth
Ninth
Tenth

我如何打印越来越多的命名变量的内容?

4

9 回答 9

3
for i in range(1, 11):
    print(locals()['v' + str(i)])

顺便说一句,这很糟糕。了解字典

于 2018-07-11T11:15:14.320 回答
3

尝试这个。只需将字符串存储在列表中并打印:

x = 1
store_1 = ["First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth","Tenth"] 
for x in store_1:
    print(x)
于 2018-07-11T11:16:43.850 回答
2

我建议不要将每个值存储在它自己的变量中,而是将所有值存储在 a 中list

存储在列表中 -

v = ["First", "Second", "Third"...] 

要访问此列表中的值,您将使用与此类似的语法(请注意,在 python 中,列表从索引零开始):

str = v[0]
# str will now be equal to "First"

现在,当您使用 时range,您将生成一个可用作列表索引的数字序列:

for x in range (0,11):
    print(v[x])

所以这个循环将评估打印命令,例如:

print(v[0]) # "First"
print(v[1]) # "Second"
print(v[2]) # "Third"
...

访问特定索引处的列表将返回存储在同一索引处的值。


请注意,我已更改range()为从零开始,因为如果您从零开始,1您将跳过索引为零的第一个元素。此外,可以通过使用列表的长度作为"end"范围的索引来进一步改进此代码:

v = ["First", "Second", "Third"...] 
num_items = len(v)
for x in range(0, num_items):
    print(v[x])

使用这种方法,您的v列表可以有不同的大小,并且您不需要对循环进行任何更改。


最后要感谢一位乐于助人的评论者: range 函数实际上使用零作为默认的第一个值,因此可以进一步简化循环:

for x in range(num_items):
    print(v[x])
于 2018-07-11T11:20:22.610 回答
1

Eval is also an option here

for i in range (10):
    print(eval('v{}'.format(i+1)))
于 2018-07-11T11:24:10.690 回答
1

保持简单,创​​建列表并使用循环进行迭代。

Numbers = ["First", "Second","Third","Forth", "Fifth", "Sixth","Seventh","Eighth", "Ninth", "Tenth"]

for number in Numbers:
    print number

输出:

第一 第二 第三 第四 第五 第六 第七 第八 第九 第十

于 2018-07-11T11:30:08.770 回答
1

您可以选择使用马可指出的当地人,也可以使用eval-

for i in range(1, 11):
    print eval("v{}".format(i))

但是解决这个问题更好的方法是使用列表或字典

于 2018-07-11T11:20:42.420 回答
0

您可以使用globals()来访问全局变量。

x = 1

v1 = "First"
v2 = "Second"
v3 = "Third"
v4 = "Forth"
v5 = "Fifth"
v6 = "Sixth"
v7 = "Seventh"
v8 ="Eighth"
v9 = "Ninth"
v10 = "Tenth"

for x in range (1,11):
    print(globals()["v"+str(x)])

这输出:

First
Second
Third
Forth
Fifth
Sixth
Seventh
Eighth
Ninth
Tenth
于 2018-07-11T11:15:59.147 回答
0

使用列表:

v1 = "First"
v2 = "Second"
v3 = "Third"
v4 = "Forth"
v5 = "Fifth"
v6 = "Sixth"
v7 = "Seventh"
v8 ="Eighth"
v9 = "Ninth"
v10 = "Tenth"
for x in [v1,v2,v3,v4,v5v6,v7,v8,v9,v10]:
    print(x)
于 2018-07-11T11:17:04.917 回答
0

您还可以将项目作为键值对存储在字典中:

lst = ["First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth","Tenth"]

d = {"v" + str(i+1): e for i, e in enumerate(lst)}
# {'v1': 'First', 'v2': 'Second', 'v3': 'Third', 'v4': 'Fourth', 'v5': 'Fifth', 'v6': 'Sixth', 'v7': 'Seventh', 'v8': 'Eighth', 'v9': 'Ninth', 'v10': 'Tenth'}

# print key -> value pairs
for v in d:
    print(v, "->", d[v])

这给出了以下映射:

v1 -> First
v2 -> Second
v3 -> Third
v4 -> Fourth
v5 -> Fifth
v6 -> Sixth
v7 -> Seventh
v8 -> Eighth
v9 -> Ninth
v10 -> Tenth
于 2018-07-11T11:24:38.593 回答