SlideShare a Scribd company logo
1 of 11
Download to read offline
变量与数据类型 
变量的声明 
a = 1 
b = -100 
c = 12.3 
d = 341341325234234523433333333333338888888888888L 
e = 1235324523452346 * 999999999999999999999999 
my_name = 'python' 
_my_name = "Life is short, I use Python" 
_my_name2 = 'I'm "OK"!' 
a1,b1,c1 = (1,2,3) 
a2,b2,c2 = [1,2,3] 
对于批量的命名需要注意变量个数和序列个数保存一致,否则会抛出ValueError更多用法 
a,b,c,d,e='hello' ##必须为strings,files,iterators 
_,age,_=['robin',10,('aaa',222,'ccc')] ##只关注age 
或者 
a,b,*c=[1,2,3,4,5] #c=[3,4,5] 
a,*b,c=[1,2,3,4,5] #b=[2,3,4] 
bool 
采用True, False表示 
>>> True 
True 
>>> False 
False 
>>> 3 > 2 
True 
>>> 3 > 5 
False 
布尔运算 
True and True ##True 
True and False ## False 
False and False ## False 
True or True ##True 
True or False ##True 
False or False ##False 
not True ## False 
not False ## True 
数字与运算 
类型 
int 整数,正整数或负整数 type(10)--> <type 'int'> 
long 长整数,无限大小。可以l或L结尾 type(3452345234523452345234) --> <type 'long'> 
float 浮点数 type(2.1)--><type 'float'>, type(32.3e18) --> <type 'float'> 
complex 复数 type(1+2j)--><type 'complex'> 
算术运算 
7 + 2 >> 9 
7 - 2 >> 5 
7 * 2 >> 14 
7 / 2 >> 3 
7 % 2 >> 1
7 % 2 >> 1 
注意/是取整,可以用浮点数除法或from __future__ import division在python3中处理为一样 
7.0 / 2 >> 3.5 
7 / 2.0 >> 3.5 
7. /2 >> 3.5 
或 
>>> from __future__ import division 
>>> 7/2 
3.5 
再介绍一种运算//返回除法的整数部分 
7 // 2 >> 3 
7.0 // 2 >> 3.0 
比较、逻辑、布尔运算 
常用比较运算(==, !=, >, <, >=, <=) 
布尔运算 and, or, not 
>>> 4>3 and 4>5 
False 
>>> 4>3 or 4>5 
True 
>>> not 4>3 
False 
>>> not 4>5 
True 
当然在python中比较并不只是这么简单,下面来个链式比较: 
>>> x = 5 
>>> 1 < x < 10 
True 
>>> x < 10 < x*10 < 100 
True 
这才是你希望的书写方式 
不存在整数溢出问题 
>>>123456789870987654321122343445567678890098*123345566778999009987654333238766544 
15227847719352756287004435258757627686452840919334677759119167301324455281312L 
神奇的* 
>>> a = 'abc' 
>>> a*4 
'abcabcabcabc' 
>>> b = (1,2) 
>>> b*2 
(1, 2, 1, 2) 
>>> c = [1,2] 
>>> c*3 
[1, 2, 1, 2, 1, 2] 
字符串 
申明 
a0 = "Hello" 
a1 = 'world' 
//运算 
a = a0 + a1 
a*2 
//转义或doc方式 
b = "what's your name?"
c = 'what's your name' 
d = """what's your name?""" 
//unicode 
>>> e = u'你好' 
>>> e 
u'xc4xe3xbaxc3' 
字符串连接 
a = "my name is %s, and %d years old"%('abc',1) 
b = "my name is %s"%'abc' 
常用方法 
a = 'Hello World' 
len(a) 
//大小写转换 
a.upper() 
a.lower() 
a.capitalize() 
//下标与数量 
a.index('e') 
a.count('l') 
a.strip(); 
a.rstrip() 
分组、切片与其他 
索引 
name = 'Hello' 
name[0] --> H 
name[-2] --> l 
name[6] -->IndexError 
切片 
name[0:3] --> 'Hel' 
name[3:] --> 'lo' 
name[:] --> 'Hello' 
name[0:3:2] --> 'Hl' 步长为2 
name[::-1] --> 'olleH' 
其他 
list(name) --> ['H', 'e', 'l', 'l', 'o']返回用"".join([]) 
>>> 'a' in name 
False 
>>> 'a' not in name 
True 
函数 
函数定义 
import random 
def guess_a_number(x): 
a = random.randrange(1, 100) 
if x > a: 
print 'Larger' 
elif x == a: 
print 'equal' 
else: 
pass 
几点说明 
用关键字def申明,不使用{}用缩进代替,注意后面的冒号(:) 
注意参数个数和类型
注意参数个数和类型 
pass表示空实现 
是否可以返回多个参数?答案是肯定的 
def return_multi_param(x): 
return x + 1, x + 2 
a,b = return_multi_param(1) 
可变参数 
def foo(name, *args, **kwds): 
print "name= ", name 
print "args= ", args 
print "kwds= ",kwds 
可简单理解*args为可变列表, **kwds为可变字典 
foo("python", "10", "chengdu", a="aaaaaaaaa", b="vvvvvvvvvvvvv") 
## outputs 
name= python 
args= ('10', 'chengdu') 
kwds= {'a': 'aaaaaaaaa', 'b': 'vvvvvvvvvvvvv'} 
默认参数 
def foo(name = 'abc'): 
print "name= ", name 
## outputs 
foo() #name= abc 
foo("lala") #name= lala 
注意如果默认参数是可变参数,那么可能导致和你预期不一样的结果 
def foo(bar=[]): 
bar.append('bar') 
return bar 
foo() >>['bar'] 
foo() >>['bar', 'bar'] 
foo() >>['bar', 'bar', 'bar'] 
这里调用三次后结果并不是预期那样:一个函数参数的默认值,仅仅在该函数定义的时候,被赋值一次。如此,只有当函数foo()第 
一次被定义的时候,才讲参数bar的默认值初始化到它的默认值(即一个空的列表)。当调用foo()的时候(不给参数bar),会继续 
使用bar最早初始化时的那个列表。 当然也有方式来避免这种方式 
def foo(bar=[]): 
if not bar: 
bar = [] 
bar.append('bar') 
return bar 
更多信息见http://blog.jobbole.com/40088/ 
函数作为参数传递 
def get_text(name): 
return "lorem ipsum, {0} dolor sit amet".format(name) 
def p_decorate(func): 
def func_wrapper(name): 
return "<p>{0}</p>".format(func(name)) 
return func_wrapper 
my_get_text = p_decorate(get_text) 
## outputs 
<p>lorem ipsum, John dolor sit amet</p> 
这也可以用语法糖@来修饰 
@p_decorate 
def get_text(name): 
return "lorem ipsum, {0} dolor sit amet".format(name)
return "lorem ipsum, {0} dolor sit amet".format(name) 
这样就和普通的调用一样即可get_text("John")。这就是Python中的修饰器,更多信息可参考 
http://thecodeship.com/patterns/guide-to-python-function-decorators/ 
http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps 
几个内置函数 
lambda函数 
lambda函数即匿名函数,没有函数名通常直接调用。考虑下面这个普通函数: 
def squ(x): 
return x*x 
采用关键字lambda的实现 
g = lambda x:x*x 
filter(function, sequence) 
对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于sequence的类型)返 
回 
过滤序列中大于5小于10的数 
def largerThan(x): 
return x > 5 and x < 10 
if __name__ == '__main__': 
x = filter(largerThan, [1,6,4,8,11]) 
print x 
通常与lambda结合使用 
filter(lambda i:i > 5 and i < 10, [1, 6, 4, 8, 11]) 
返回的结果与sequence的类型有关 
filter(lambda i:i > 5 and i < 10, [1, 6, 4, 8, 11]) 
filter(lambda i:i > 5 and i < 10, (1, 6, 4, 8, 11,)) 
filter(lambda x: x != 'a' and x != 'c', 'abcdefadc') 
map(function, sequence) 
对sequence中的item依次执行function(item),将执行结果组成一个List返回.与filter很类似,比较下两者的用法 
filter(lambda i:i > 5 and i < 10, [1, 6, 4, 8, 11])-->[6, 8] 
map(lambda i:i > 5 and i < 10, [1, 6, 4, 8, 11])-->[False, True, False, True, False] 
当然也支持多种参数的输入 
map(lambda i:i * i, [1, 6, 4, 8, 11]) 
map(lambda i: i + i, 'abcd') 
可以看到map返回的序列和远序列的长度一致,只是类型会根据条件变化.同样支持多个序列 
map(lambda x, y: x + y, range(8), range(8)) 
如果函数为None时,返回序列 
map(None, range(3), range(3)) -->[(0, 0), (1, 1), (2, 2)] 
map(None, range(2), range(3)) -->[(0, 0), (1, 1), (None, 2)] 
reduce(function, sequence, starting_value) 
对sequence中的item顺序迭代调用function,如果有starting_value,还可以作为初始值调用.需要注意与上诉用法的区别,是对列 
表中的元素依次调用对应的function 
reduce(lambda x, y:x + y, range(4)) --> 0+1+2+3 
reduce(lambda x, y:x + y, ['a','b','c','d']) 
reduce(lambda x, y:x + y, range(4), 10) --> 0+1+2+3+10
数据结构 
序列 
序列是Python中最基本的元素,列表中的每个元素下标从0开始 
a = [1, 'abc', 2, 3] 
b = [1, 2, [1, 2]] 
索引与分片 
a[0] --> 1 
a[4] --> IndexError 
a[1:3] --> ['abc', 2] 
a[::-1] --> [3, 2, 'abc', 1] 
a[0:20] --> [1, 'abc', 2, 3] 
追加元素 
这里主要介绍3种方式 
a = [1,'a',2,'c'] 
a.append(3) --> a = [1, 'a', 2, 'c', 3] 
a[len(a):] = [3] --> a = [1, 'a', 2, 'c', 3, 3] 
a[len(a):] = 'abc' --> a = [1, 'a', 2, 'c', 3, 3, 'a', 'b', 'c'] 
a.extend([1,2,3]) 
a.extend('abc') 
a.insert(0,'a') 
a.insert(16,'a') 
注意对于a[len(a):] = [3]这种追加方式需要注意 
不能使用a[len(a)+1]来指定下标,这样会抛出IndexError 
赋予的新值必须是可迭代的,如a[len(a):] = 3就会抛出TypeError,当然也支持多个元素的 a[len(a):] = [3, 4, 5] 
对于extend参数也是接受可迭代的类型如列表[1,2,3]或者字符串abc。而对于insert(i, x)则是对指定的下标插入元素,注意下 
标可以大于列表的实际长度,但是也只是在列表后面追加,相应的长度增加1。 
常用方法 
a = [1, 2, 1, 'a', 'b'] 
len(a) -->5 //长度 
a.count(1) --> 2 //元素个数 
a.count(3) --> 0 //元素不存在不报错返回0 
a.index(1) --> 0 //元素在列表中的下标,多个返回第一个 
a.index(3) --> //元素不存在抛异常ValueError 
a.remove(1) --> 多个元素只删除第一个 
a.remove(3) --> 不存在的元素抛出异常ValueError 
a.pop(1) --> 删除指定下标的元素,返回该元素这里为2 
a.pop(10) --> 下标越界抛出IndexError 
列表循环 
对于列表的循环,我们通常采用这种方式 
a = [1, 'a', 2, 'b'] 
for i in a: 
print i 
或者 
for i in range(len(a)):
print a[i] 
如果对列表只是一些简单的运算,可以考虑使用列表推导 
[x * x for x in range(3)]-->[0, 1, 4] 
也可以用if进行过滤 
[x * x for x in range(3) if x % 2 == 0]-->[0, 4] 
对多个for语句进行 
[(x, y) for x in range(3) for y in range(3)] 
有时我们希望将列表处理为一个枚举,可采用列表推导和函数enumerate([]) 
a = ['a', 'b', 'c', 'd', 'e'] 
b = [(index, item) for index, item in enumerate(a)] 
outputs 
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')] 
这里有必要介绍下函数range(start,end,step) 
range(start,end,step) 
返回指定范围的列表,包括起止范围和步长 
range(5) --> [0, 1, 2, 3, 4] 
range(0,5) --> [0, 1, 2, 3, 4] 
range(0,5,2) --> [0, 2, 4] 
与str的区别 
通过上面的讲解我们会发现列表和字符串存在一定的相似地方,如索引,下标操作等。如: 
a = 'abc' 
b = ['a', 'b', 'c'] 
a[0:1] 
b[0:1] 
a[::-1] 
b[::-1] 
a+"d" 
b + ['d'] 
a*3 
b*3 
以上操作都很类似只是结果返回不一样。当然也有不同的地方,列表本质是可变长的,因此对列表的操作中如append, insert等就 
不适合字符串。当然两者也可以很方便转换 
list(a) --> ['a', 'b', 'c'] 
''.join(b) --> abc 
元组 
不可变序列即为元组 
(1,2,3) 
('a','b','c') 
(1,) 
如果只有一个元素需要有末尾的逗号(,)注意比较 
3 * (10 + 2) 
3 * (10 + 2,) 
可以通过函数tuple来实现列表和元组的转换 
tuple('1bs3') 
tuple([1, 2, 3, 4])
tuple([1, 2, 3, 4]) 
tuple((1, 2, 3,)) 
tuple([1, 2, 3, 4, [1, 2]]) 
注意上面最后元素是可以包含可变元素的。对其中可变元素[1,2]操作与普通的列表没什么差别 
t = tuple([1, 2, 3, 4, [1, 2]]) 
t[4].insert(0, 0) 
print t --> (1, 2, 3, 4, [0, 1, 2]) 
元组的循环可以使用上面的列表介绍的方法,但是由于元组不可变那么改变元组的方法是不可用的。会抛出异常AttributeError。 
集合(set) 
与其他语言一样,是一个无序不可重复的元素集。因此不支持下标、分片等类序列操作的方法 
创建集合 
s0 = {'h', 'e', 'l', 'l', 'o'} 
s1 = set('hello') 
s2 = set(['h', 'e', 'l', 'l', 'o']) 
都返回set(['h', 'e', 'l', 'o'])与期望的一样不包括重复元素。同样作为集合元素也不能包含可变元素。像这样使用没问 
题set(((1, 2), (3,)))而set([1, 2, 3, [1, 2]])则会抛出异常TypeError。虽然集合不能包含可变元素,但作为本身是可变的 
(注意与元组比较)。 
s1.add('a') --> set(['a', 'h', 'e', 'l', 'o']) 
s1.add(['a','b']) --> TypeError不支持可变元素 
当然也有不可变的集合frozenset 
>>> cities = frozenset(["Frankfurt", "Basel","Freiburg"]) 
>>> cities.add("Strasbourg") 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
AttributeError: 'frozenset' object has no attribute 'add' 
常用方法 
s0.remove('h') -->s0: set(['e', 'l', 'o']) 
s0.remove('hs') -->不存在的元素抛出KeyError 
s0.discard('h') -->s0: set(['e', 'l', 'o']) 
s0.discard('hs') -->不存在的元素不出错原来一致 
s0.clear() -->s0: set([]) 
交集(difference()) 
>>> x = {"a","b","c","d","e"} 
>>> y = {"b","c"} 
>>> z = {"c","d"} 
>>> x.difference(y) 
set(['a', 'e', 'd']) 
>>> x.difference(y).difference(z) 
set(['a', 'e']) 
也可以更简单的处理方式 
>>> x - y 
set(['a', 'e', 'd']) 
>>> x - y - z 
set(['a', 'e']) 
补集(difference_update) 
x = {"a","b","c","d","e"} 
y = {"b","c"} 
x.difference_update(y) 
x --> set(['a', 'e', 'd']) 
z = x - y 
z --> set(['a', 'e', 'd'])
z --> set(['a', 'e', 'd']) 
或者 
x = {"a", "b", "c", } 
y = {"c", "d", "e"} 
print x - y 
print x | y 
print x & y 
outputs 
set(['a', 'b']) 
set(['a', 'c', 'b', 'e', 'd']) 
set(['c']) 
字典 
字典与java中的map作用类似首先来看如何创建一个字典 
d1 = {} 
d2 = {'name':'python', 'version':'2.7.6'} 
d3 = dict((['name', 'python'], ['version', 2.7])) 
d4 = dict(name='python', version=2.7) 
d5 = {}.fromkeys(('x', 'y'), -1) 
对fromkeys接收两个参数,前面作为key后面相应的为value,如果只有一个参数那么就是这样: d5 = {}.fromkeys(('x', 'y')) 
#{'x': None, 'y': None} 
遍历访问 
d2 = {'name':'python', 'version':'2.7.6'} 
for key in d2: 
print "key=%s, value=%s" % (key, d2[key]) 
for key in d2.iterkeys(): 
print "key=%s, value=%s" % (key, d2[key]) 
for key in d2.keys(): 
print "key=%s, value=%s" % (key, d2[key]) 
for key in iter(d2): 
print "key=%s, value=%s" % (key, d2[key]) 
for key, item in d2.items(): 
print "key=%s, value=%s" % (key, item) 
以上的遍历是通过key,value或iter的访问。当然也支持对单个key的访问,如d2['name']或d2.get(name)对于后者也可以传入默认 
的value,如果key不存在时返回该值,此时该字典并不改变 
d2.get('age', 10) >> 10 
print d2 >> {'version': '2.7.6', 'name': 'python'} 
转换 
常见的转换,可以将字典转换为列表(list)和迭代器(iterator) 
dict={'name':'python', 'version':'2.7.6'} 
#list 
dict.items() >>[('version', '2.7.6'), ('name', 'python')] 
dict.keys() >>['version', 'name'] 
dict.values() >>['2.7.6', 'python'] 
#iterator 
dict.iteritems() 
dict.iterkeys() 
dict.itervalues() 
常见方法 
dict.copy() #返回字典(浅复制)的一个副本:原字典发生改变不影响新的字典 
dict.clear() #清空字典中所有元素:{} 
dict1.has_key("name") #判断是否有键,返回True或False
dict1.has_key("name") #判断是否有键,返回True或False 
dict.pop(key[, default]) #如果字典中key存在,删除并返回dic[key],如果key不存在,default没赋值将抛出KeyError 
dict = {'name':'python', 'version':'2.7.6'} 
dict.pop("name") 
dict.pop("name","python") 
dict.pop("name") 
dict.setdefault(key,default=None) 和方法set()相似,如果字典中不存在key 键,由dict[key]=default为它赋值 
dict.update(dict2) 将字典dict2 的键-值对添加到字典dict 
创建进阶的字典 
针对字典中key是列表,可这样操作dict = {'name':['python','java']}可用setdefault()来实现 
dict = {}; 
dict.setdefault("name",[]).append("python"); 
dict.setdefault("name",[]).append("java"); 
或者用集合中的defaultdict 
from collections import defaultdict 
d = defaultdict(list); 
d['name'].append('python') 
d['name'].append('java') 
上面使用的集合模块同样提供了有序集合OrderedDict 
d = OrderedDict() 
d['name'] = 'python' 
d['age'] = 20 
字典的计算 
几种计算 
d = { 
'iphone5':5000, 
'g4':4000, 
'mi3':1999, 
'mi2s':1699 
} 
min(d) //key按照字母顺序排列的最小 
min(d.values()) # values最小 
min(zip(d.values(), d.keys())) # 最小的序列,按照values排序。 
max(zip(d.values(), d.keys())) 
sorted(zip(d.values(), d.keys())) # 按照values排序 
min(d, key=lambda k:d[k])#获取values最小的key 
上面用到了几个内置函数:zip,min,max,sorted: 
zip:接受可迭代的参数转换为元组或列表,如果长度不一样返回最小的列 
d = { 
'iphone5':5000, 
'g4':4000 
} 
zip(d, d.values()) 
zip([1,2],[1,2,3]) 
找到相同的 
a = {'x':1,'y':2,'z':3} 
b = {'a':4,'y':2,'z':4} 
set(a.keys()) & set(b.keys()) #{'y', 'z'} 
set(a.keys()) - set(b.keys()) #{'x'} 
set(a.items()) & set(b.items()) #{('y', 2)} 
根据条件生产新的字典
a = {'x':1, 'y':2, 'z':3} 
{key:a[key] for key in set(a.keys()) - {'z'}} #{'x': 1, 'y': 2} 
生成新的字典中将不包含key:z 
参考 
https://docs.python.org/2.7/ 
http://blog.jobbole.com/68256/ 
http://blog.jobbole.com/69834/ 
http://www.python-course.eu/course.php 
http://blog.segmentfault.com/qiwsir 
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000

