Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用变量字符串命名另一个变量(Python)_Python - Fatal编程技术网

使用变量字符串命名另一个变量(Python)

使用变量字符串命名另一个变量(Python),python,Python,我有一个列表,我想在命名对象时使用其中的信息。 例如: print this_device # ex. output ['dev879:', 'drain', '7.474', '11.163', '18.637'] print this_device # ex. ['dev879:', 'drain', '7.474', '11.163', '18.637'] Drive_DeviceName_ = this_device[0] # which is 'dev879' Drivedev8

我有一个列表,我想在命名对象时使用其中的信息。 例如:

print this_device # ex. output ['dev879:', 'drain', '7.474', '11.163', '18.637']
print this_device # ex. ['dev879:', 'drain', '7.474', '11.163', '18.637']

Drive_DeviceName_ = this_device[0] # which is 'dev879'

Drivedev879 = ReadableDevice(this_device[0], this_device[1], this_device[2])
我想做的是使用此_设备[0]作为下一个用于创建对象的变量的名称

例如:

print this_device # ex. output ['dev879:', 'drain', '7.474', '11.163', '18.637']
print this_device # ex. ['dev879:', 'drain', '7.474', '11.163', '18.637']

Drive_DeviceName_ = this_device[0] # which is 'dev879'

Drivedev879 = ReadableDevice(this_device[0], this_device[1], this_device[2])

请让我知道这是否合理。

据我所知,这是不可能的。您可以做的是从列表中创建一个字典,并使用您想要的名称作为键

devices = {}
devices['name'] = this_device[0]
etc...
通过从原始列表创建属性名称列表,可以更快地完成此操作。我假设您提供的列表是一个属性列表

props = ['name', 'prop_1', 'prop_2', ...]
this_device = ['dev879:', 'drain', '7.474', '11.163', '18.637']
device = dict(zip(props, this_device))
将导致:

{'name': 'dev870', 'prop_1': 'drain', ..}

如果我明白了,你想做什么,你可以使用globals:

globals()['b'] = 3
# Now there is a variable named b
print b

请注意,您的一些姓名中有:个。这使它们成为非法的变量名。但是,您应该仍然能够操纵globals来完成这项工作,这不是您真正想要做的。这听起来像是个XY问题,让我改用字典。