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




 












原文地址:https://www.cnblogs.com/hanxing/p/4313765.html