More Related Content

What's hot

Bash shell script 教學
Bash shell script 教學Bash shell script 教學
Bash shell script 教學Ming-Sian Lin
 
Sql培训 (1)
Sql培训 (1)Sql培训 (1)
Sql培训 (1)jhao niu
 
COSCUP: Metaprogramming in Julia
COSCUP: Metaprogramming in JuliaCOSCUP: Metaprogramming in Julia
COSCUP: Metaprogramming in Julia岳華 杜
 
Wind.js无障碍调试与排错
Wind.js无障碍调试与排错Wind.js无障碍调试与排错
Wind.js无障碍调试与排错jeffz
 
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...彼得潘 Pan
 
Python速成指南
Python速成指南Python速成指南
Python速成指南March Liu
 
改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法crasysatan
 
LazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLin Yo-An
 
twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC
 

What's hot (20)

Ch10 教學
Ch10 教學Ch10 教學
Ch10 教學
 
Python串列資料應用
Python串列資料應用Python串列資料應用
Python串列資料應用
 
Bash shell script 教學
Bash shell script 教學Bash shell script 教學
Bash shell script 教學
 
Sql培训 (1)
Sql培训 (1)Sql培训 (1)
Sql培训 (1)
 
COSCUP: Metaprogramming in Julia
COSCUP: Metaprogramming in JuliaCOSCUP: Metaprogramming in Julia
COSCUP: Metaprogramming in Julia
 
