【Python练习】分类数据

将11,22,33,44,55,77,66,88,99,9 进行分类,以66为界限
代码1:
```
list1 =[11,22,33,44,55,77,66,88,99,91]
print(list1)
list2 =[]
list3 =[]
for i in list1:
if i<66:
print(i)
list2.append(i)
else:
list3.append(i)

dict1 = {'k1':list2,'k2':list3}

print(dict1['k1'])
print(dict1['k2'])
```
优化后代码:
```
list1 =[11,22,33,44,55,77,66,88,99,91]
dict1={}
for i in list1:
if i>66:
if 'k1' in dict1.keys():
dict1['k1'].append(i)
else:
dict1['k1']=[i,]
else:
if 'k2' in dict1.keys():
dict1['k2'].append(i)
else:
dict1['k2'] = [i,]
print("小于66的数值:{0}".format(dict1['k1']))
print(dict1['k2'])
```




posted @ 2016-11-13 21:00  StellariLee  阅读(187)  评论(0编辑  收藏  举报