#把一个数列进行划分,划分出来的结果生成另外几个数列,再把生成的每个数列座位字典里面的value
#第一种实现方法:
vv = {}
v1 = []
v2 = []
#for 变量 该变量就是 in里面的值
number_list = [12,5,3,4,9,7,54,26,18,14,13]
for i in number_list: # for i in range(11)
if i > 7: # if number[i] > 7:
print(i) # print(number[i])
v1.append(i) # v1.append(number[i])
vv['k1'] = v1
else:
print(i) # print(number[i])
v2.append(i) # v2.append(number[i])
vv['k2'] = v2
print(vv['k1'])
print(vv['k2'])
print(vv.keys())
print(vv.values())
print('*'*16)
#第二种实现方法:
#先不定义新数列,先进行判刑是否有数列没有,没有就把带有 初值的数列
# 赋给vv['k1']/vv['k2'],使得这两个变成新的数列,之后再用append方法在数列中增加值
vv = {}
number_list = [12,5,3,4,9,7,54,26,18,14,13]
for k in number_list:
if k > 7:
if 'k1' in vv.keys():
vv['k1'].append(k)
else:
vv['k1'] = [k, ]
else:
if 'k2' in vv.keys():
vv['k2'].append(k)
else:
vv['k2'] = [k, ]
print(vv['k1'])
print(vv['k2'])
print(vv.keys())
print(vv.values())
#第三种方法,用默认字典提前申明好values值是一个数列,
在后面分类好的number添加到数列时直接可以用append方法加入到value数列中
import collections
from collections import defaultdict
dic = defaultdict(list)
number_list = [12,5,3,4,9,7,54,26,18,14,13]
for j in number_list:
if j > 7:
dic['k1'].append(j)
else:
dic['k2'].append(j)
print(dic['k1'])
print(dic['k2'])
print(dic)