我有以下列表清单:
sims1 = [[(2, 0.90452874), (1, 0.83522302), (4, 0.77591574), (0, 0.72705799), (3, 0.52282226)],
[(3, 0.79298556), (1, 0.78112978), (2, 0.76006395), (0, 0.58570701), (4, 0.40093967)],
[(2, 0.9549554), (1, 0.71705657), (0, 0.58731651), (3, 0.43987277), (4, 0.38266104)],
[(2, 0.96805269), (4, 0.68034023), (1, 0.66391909), (0, 0.64251828), (3, 0.50730866)],
[(2, 0.84748113), (4, 0.8338449), (1, 0.61795002), (0, 0.60271078), (3, 0.20899911)]]
我想根据这些字符串命名列表中的每个列表:url = [‘a’,’b’,’c’,’d’].所以,例如,
>>> a
[(2, 0.90452874), (1, 0.83522302), (4, 0.77591574), (0, 0.72705799), (3, 0.52282226)]
>>> b
[(3, 0.79298556), (1, 0.78112978), (2, 0.76006395), (0, 0.58570701), (4, 0.40093967)]
那么如何在Python中实现这一目标?
最佳答案 你可以使用字典:
names = ['a', 'b', 'c', 'd', 'e']
named_sims1 = dict(zip(names, sims1))
print(named_sims1['a'])
如果要将变量替换为x.a或x [‘a’],则可以使用:
exec(",".join(names) + ' = ' + str(sims1)) # ensure same length
但我不确定,如果这是推荐的话.