python对多个变量循环赋值
按照月份对数据进行分类:有12个list:a1,a2,...,a12,有数据集合data=[1,2,3,4,3,4,...,5,6,7,8],data中的元素等于几,就放...
按照月份对数据进行分类:有12个list :a1, a2, ... , a12, 有数据集合 data = [1,2,3,4,3,4,...,5,6,7,8 ] ,data 中的元素等于几,就放进相应的list a几 中,如何循环实现,不用一个一个If,12个if。
展开
2个回答
展开全部
先将 a1,a2 .. a12 放进一个以月份为key的字典中,然后通过循环data 放进相应的 list 中,例如下面代码:
#coding=utf-8
# 定义 12 个 List
a1 = []
a2 = []
a3 = []
a4 = []
a5 = []
a6 = []
a7 = []
a8 = []
a9 = []
a10 = []
a11 = []
a12 = []
# 将 12 个 List 放进一个字典
a = { 1: a1, 2: a2, 3: a3, 4: a4, 5: a5, 6: a6, 7: a7, 8: a8, 9: a9, 10: a10, 11: a11, 12: a12 }
data = [1,2,3,4,3,4,12,11,9,7,5,6,7,8 ]
for item in data:
a[item].append(item)
print a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询