Ch8
Ch8Ch8
Ch8
 
Ch9
Ch9Ch9
Ch9
 
Wind.js无障碍调试与排错
Wind.js无障碍调试与排错Wind.js无障碍调试与排错
Wind.js无障碍调试与排错
 
Fp
FpFp
Fp
 
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
 
Python變數與資料運算
Python變數與資料運算Python變數與資料運算
Python變數與資料運算
 
Ch7 教學
Ch7 教學Ch7 教學
Ch7 教學
 
Python速成指南
Python速成指南Python速成指南
Python速成指南
 
改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法改善程序设计技术的50个有效做法
改善程序设计技术的50个有效做法
 
Python 迴圈作業
Python 迴圈作業Python 迴圈作業
Python 迴圈作業
 
Ch9 教學
Ch9 教學Ch9 教學
Ch9 教學
 
Ch12
Ch12Ch12
Ch12
 
LazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHP
 
twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹
 
nodeMCU IOT教學02 - Lua語言
nodeMCU IOT教學02 - Lua語言nodeMCU IOT教學02 - Lua語言
nodeMCU IOT教學02 - Lua語言
 

Viewers also liked

Slide 4 pengorganisasian kearsipan
Slide 4 pengorganisasian kearsipanSlide 4 pengorganisasian kearsipan
Slide 4 pengorganisasian kearsipanFlorencia Monica
 
