Python_Creating空列表和迭代器命名

时间:2017-12-24 00:12:41

标签: python list

我想创建一堆空列表并通过迭代命名它们,但代码会出现错误显示"语法错误:无法分配给运算符"。 我的目的是创建名为5R05D,15R05D,15R20D等的列表。 感谢任何帮助!

MA = ['5','15','30','60']
For each in MA:
    each+'R05D'=[]
    each+'R10D'=[]
    each+'R20D'=[]

1 个答案:

答案 0 :(得分:2)

您无法创建类似的变量。首先,Python变量名称不能以数字开头。使用'5R05D'(和类似)作为键的字典会更好,并列为值:

MA = ['5','15','30','60']
d = {}
for each in MA:
    d[each+'R05D'] = []
    d[each+'R10D'] = []
    d[each+'R20D'] = []

>>> d
{'5R05D': [], '5R10D': [], '5R20D': [], '15R05D': [], '15R10D': [], '15R20D': [], '30R05D': [], '30R10D': [], '30R20D': [], '60R05D': [], '60R10D': [], '60R20D': []}