1

我是新手,所以我的标题可能具有误导性和/或没有意义。

我有 32 个对象我想用循环初始化。我在一个列表中有所需的对象名称,称为 A。在一个列表中,所需的值称为 B。

我想做类似的事情:

for(a,b) in zip(A,B):
a = b

显然这是行不通的,因为它只会改变列表 A 的内容。

为清楚起见,如果列表 A 定义如下:

A = [
'Game'
'Developer'
]

和 B 喜欢:

B = [
'Super Mario 64'
'Nintendo'
]

I would want a loop that would execute the code:

Game = 'Super Mario 64'
Developer = 'Nintendo'

我该怎么做呢?

4

1 回答 1

-3

你可以用它globals()来做到这一点。

A = [
    'Game',
    'Developer'
]

B = [
    'Super Mario 64',
    'Nintendo'
]

for(a, b) in zip(A, B):
    globals()[a] = b

print(Game)
print(Developer)
Super Mario 64
Nintendo
于 2020-05-18T20:58:59.683 回答