码迷,mamicode.com
首页 > 编程语言 > 详细

Python随笔(三)、python基础

时间:2017-11-19 18:43:31      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:tuple list dict

一、练习:

#!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” 博客,转载请与作者联系!

Python随笔(三)、python基础

标签:tuple list dict

原文地址:http://chenjisong.blog.51cto.com/7858025/1983304

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!