Brand Building in the Age of Big Data by Mr. Gavin Coombes
Brand Building in the Age of Big Data by Mr. Gavin CoombesBrand Building in the Age of Big Data by Mr. Gavin Coombes
Brand Building in the Age of Big Data by Mr. Gavin Coombeswkwsci-research
 
Uu hak cipta
Uu hak ciptaUu hak cipta
Uu hak cipta123admin
 
Fisheries Debates Management Challenges
Fisheries Debates Management Challenges
Fisheries Debates Management Challenges
Fisheries Debates Management Challenges permissibleyout77
 
Creativity without comprise by Cleve Gibbon
Creativity without comprise by Cleve Gibbon Creativity without comprise by Cleve Gibbon
Creativity without comprise by Cleve Gibbon AEM HUB
 
Action weekly'15 edition 1
Action weekly'15 edition 1Action weekly'15 edition 1
Action weekly'15 edition 1inactionagency
 
(N)one Click Deployment by Joerg Hoh
(N)one Click Deployment by Joerg Hoh(N)one Click Deployment by Joerg Hoh
(N)one Click Deployment by Joerg HohAEM HUB
 
Adjusting Document Margins
Adjusting Document MarginsAdjusting Document Margins
Adjusting Document Marginscbuzz001
 
Chris margins pp
Chris margins ppChris margins pp
Chris margins ppcbuzz001
 
