Merge pull request #10 from zhisheng17/master

update python_base.py
master
xianhu 2016-11-08 11:33:41 +08:00 committed by GitHub
commit dca203b811
1 changed files with 5 additions and 5 deletions

View File

@ -254,7 +254,7 @@
#-- 常用字典常量和操作
D = {}
D = {'spam':2, 'tol':{'ham':1}} # 嵌套字典
D = dict.fromkeys(['s', 'd'], 8) # {'d': 8, 's': 8}
D = dict.fromkeys(['s', 'd'], 8) # {'s': 8, 'd': 8}
D = dict(name = 'tom', age = 12) # {'age': 12, 'name': 'tom'}
D = dict([('name', 'tom'), ('age', 12)]) # {'age': 12, 'name': 'tom'}
D = dict(zip(['name', 'age'], ['tom', 12])) # {'age': 12, 'name': 'tom'}
@ -270,8 +270,8 @@
# 字典注意事项1对新索引赋值会添加一项2字典键不一定非得是字符串也可以为任何的不可变对象
#-- 字典解析
D = {k:8 for k in ['s', 'd']} # {'d': 8, 's': 8}
D = {k:v for (k, v) in zip(['name', 'age'], ['tom', 12])}
D = {k:8 for k in ['s', 'd']} # {'s': 8, 'd': 8}
D = {k:v for (k, v) in zip(['name', 'age'], ['tom', 12])} # {'age': 12, 'name': tom}
#-- 字典的特殊方法__missing__当查找找不到key时会执行该方法
class Dict(dict):
@ -335,8 +335,8 @@
#-- 序列赋值 序列解包
[a, b, c] = (1, 2, 3) # a = 1, b = 2, c = 3
a, b, c, d = "spam" # a = 's', b = 'p'
a, b, c = range(3) # a = 0, b = 1
a, b, c, d = "spam" # a = 's', b = 'p', c = 'a', d = 'm'
a, b, c = range(3) # a = 0, b = 1, c = 2
a, *b = [1, 2, 3, 4] # a = 1, b = [2, 3, 4]
*a, b = [1, 2, 3, 4] # a = [1, 2, 3], b = 4
a, *b, c = [1, 2, 3, 4] # a = 1, b = [2, 3], c = 4