一.zip函数:接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表。

1.示例1:


python dict保存到文件 python中dict(zip)_python dict保存到文件



x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zip(x, y, z) print xyz



python dict保存到文件 python中dict(zip)_python dict保存到文件


运行的结果是:

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

从这个结果可以看出zip函数的基本运作方式。

2.示例2:


x = [1, 2, 3] y = [4, 5, 6, 7] xy = zip(x, y) print xy


运行的结果是:

[(1, 4), (2, 5), (3, 6)]

从这个结果可以看出zip函数的长度处理方式。

3.示例3:


x = [1, 2, 3] x = zip(x) print x


运行的结果是:

[(1,), (2,), (3,)]

从这个结果可以看出zip函数在只有一个参数时运作的方式。

4.示例4:


x = zip() print x


运行的结果是:

[]

从这个结果可以看出zip函数在没有参数时运作的方式。

5.示例5:


python dict保存到文件 python中dict(zip)_python dict保存到文件



x = [1, 2, 3] y = [4, 5, 6] z = [7, 8, 9] xyz = zip(x, y, z) u = zip(*xyz) print u



python dict保存到文件 python中dict(zip)_python dict保存到文件


运行的结果是:

[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

一般认为这是一个unzip的过程,它的运行机制是这样的:

在运行zip(*xyz)之前,xyz的值是:[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

那么,zip(*xyz) 等价于 zip((1, 4, 7), (2, 5, 8), (3, 6, 9))

所以,运行结果是:[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

注:在函数调用中使用*list/tuple的方式表示将list/tuple分开,作为位置参数传递给对应函数(前提是对应函数支持不定个数的位置参数)

6.示例6:


x = [1, 2, 3] r = zip(* [x] * 3) print r


运行的结果是:

[(1, 1, 1), (2, 2, 2), (3, 3, 3)]

它的运行机制是这样的:

[x]生成一个列表的列表,它只有一个元素x

[x] * 3生成一个列表的列表,它有3个元素,[x, x, x]

zip(* [x] * 3)的意思就明确了,zip(x, x, x)

 

二、dict操作

1、使用zip创建字典

= ‘abcde‘

value = range(1, 6)

dict(zip(key, value))

2、使用items()来遍历字典

for key,value in d.items():

3.使用get, pop来获取/删除key

首先,dict[key] 与 delete dict[key]也可以获取/删除key。但是key不存在时,会引发 KeyError

  • get(key[, default])
    如果key在字典中,返回对应的value, 否则返回default。所以从来不会引发异常。
  • pop(key[, default])
    如果default未设置,则如果key不在字典中,删除key将引发异常。使用时加上default。

4、dict(dict1, **dict2)合并两个字典

合并两个字典可以先把两个字典分成key-value对,再连接两个key-value对,生成新的字典。即 dict(dict1.items()+dict2.items()) ,不过效率有些低。

使用效率更高的 dict(dict1, **dict2)

In [29]: dict1
Out[29]: {‘a‘: 1, ‘b‘: 2, ‘c‘: 3} In [30]: dict2 Out[30]: {‘d‘: 4, ‘e‘: 5, ‘f‘: 6} In [31]: dict(dict1, **dict2) Out[31]: {‘a‘: 1, ‘b‘: 2, ‘c‘: 3, ‘d‘: 4, ‘e‘: 5, ‘f‘: 6}
In [29]: dict1
Out[29]: {‘a‘: 1, ‘b‘: 2, ‘c‘: 3} In [30]: dict2 Out[30]: {‘d‘: 4, ‘e‘: 5, ‘f‘: 6} In [31]: dict(dict1, **dict2) Out[31]: {‘a‘: 1, ‘b‘: 2, ‘c‘: 3, ‘d‘: 4, ‘e‘: 5, ‘f‘: 6}


$ python -m timeit -s ‘dict1=dict2=dict(a=1,b=2)‘ ‘dict3=dict(dict1,**dict2)‘ 1000000 loops, best of 3: 0.573 usec per loop $ python -m timeit -s ‘dict1=dict2=dict(a=1,b=2)‘ ‘dict3=dict(dict1.items()+dict2.items())‘ 100000 loops, best of 3: 2.21 usec per loop
$ python -m timeit -s ‘dict1=dict2=dict(a=1,b=2)‘ ‘dict3=dict(dict1,**dict2)‘ 1000000 loops, best of 3: 0.573 usec per loop $ python -m timeit -s ‘dict1=dict2=dict(a=1,b=2)‘ ‘dict3=dict(dict1.items()+dict2.items())‘ 100000 loops, best of 3: 2.21 usec per loop


5、使用keys()和values()获取关键词和值

d.keys()

d.values()

6、使用update()更新字典,与4的dict(dict1, **dict2)效果一样




python dict保存到文件 python中dict(zip)_python_05

1 dict1 = {"a" : "apple", "b" : "banana"}
2 print(dict1)
3 dict2 = {"c" : "grape", "d" : "orange"}
4 dict1.update(dict2)
5 print(dict1)


View Code

输出:

{‘a‘: ‘apple‘, ‘b‘: ‘banana‘}
{‘c‘: ‘grape‘, ‘a‘: ‘apple‘, ‘b‘: ‘banana‘, ‘d‘: ‘orange‘}

7、使用sorted排序

#按照key排序
print(sorted(dict1.items(), key=lambda d: d[0]))
#按照value排序
print(sorted(dict1.items(), key=lambda d: d[1]))

8、使用setdefault设置默认值

dict = {}
dict.setdefault("a")

9、pop()和clear()删除




python dict保存到文件 python中dict(zip)_python_05

1 d={"a":1,"b":2,"c":3}
 2 print(d)
 3 #输出
 4 #{"a":1,"b":2,"c":3}
 5 
 6 d1=d.pop("a") #d1=1
 7 print(d)
 8 #输出
 9 #{"b":2,"c":3}
10 
11 d2=d.pop("f",100) #d2=100
12 print(d)
13 #输出
14 #{"b":2,"c":3}


View Code

d.clear() #将字典d清空

print(d)  #输出{}

10、使用fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。




python dict保存到文件 python中dict(zip)_python_05

1 seq={‘name‘,‘age‘,‘score‘}
2 d=dict.fromkeys(seq,100)
3 print(d)
4 #输出
5 #{‘name‘: 100, ‘age‘: 100, ‘score‘: 100}


View Code


11、使用popitems()随机返回并删除字典中的一对键和值(项)




python dict保存到文件 python中dict(zip)_python_05

1 seq={‘name‘,‘age‘,‘score‘}
2 d=dict.fromkeys(seq,100)
3 print(d)  #输出:{‘age‘: 100, ‘name‘: 100, ‘score‘: 100}
4 d2=d.popitem()
5 print(d2) #输出:(‘age‘, 100)
6 print(d)  #输出:{‘name‘: 100, ‘score‘: 100}