我正在寻找一种更好的方法来创建变量并将其赋值给变量, 这是一个更大的函数“ get_info”的一部分,exec运行良好,直到我添加了更多条件,这是我第二次不得不重写它,我想找到一种更好的方法这样,我就不必使用exec。
if (a != b):
info= data.info
if info:
if status == 10:
number_of_people = [i.person for i in entries if "OK" in i.what]
dates = [str(i.when) for i in info if "OK" in i.what]
dates =[time.strftime('%Y-%m-%d', time.localtime(int(i[:-3]))) for i in dates]
for i in range(len(number_of_people)):
exec('dude_{} = number_of_people[i]').format(i+1)
exec('dude_date_{} = dates[i]').format(i+1)
继续抛出此错误:
SyntaxError: unqualified exec is not allowed in function 'get_info' it contains a nested function with free variables
Expected Output:
dude_1 = "Harry"
dude_date_1= "2019-02-16"
dude_2 = "Joe"
dude_date_2= "2018-02-01" and so on..