dic
内置函数
map(f,list) f接收一个参数
def format_name(s):
return s[0].upper() + s[1:].lower()
reduce(f,list)f函数接收2个参数 如果接收3个参数第三个做初始值
def prod(x, y):
return x * y
filter(f,list) f判断函数 返回符合判断的list元素
def is_odd(x):
return x % 2 == 1
sorted() 传入两个待比较的元素 x, y,如果 x 应该排在 y 的前面,返回 -1,如果 x 应该排在 y 的后面,返回 1。如果 x 和 y 相等,返回 0。
def format_name(s):
return s[0].upper() + s[1:].lower() 首字母大写 后面的小写
L= [1,2,3]
print sum(L) 求和函数
return math.sqrt(x) == int(math.sqrt(x)) 开平方和取整
dict的作用是建立一组 key 和一组 value 的映射关系,dict的key是不能重复的
一是先判断一下 key 是否存在,用 in 操作符:
if 'Paul' in d:
print d['Paul']
如果 'Paul' 不存在,if语句判断为False,自然不会执行 print d['Paul'] ,从而避免了错误。
二是使用dict本身提供的一个 get 方法,在Key不存在的时候,返回None:
>>> print d.get('Bart')
59
>>> print d.get('Paul')
None
列表 L = [1,2,3]
元组 t = (1,2,[1,2,3])
字典 d = {key1:value1, key2:value2} d1 = {"1":"one", "2":"two"}
L= []
L.append ---向列表尾添加项value
L.insert insert(i, value) ---向列表i位置插入项vlaue,如果没有i,则添加到列表尾部
L.extend 表尾添加一个list
L.pop 按索引删除 L.remove 按值删除 L.index 某个值的索引号是多少
L.count 某个值出现的次数 t = ()
t.count(value)
t.index(value) d = {}
d.clear()
d2 = d.copy()
l = [1, 2, 3]
t = (1, 2, 3)
d3 = {}.fromkeys(l) 创建并返回一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值(默认为None) print d3 #{1: None, 2: None, 3: None}
d4 = {}.fromkeys(t, "default")
print d4 #{1: 'default', 2: 'default', 3: 'default'}
d3 = {}.fromkeys(l,t) print d3 #{1: (11, 22, 33), 2: (11, 22, 33), 3: (11, 22, 33)} d.get(key) 结果是value 返回字典dict中键key对应值,如果字典中不存在此键,则返回default 的值(default默认值为None)
d6 = {1:"one", 2:"two", 3:"three"}
print d5.get(1) #one
print d5.get(1,"zzx") #one
print d5.get(5) #None
print d5.get(5, "test") #test
has_key(key) ---判断字典中是否有键key
d6 = {1:"one", 2:"two", 3:"three"}
print d6.has_key(1) #True
print d6.has_key(5) #False
items() ---返回一个包含字典中(键, 值)对元组的列表

d7 = {1:"one", 2:"two", 3:"three"}
for item in d7.items():
print item
#(1, 'one')
#(2, 'two')
#(3, 'three')
for key, value in d7.items():
print "%s -- %s" % (key, value)
#1 -- one
#2 -- two
#3 -- three

keys() ---返回一个包含字典中所有键的列表

d8 = {1:"one", 2:"two", 3:"three"}
for key in d8.keys():
print key
#1
#2
#3

values() ---返回一个包含字典中所有值的列表

d8 = {1:"one", 2:"two", 3:"three"}
for value in d8.values():
print value
#one
#two
#three

pop(key, [default]) ---若字典中key键存在,删除并返回dict[key],若不存在,且未给出default值,引发KeyError异常

d9 = {1:"one", 2:"two", 3:"three"}
print d9.pop(1) #one
print d9 #{2: 'two', 3: 'three'}
print d9.pop(5, None) #None
try:
d9.pop(5) # raise KeyError
except KeyError, ke:
print "KeyError:", ke #KeyError:5

popitem() ---删除任意键值对,并返回该键值对,如果字典为空,则产生异常KeyError
d10 = {1:"one", 2:"two", 3:"three"}
print d10.popitem() #(1, 'one')
print d10 #{2: 'two', 3: 'three'}
setdefault(key,[default]) ---若字典中有key,则返回vlaue值,若没有key,则加上该key,值为default,默认None

d = {1:"one", 2:"two", 3:"three"}
print d.setdefault(1) #one
print d.setdefault(5) #None
print d #{1: 'one', 2: 'two', 3: 'three', 5: None}
print d.setdefault(6, "six") #six
print d #{1: 'one', 2: 'two', 3: 'three', 5: None, 6: 'six'}