Choosing Page Orientation
Choosing Page OrientationChoosing Page Orientation
Choosing Page Orientationcbuzz001
 
Action Weekly_april_2015
Action Weekly_april_2015Action Weekly_april_2015
Action Weekly_april_2015inactionagency
 
openSIS - A portal that is must for the educational system
openSIS - A portal that is must for the educational systemopenSIS - A portal that is must for the educational system
openSIS - A portal that is must for the educational systemEjobIndia
 
Adjusting Fonts
Adjusting FontsAdjusting Fonts
Adjusting Fontscbuzz001
 

Viewers also liked (17)

Slide 4 pengorganisasian kearsipan
Slide 4 pengorganisasian kearsipanSlide 4 pengorganisasian kearsipan
Slide 4 pengorganisasian kearsipan
 
Brand Building in the Age of Big Data by Mr. Gavin Coombes
Brand Building in the Age of Big Data by Mr. Gavin CoombesBrand Building in the Age of Big Data by Mr. Gavin Coombes
Brand Building in the Age of Big Data by Mr. Gavin Coombes
 
Uu hak cipta
Uu hak ciptaUu hak cipta
Uu hak cipta
 
Action weekly ver.3
Action weekly ver.3Action weekly ver.3
Action weekly ver.3
 
Fisheries Debates Management Challenges
Fisheries Debates Management Challenges
Fisheries Debates Management Challenges
Fisheries Debates Management Challenges
 
Creativity without comprise by Cleve Gibbon
Creativity without comprise by Cleve Gibbon Creativity without comprise by Cleve Gibbon
Creativity without comprise by Cleve Gibbon
 
Your appearance
Your appearanceYour appearance
Your appearance
 
Action weekly'15 edition 1
Action weekly'15 edition 1Action weekly'15 edition 1
Action weekly'15 edition 1
 
Action weekly ver.6
Action weekly ver.6Action weekly ver.6
Action weekly ver.6
 
(N)one Click Deployment by Joerg Hoh
(N)one Click Deployment by Joerg Hoh(N)one Click Deployment by Joerg Hoh
(N)one Click Deployment by Joerg Hoh
 
Adjusting Document Margins
Adjusting Document MarginsAdjusting Document Margins
Adjusting Document Margins
 
Chris margins pp
Chris margins ppChris margins pp
Chris margins pp
 
Choosing Page Orientation
Choosing Page OrientationChoosing Page Orientation
Choosing Page Orientation
 
HOSP PP
HOSP PPHOSP PP
HOSP PP
 
Action Weekly_april_2015
Action Weekly_april_2015Action Weekly_april_2015
Action Weekly_april_2015
 
openSIS - A portal that is must for the educational system
openSIS - A portal that is must for the educational systemopenSIS - A portal that is must for the educational system
openSIS - A portal that is must for the educational system
 
Adjusting Fonts
Adjusting FontsAdjusting Fonts
Adjusting Fonts
 

Similar to Python learn guide (20)

Ch4 教學
Ch4 教學Ch4 教學
Ch4 教學
 
Ch4
Ch4Ch4
Ch4
 
Ch8 教學
Ch8 教學Ch8 教學
Ch8 教學
 
Ch5 教學
Ch5 教學Ch5 教學
Ch5 教學
 
Ch5
Ch5Ch5
Ch5
 
ncuma_串列.pptx
ncuma_串列.pptxncuma_串列.pptx
ncuma_串列.pptx
 
Arduino程式快速入門
Arduino程式快速入門Arduino程式快速入門
Arduino程式快速入門
 
Ch5
Ch5Ch5
Ch5
 
ncuma_pylab.pptx
ncuma_pylab.pptxncuma_pylab.pptx
ncuma_pylab.pptx
 
Python 入門
Python 入門 Python 入門
Python 入門
 
Scilab Chap1
Scilab Chap1Scilab Chap1
Scilab Chap1
 
Swift基礎
Swift基礎Swift基礎
Swift基礎
 
Ppt 51-77
Ppt 51-77Ppt 51-77
Ppt 51-77
 
Ppt 51-77
Ppt 51-77Ppt 51-77
Ppt 51-77
 
Scilab introduction(Scilab 介紹)
Scilab introduction(Scilab 介紹)Scilab introduction(Scilab 介紹)
Scilab introduction(Scilab 介紹)
 
Ch1 教學
Ch1 教學Ch1 教學
Ch1 教學
 
Ch1
Ch1Ch1
Ch1
 
Ppt 1-50
Ppt 1-50Ppt 1-50
Ppt 1-50
 
Ch2 教學
Ch2 教學Ch2 教學
Ch2 教學
 
第三章 栈和队列
第三章 栈和队列第三章 栈和队列
第三章 栈和队列
 

