python基础(List,Dictionary,Random,循环,Lambda,Tumple)
一.List
1 #coding=utf-8 2 xx = [8, 2, 4, 5, 6] 3 xx.append(7) 4 print xx 5 print xx[:3] 6 print xx[3:] 7 print xx.index(4) 8 xx.insert(3, 3) 9 print xx 10 xx.sort() 11 print xx 12 xx.remove(2) 13 print xx 14 xx.pop(2) 15 print xx 16 del(xx[1]) 17 print xx
1 m = [1, 2, 3] 2 n = [4, 5, 6] 3 m += n 4 print m 5 m = [1, 2, 3] 6 m.extend(n) 7 print m 8 letters = ['a', 'b', 'c', 'd'] 9 print '---'.join(letters) 10 print ["o"] * 5
1 a = [5, 4, 3, 2, 1] 2 print sorted(a) #1, 2, 3, 4, 5 3 print a#5, 4, 3, 2, 1
1 print xrange(3, 11, 2) 2 print list(xrange(3, 11, 2))
xrange 用法与 range 完全相同,所不同的是生成的不是一个list对象,而是一个生成器
1 a = range(0,100) 2 print type(a) 3 print a 4 print a[0], a[1] 5 6 a = xrange(0,100) 7 print type(a) 8 print a 9 print a[0], a[1]
1 a = [1,132,0.432] 2 print help(a.sort) #sort(cmp=None, key=None, reverse=False) 3 a.sort(reverse=True) 4 print a 5 a = ['ab', 'abc', 'a'] 6 a.sort(key=len) 7 print a
1 lst = range(1, 11) 2 print lst[::2] #[1, 3, 5, 7, 9] 3 4 letters = ['a', 'b', 'c'] 5 print letters[::-1] #['c', 'b', 'a'] 6 print letters[-2:] #['b', 'c']
二.Dictionary
1 menu = {} 2 menu['ChickenAlfredo'] = 14.5 3 menu['Spam'] = 2.5 4 menu['American'] = 2.5 5 menu['China'] = 2.5 6 print menu 7 print menu.items() 8 print menu.keys() 9 print menu.values() 10 11 del menu['China'] 12 print menu 13 14 inventory = {'g':500, 'p':['flint', 'twine']} 15 16 lloyd = {'a':1, 'c':2, 'b':3} 17 print lloyd.get('a')
keys should hashable 生存期内可变的对象不可以hash(列表,字典,集合)
字典keys无顺序
1 #coding=utf-8 2 xx = [['a' , 1], ['b' , 2]] 3 yy = [('a' , 1), ('b' , 2)] 4 print dict(xx) #{'a': 1, 'b': 2} 5 print dict(yy) #{'a': 1, 'b': 2} 6 print dict(a = 1, b = 2) #{'a': 1, 'b': 2} 7 8 a = {}.fromkeys(('a', 'b', 'c', 'd'), 3000) 9 print a #{'a': 3000, 'c': 3000, 'b': 3000, 'd': 3000} 10 11 del xx #删除字典 12 13 template = 'abc %(a)d, %(b)d' 14 print template%dict(yy) #abc 1, 2 15 16 a = ['a', 'b', 'c'] 17 b = [1, 2, 3] 18 c = dict(zip(a, b)) #{'a': 1, 'c': 3, 'b': 2}
二.Random
1 import random 2 random.randint(0, 5) #0, 1, 2, 3, 4, 5 3 random.randrange(0, 5, 1) #0, 1, 2, 3, 4
三.循环
1 count = 0 2 while count < 10 3 count += 1 4 5 while count < 3: 6 pass 7 else: 8 pass 9 10 while count != 'y' and count != 5: 11 count -= 1 12 13 for i in range(20): 14 print i #0-19 15 16 thing = "Spam!" 17 for c in thing: 18 print c 19 20 d = {'a' : 1, 'b' : 2} 21 for key in d: 22 print d[key]
1 list = ['a', 'b', 'c', 'd'] 2 for i, j in enumerate(list): 3 print i + 1, j 4 5 m = [3, 5, 7, 9] 6 n = [2, 4, 6, 8, 10, 12, 14] 7 print zip(m, n) 8 for a, b in zip(m , n): 9 if a > b: 10 print a 11 else: 12 print b
五.Lambda,filter,map,reduce
1 #coding=utf-8 2 bythree = lambda x: x % 3 == 0 3 print bythree(9)#True 4 5 add = lambda x, y: x+y 6 print add(3, 4) #7 7 8 myList = [3, 6, 9, 12, 11, 10] 9 print filter(bythree, myList) 10 """ 11 对myList中每项依次执行function将结果为true的item组成一个List,String,Tuple(取决于myList类型) 12 filter(function, sequence) 13 [3, 6, 9, 12] 14 """ 15 16 print map(lambda x:x**2, range(1,11)) 17 """ 18 对sequence中每项依次执行function将结果返回的item组成一个List 19 map(function, sequence) 20 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 21 """ 22 23 print reduce(add, range(1, 11), 20) 24 """ 25 对sequence中每项顺序迭代调用 26 reduce(function, sequence, starting_value) 27 1+2+3+4+5+6+7+8+9+10+20=75 28 """
二.Tumple
1 #coding=utf-8 2 t = ('a', 'b', 'c', 'd', 'e') 3 print type(t) #<type 'tuple'> 4 print t #('a', 'b', 'c', 'd', 'e') 5 print (0, 1, 2) < (0, 3, 4) #True 6 print (0, 1, 2000000) < (0, 3, 4)#True 7 """ 8 比较运算符适用于元组和其它序列, Python从每个序列的第一个元素开始比较。 如果它们相等, 继续比较下一个元素, 以此 9 类推, 直到找到不同的元素。 找到不同元素之后, 随后的元素就不再考虑了 ( 即便它们真得很大) 10 """ 11 t = (3, 2, 1) 12 (x, y, z) = t 13 print x, y, z #3, 2, 1 14 a = 1 15 b = 2 16 b, a = a, b 17 print a, b # 2 1 18 19 dic = {'a':1, 'b':2, t:3} 20 print dic #{'a': 1, 'b': 2, (3, 2, 1): 3}