update(dict2) ---把dict2的元素加入到dict中去,键字重复时会覆盖dict中的键值
d = {1:"one", 2:"two", 3:"three"}
d2 = {1:"first", 4:"forth"}
d.update(d2)
print d #{1: 'first', 2: 'two', 3: 'three', 4: 'forth'}
viewitems() ---返回一个view对象,(key, value)pair的列表,类似于视图。优点是,如果字典发生变化,view会同步发生变化。在
迭代过程中,字典不允许改变,否则会报异常
d = {1:"one", 2:"two", 3:"three"}
for key, value in d.viewitems():
print "%s - %s" % (key, value)
#1 - one
#2 - two
#3 - three
viewkeys() ---返回一个view对象,key的列表
d = {1:"one", 2:"two", 3:"three"}
for key in d.viewkeys():
print key
#1
#2
#3
viewvalues() ---返回一个view对象,value的列表
d = {1:"one", 2:"two", 3:"three"}
for value in d.viewvalues():
print value
#one
#two
#three
dic的更多相关文章
- python征程3.1(列表,迭代,函数,dic,set,的简单应用)
1.列表的切片. 1.对list进行切片.'''name=["wangshuai","wangchuan","wangjingliang", ...
- Python_Day_03 list,dic,tuple方法总结
编程语言中最长见的几种数据类型,字典,列表,等.同样在Python中也有这些数据类型,只是有些表现形式不同.同时在Python中又多了一种叫做元组(tuple)的东西. list(列表) 初始化列表 ...
- iOS不使用JSONKit做Dic到JsonString的转换
NSDictionary to jsonString [self DataTOjsonString:dic] -(NSString*)DicToJsonString:(id)object { NSSt ...
- iOS -- 给model赋值时走了[self setValuesForKeysWithDictionary:dic]不走setvalue: forked:
这是一个小坑, 看看你的BaseModel的便利构造器的方法: + (__kindof BaseModel *)modelWithDic:(NSDictionary *)dic { return [[ ...
- Python中的List,Tuple,Dic,Set
Python中的List,Tuple,Dic,Set List定义 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 ...
- Ansj分词双数组Trie树实现与arrays.dic词典格式
http://www.hankcs.com/nlp/ansj-word-pairs-array-tire-tree-achieved-with-arrays-dic-dictionary-format ...
- hashTabel List 和 dic
hashTabel List 和 dic 原:https://www.cnblogs.com/jilodream/p/4219840.html .Net 中HashTable,HashMap 和 ...
- python dic字典使用
#!/usr/bin/env python -*-''' 字典的基本组成及用法: dict={key:value} dict[key]=value 字典是无序的. key值是唯一属性,一对一,几个ke ...
- 批量操作RunTime之获取的Dic换成Model
方法一: // // AlinkDeviceInfo.m //// // Created by Vivien on 2018/10/12. // Copyright © 2018年 . All rig ...
- PaodingAnalysis 提示 "dic home should not be a file, but a directory"
Exception in thread "main" net.paoding.analysis.exception.PaodingAnalysisException: dic ho ...
随机推荐
- git下载安装、配置及idea初始化
安装 wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.19.0.tar.gz git 安装依赖 yum -y insta ...
- python重要函数eval
1.参数会作为一个 Python 表达式(从技术上说是一个条件列表)被解析并求值 >>> x = 1 >>> eval('x+1') 2 2.去除字符串两边的引号 ...
- angularJS MVC及$scope作用域
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-star-empty
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- ACM-寻宝
题目描述:寻宝 有这么一块神奇的矩形土地,为什么神奇呢?因为上面藏有很多的宝藏.该土地由N*M个小正方形土地格子组成,每个小正方形土地格子上,如果标有“E”,则表示该格可以通过:如果标有“X”,则表示 ...
- Spring容器的创建原理
1.new ioc容器(AnnotationConfigApplicationContext 注解ioc) 2.refresh()方法调用 2.1 prepareRefresh()刷新前的预处理 a: ...
- Redis集群环境之linux搭建单机版
Redis解决的问题是:作为一个缓存nosql数据库,能够支持高并发,关系型数据库是存储在磁盘中,通过io读写,而redis是存储在内存中,因此,能够实现高可用,他主要是解决数据库性能瓶颈而产生的. ...
- Spring-IOC(基于注解)
1.Spring 的 Bean 管理:(注解方式) 1.1 创建 web 项目,引入 Spring 的开发包: 注:在 Spring 的注解的 AOP 中需要引入 spring-aop 的 jar 包 ...
- 如何有效避免Essay写作抄袭
每到学期末的时候,各种考试,论文以及作业数不胜数,压得留学党们快要喘不过气了.我想比起写论文,同学们更操心的问题应该是:Plagiarism.要知道在国外Plagiarism的这种行为在学术中是零容忍 ...
- C#的listview
listView1.Items.Clear(); ListViewItem listitem = new ListViewItem(字符串);//这是第一列的内容,需要,而且必须通过构造方法添加 ; ...