Python随笔(三)、python基础

时间:2021-01-07 18:39:42

一、练习:

#!usr/bin/env python 
#-*- coding:utf-8 _*-  
"""
@author:Administrator
@file: dictionary.py
@time: 2017/11/19
"""
'''
有如下集合[11,22,33,44,55,66,77,88,99,110,121,132,143,154,165,176,187],将所有大于66的值保存在字典的第一个key中,将小于66的值保存在第二个key的值中
即:{'k1':大于66,'k2':小于66}
'''
######方法一
#dic = {}
#all_list = [11,22,33,44,55,66,77,88,99,110,121,132,143,154,165,176,187]
#for i in all_list:
#    if i > 66:
#        if "k1" in dic.keys():
#            dic["k1"].append(i)
#        else:
#            dic['k1'] = [i,]
#    else:
#        if "k2" in dic.keys():
#            dic["k2"].append(i)
#        else:
#            dic['k2'] = [i,]
#print(dic['k1'])
#print(dic['k2'])

#方法二、
all_list = [11,22,33,44,55,66,77,88,99,110,121,132,143,154,165,176,187]
dic = {'k1':[],'k2':[]}
for item in all_list:
   
if item>66:
       
dic["k1"].append(item)
   else:
       
dic["k2"].append(item)
print(dic['k1'])
print(dic['k2'])


本文出自 “linux” 博客,转载请与作者联系!