Python learn guide

  • 1. 变量与数据类型 变量的声明 a = 1 b = -100 c = 12.3 d = 341341325234234523433333333333338888888888888L e = 1235324523452346 * 999999999999999999999999 my_name = 'python' _my_name = "Life is short, I use Python" _my_name2 = 'I'm "OK"!' a1,b1,c1 = (1,2,3) a2,b2,c2 = [1,2,3] 对于批量的命名需要注意变量个数和序列个数保存一致,否则会抛出ValueError更多用法 a,b,c,d,e='hello' ##必须为strings,files,iterators _,age,_=['robin',10,('aaa',222,'ccc')] ##只关注age 或者 a,b,*c=[1,2,3,4,5] #c=[3,4,5] a,*b,c=[1,2,3,4,5] #b=[2,3,4] bool 采用True, False表示 >>> True True >>> False False >>> 3 > 2 True >>> 3 > 5 False 布尔运算 True and True ##True True and False ## False False and False ## False True or True ##True True or False ##True False or False ##False not True ## False not False ## True 数字与运算 类型 int 整数,正整数或负整数 type(10)--> <type 'int'> long 长整数,无限大小。可以l或L结尾 type(3452345234523452345234) --> <type 'long'> float 浮点数 type(2.1)--><type 'float'>, type(32.3e18) --> <type 'float'> complex 复数 type(1+2j)--><type 'complex'> 算术运算 7 + 2 >> 9 7 - 2 >> 5 7 * 2 >> 14 7 / 2 >> 3 7 % 2 >> 1
  • 2. 7 % 2 >> 1 注意/是取整,可以用浮点数除法或from __future__ import division在python3中处理为一样 7.0 / 2 >> 3.5 7 / 2.0 >> 3.5 7. /2 >> 3.5 或 >>> from __future__ import division >>> 7/2 3.5 再介绍一种运算//返回除法的整数部分 7 // 2 >> 3 7.0 // 2 >> 3.0 比较、逻辑、布尔运算 常用比较运算(==, !=, >, <, >=, <=) 布尔运算 and, or, not >>> 4>3 and 4>5 False >>> 4>3 or 4>5 True >>> not 4>3 False >>> not 4>5 True 当然在python中比较并不只是这么简单,下面来个链式比较: >>> x = 5 >>> 1 < x < 10 True >>> x < 10 < x*10 < 100 True 这才是你希望的书写方式 不存在整数溢出问题 >>>123456789870987654321122343445567678890098*123345566778999009987654333238766544 15227847719352756287004435258757627686452840919334677759119167301324455281312L 神奇的* >>> a = 'abc' >>> a*4 'abcabcabcabc' >>> b = (1,2) >>> b*2 (1, 2, 1, 2) >>> c = [1,2] >>> c*3 [1, 2, 1, 2, 1, 2] 字符串 申明 a0 = "Hello" a1 = 'world' //运算 a = a0 + a1 a*2 //转义或doc方式 b = "what's your name?"
  • 3. c = 'what's your name' d = """what's your name?""" //unicode >>> e = u'你好' >>> e u'xc4xe3xbaxc3' 字符串连接 a = "my name is %s, and %d years old"%('abc',1) b = "my name is %s"%'abc' 常用方法 a = 'Hello World' len(a) //大小写转换 a.upper() a.lower() a.capitalize() //下标与数量 a.index('e') a.count('l') a.strip(); a.rstrip() 分组、切片与其他 索引 name = 'Hello' name[0] --> H name[-2] --> l name[6] -->IndexError 切片 name[0:3] --> 'Hel' name[3:] --> 'lo' name[:] --> 'Hello' name[0:3:2] --> 'Hl' 步长为2 name[::-1] --> 'olleH' 其他 list(name) --> ['H', 'e', 'l', 'l', 'o']返回用"".join([]) >>> 'a' in name False >>> 'a' not in name True 函数 函数定义 import random def guess_a_number(x): a = random.randrange(1, 100) if x > a: print 'Larger' elif x == a: print 'equal' else: pass 几点说明 用关键字def申明,不使用{}用缩进代替,注意后面的冒号(:) 注意参数个数和类型
  • 4. 注意参数个数和类型 pass表示空实现 是否可以返回多个参数?答案是肯定的 def return_multi_param(x): return x + 1, x + 2 a,b = return_multi_param(1) 可变参数 def foo(name, *args, **kwds): print "name= ", name print "args= ", args print "kwds= ",kwds 可简单理解*args为可变列表, **kwds为可变字典 foo("python", "10", "chengdu", a="aaaaaaaaa", b="vvvvvvvvvvvvv") ## outputs name= python args= ('10', 'chengdu') kwds= {'a': 'aaaaaaaaa', 'b': 'vvvvvvvvvvvvv'} 默认参数 def foo(name = 'abc'): print "name= ", name ## outputs foo() #name= abc foo("lala") #name= lala 注意如果默认参数是可变参数,那么可能导致和你预期不一样的结果 def foo(bar=[]): bar.append('bar') return bar foo() >>['bar'] foo() >>['bar', 'bar'] foo() >>['bar', 'bar', 'bar'] 这里调用三次后结果并不是预期那样:一个函数参数的默认值,仅仅在该函数定义的时候,被赋值一次。如此,只有当函数foo()第 一次被定义的时候,才讲参数bar的默认值初始化到它的默认值(即一个空的列表)。当调用foo()的时候(不给参数bar),会继续 使用bar最早初始化时的那个列表。 当然也有方式来避免这种方式 def foo(bar=[]): if not bar: bar = [] bar.append('bar') return bar 更多信息见http://blog.jobbole.com/40088/ 函数作为参数传递 def get_text(name): return "lorem ipsum, {0} dolor sit amet".format(name) def p_decorate(func): def func_wrapper(name): return "<p>{0}</p>".format(func(name)) return func_wrapper my_get_text = p_decorate(get_text) ## outputs <p>lorem ipsum, John dolor sit amet</p> 这也可以用语法糖@来修饰 @p_decorate def get_text(name): return "lorem ipsum, {0} dolor sit amet".format(name)
  • 5. return "lorem ipsum, {0} dolor sit amet".format(name) 这样就和普通的调用一样即可get_text("John")。这就是Python中的修饰器,更多信息可参考 http://thecodeship.com/patterns/guide-to-python-function-decorators/ http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps 几个内置函数 lambda函数 lambda函数即匿名函数,没有函数名通常直接调用。考虑下面这个普通函数: def squ(x): return x*x 采用关键字lambda的实现 g = lambda x:x*x filter(function, sequence) 对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于sequence的类型)返 回 过滤序列中大于5小于10的数 def largerThan(x): return x > 5 and x < 10 if __name__ == '__main__': x = filter(largerThan, [1,6,4,8,11]) print x 通常与lambda结合使用 filter(lambda i:i > 5 and i < 10, [1, 6, 4, 8, 11]) 返回的结果与sequence的类型有关 filter(lambda i:i > 5 and i < 10, [1, 6, 4, 8, 11]) filter(lambda i:i > 5 and i < 10, (1, 6, 4, 8, 11,)) filter(lambda x: x != 'a' and x != 'c', 'abcdefadc') map(function, sequence) 对sequence中的item依次执行function(item),将执行结果组成一个List返回.与filter很类似,比较下两者的用法 filter(lambda i:i > 5 and i < 10, [1, 6, 4, 8, 11])-->[6, 8] map(lambda i:i > 5 and i < 10, [1, 6, 4, 8, 11])-->[False, True, False, True, False] 当然也支持多种参数的输入 map(lambda i:i * i, [1, 6, 4, 8, 11]) map(lambda i: i + i, 'abcd') 可以看到map返回的序列和远序列的长度一致,只是类型会根据条件变化.同样支持多个序列 map(lambda x, y: x + y, range(8), range(8)) 如果函数为None时,返回序列 map(None, range(3), range(3)) -->[(0, 0), (1, 1), (2, 2)] map(None, range(2), range(3)) -->[(0, 0), (1, 1), (None, 2)] reduce(function, sequence, starting_value) 对sequence中的item顺序迭代调用function,如果有starting_value,还可以作为初始值调用.需要注意与上诉用法的区别,是对列 表中的元素依次调用对应的function reduce(lambda x, y:x + y, range(4)) --> 0+1+2+3 reduce(lambda x, y:x + y, ['a','b','c','d']) reduce(lambda x, y:x + y, range(4), 10) --> 0+1+2+3+10
  • 6. 数据结构 序列 序列是Python中最基本的元素,列表中的每个元素下标从0开始 a = [1, 'abc', 2, 3] b = [1, 2, [1, 2]] 索引与分片 a[0] --> 1 a[4] --> IndexError a[1:3] --> ['abc', 2] a[::-1] --> [3, 2, 'abc', 1] a[0:20] --> [1, 'abc', 2, 3] 追加元素 这里主要介绍3种方式 a = [1,'a',2,'c'] a.append(3) --> a = [1, 'a', 2, 'c', 3] a[len(a):] = [3] --> a = [1, 'a', 2, 'c', 3, 3] a[len(a):] = 'abc' --> a = [1, 'a', 2, 'c', 3, 3, 'a', 'b', 'c'] a.extend([1,2,3]) a.extend('abc') a.insert(0,'a') a.insert(16,'a') 注意对于a[len(a):] = [3]这种追加方式需要注意 不能使用a[len(a)+1]来指定下标,这样会抛出IndexError 赋予的新值必须是可迭代的,如a[len(a):] = 3就会抛出TypeError,当然也支持多个元素的 a[len(a):] = [3, 4, 5] 对于extend参数也是接受可迭代的类型如列表[1,2,3]或者字符串abc。而对于insert(i, x)则是对指定的下标插入元素,注意下 标可以大于列表的实际长度,但是也只是在列表后面追加,相应的长度增加1。 常用方法 a = [1, 2, 1, 'a', 'b'] len(a) -->5 //长度 a.count(1) --> 2 //元素个数 a.count(3) --> 0 //元素不存在不报错返回0 a.index(1) --> 0 //元素在列表中的下标,多个返回第一个 a.index(3) --> //元素不存在抛异常ValueError a.remove(1) --> 多个元素只删除第一个 a.remove(3) --> 不存在的元素抛出异常ValueError a.pop(1) --> 删除指定下标的元素,返回该元素这里为2 a.pop(10) --> 下标越界抛出IndexError 列表循环 对于列表的循环,我们通常采用这种方式 a = [1, 'a', 2, 'b'] for i in a: print i 或者 for i in range(len(a)):
  • 7. print a[i] 如果对列表只是一些简单的运算,可以考虑使用列表推导 [x * x for x in range(3)]-->[0, 1, 4] 也可以用if进行过滤 [x * x for x in range(3) if x % 2 == 0]-->[0, 4] 对多个for语句进行 [(x, y) for x in range(3) for y in range(3)] 有时我们希望将列表处理为一个枚举,可采用列表推导和函数enumerate([]) a = ['a', 'b', 'c', 'd', 'e'] b = [(index, item) for index, item in enumerate(a)] outputs [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')] 这里有必要介绍下函数range(start,end,step) range(start,end,step) 返回指定范围的列表,包括起止范围和步长 range(5) --> [0, 1, 2, 3, 4] range(0,5) --> [0, 1, 2, 3, 4] range(0,5,2) --> [0, 2, 4] 与str的区别 通过上面的讲解我们会发现列表和字符串存在一定的相似地方,如索引,下标操作等。如: a = 'abc' b = ['a', 'b', 'c'] a[0:1] b[0:1] a[::-1] b[::-1] a+"d" b + ['d'] a*3 b*3 以上操作都很类似只是结果返回不一样。当然也有不同的地方,列表本质是可变长的,因此对列表的操作中如append, insert等就 不适合字符串。当然两者也可以很方便转换 list(a) --> ['a', 'b', 'c'] ''.join(b) --> abc 元组 不可变序列即为元组 (1,2,3) ('a','b','c') (1,) 如果只有一个元素需要有末尾的逗号(,)注意比较 3 * (10 + 2) 3 * (10 + 2,) 可以通过函数tuple来实现列表和元组的转换 tuple('1bs3') tuple([1, 2, 3, 4])
  • 8. tuple([1, 2, 3, 4]) tuple((1, 2, 3,)) tuple([1, 2, 3, 4, [1, 2]]) 注意上面最后元素是可以包含可变元素的。对其中可变元素[1,2]操作与普通的列表没什么差别 t = tuple([1, 2, 3, 4, [1, 2]]) t[4].insert(0, 0) print t --> (1, 2, 3, 4, [0, 1, 2]) 元组的循环可以使用上面的列表介绍的方法,但是由于元组不可变那么改变元组的方法是不可用的。会抛出异常AttributeError。 集合(set) 与其他语言一样,是一个无序不可重复的元素集。因此不支持下标、分片等类序列操作的方法 创建集合 s0 = {'h', 'e', 'l', 'l', 'o'} s1 = set('hello') s2 = set(['h', 'e', 'l', 'l', 'o']) 都返回set(['h', 'e', 'l', 'o'])与期望的一样不包括重复元素。同样作为集合元素也不能包含可变元素。像这样使用没问 题set(((1, 2), (3,)))而set([1, 2, 3, [1, 2]])则会抛出异常TypeError。虽然集合不能包含可变元素,但作为本身是可变的 (注意与元组比较)。 s1.add('a') --> set(['a', 'h', 'e', 'l', 'o']) s1.add(['a','b']) --> TypeError不支持可变元素 当然也有不可变的集合frozenset >>> cities = frozenset(["Frankfurt", "Basel","Freiburg"]) >>> cities.add("Strasbourg") Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'frozenset' object has no attribute 'add' 常用方法 s0.remove('h') -->s0: set(['e', 'l', 'o']) s0.remove('hs') -->不存在的元素抛出KeyError s0.discard('h') -->s0: set(['e', 'l', 'o']) s0.discard('hs') -->不存在的元素不出错原来一致 s0.clear() -->s0: set([]) 交集(difference()) >>> x = {"a","b","c","d","e"} >>> y = {"b","c"} >>> z = {"c","d"} >>> x.difference(y) set(['a', 'e', 'd']) >>> x.difference(y).difference(z) set(['a', 'e']) 也可以更简单的处理方式 >>> x - y set(['a', 'e', 'd']) >>> x - y - z set(['a', 'e']) 补集(difference_update) x = {"a","b","c","d","e"} y = {"b","c"} x.difference_update(y) x --> set(['a', 'e', 'd']) z = x - y z --> set(['a', 'e', 'd'])
  • 9. z --> set(['a', 'e', 'd']) 或者 x = {"a", "b", "c", } y = {"c", "d", "e"} print x - y print x | y print x & y outputs set(['a', 'b']) set(['a', 'c', 'b', 'e', 'd']) set(['c']) 字典 字典与java中的map作用类似首先来看如何创建一个字典 d1 = {} d2 = {'name':'python', 'version':'2.7.6'} d3 = dict((['name', 'python'], ['version', 2.7])) d4 = dict(name='python', version=2.7) d5 = {}.fromkeys(('x', 'y'), -1) 对fromkeys接收两个参数,前面作为key后面相应的为value,如果只有一个参数那么就是这样: d5 = {}.fromkeys(('x', 'y')) #{'x': None, 'y': None} 遍历访问 d2 = {'name':'python', 'version':'2.7.6'} for key in d2: print "key=%s, value=%s" % (key, d2[key]) for key in d2.iterkeys(): print "key=%s, value=%s" % (key, d2[key]) for key in d2.keys(): print "key=%s, value=%s" % (key, d2[key]) for key in iter(d2): print "key=%s, value=%s" % (key, d2[key]) for key, item in d2.items(): print "key=%s, value=%s" % (key, item) 以上的遍历是通过key,value或iter的访问。当然也支持对单个key的访问,如d2['name']或d2.get(name)对于后者也可以传入默认 的value,如果key不存在时返回该值,此时该字典并不改变 d2.get('age', 10) >> 10 print d2 >> {'version': '2.7.6', 'name': 'python'} 转换 常见的转换,可以将字典转换为列表(list)和迭代器(iterator) dict={'name':'python', 'version':'2.7.6'} #list dict.items() >>[('version', '2.7.6'), ('name', 'python')] dict.keys() >>['version', 'name'] dict.values() >>['2.7.6', 'python'] #iterator dict.iteritems() dict.iterkeys() dict.itervalues() 常见方法 dict.copy() #返回字典(浅复制)的一个副本:原字典发生改变不影响新的字典 dict.clear() #清空字典中所有元素:{} dict1.has_key("name") #判断是否有键,返回True或False
  • 10. dict1.has_key("name") #判断是否有键,返回True或False dict.pop(key[, default]) #如果字典中key存在,删除并返回dic[key],如果key不存在,default没赋值将抛出KeyError dict = {'name':'python', 'version':'2.7.6'} dict.pop("name") dict.pop("name","python") dict.pop("name") dict.setdefault(key,default=None) 和方法set()相似,如果字典中不存在key 键,由dict[key]=default为它赋值 dict.update(dict2) 将字典dict2 的键-值对添加到字典dict 创建进阶的字典 针对字典中key是列表,可这样操作dict = {'name':['python','java']}可用setdefault()来实现 dict = {}; dict.setdefault("name",[]).append("python"); dict.setdefault("name",[]).append("java"); 或者用集合中的defaultdict from collections import defaultdict d = defaultdict(list); d['name'].append('python') d['name'].append('java') 上面使用的集合模块同样提供了有序集合OrderedDict d = OrderedDict() d['name'] = 'python' d['age'] = 20 字典的计算 几种计算 d = { 'iphone5':5000, 'g4':4000, 'mi3':1999, 'mi2s':1699 } min(d) //key按照字母顺序排列的最小 min(d.values()) # values最小 min(zip(d.values(), d.keys())) # 最小的序列,按照values排序。 max(zip(d.values(), d.keys())) sorted(zip(d.values(), d.keys())) # 按照values排序 min(d, key=lambda k:d[k])#获取values最小的key 上面用到了几个内置函数:zip,min,max,sorted: zip:接受可迭代的参数转换为元组或列表,如果长度不一样返回最小的列 d = { 'iphone5':5000, 'g4':4000 } zip(d, d.values()) zip([1,2],[1,2,3]) 找到相同的 a = {'x':1,'y':2,'z':3} b = {'a':4,'y':2,'z':4} set(a.keys()) & set(b.keys()) #{'y', 'z'} set(a.keys()) - set(b.keys()) #{'x'} set(a.items()) & set(b.items()) #{('y', 2)} 根据条件生产新的字典
  • 11. a = {'x':1, 'y':2, 'z':3} {key:a[key] for key in set(a.keys()) - {'z'}} #{'x': 1, 'y': 2} 生成新的字典中将不包含key:z 参考 https://docs.python.org/2.7/ http://blog.jobbole.com/68256/ http://blog.jobbole.com/69834/ http://www.python-course.eu/course.php http://blog.segmentfault.com/qiwsir http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000