2014-02-22 116 views
0

我需要'person%s' % x等於'textvar%s' % x,但我不想將'person%s' % x設置爲people[int('%s' % x)]如何用變量%i創建變量?

global people 
people = ['Zach : 10', 'Danielle : 10'] 

global num 
num = len(people) 

for x in range(0, numDebt): 
    globals()['textvar%s' % x] = people[int('%s' % x)] 
    globals()['person%s' % x] = textvar%i % x 
+7

請[將您的數據保留在變量之外](http://nedbatchelder.com/blog/201112/keep_data_out_of_your_variable_names.html);不要在*字典*中產生一系列'textvar'和'person'名字。 –

+3

'x' *已經*爲一個整數,所以'int('%s'%x)'是一個非常*迂迴的只是說'x'的方式。 –

回答