标签:item pytho 匿名 mat lte line input span items
1 #!/usr/bin/env python 2 #-*- encoding: utf-8 -*- 3 4 ‘‘‘ 5 def foo(name): 6 print name, ‘去砍柴‘ 7 8 foo(‘yangshanlei:‘) 9 foo(‘xukequn:‘) 10 ‘‘‘ 11 12 ‘‘‘ 13 def login(usrname): 14 if username == ‘alex‘: 15 print ‘登录成功‘ 16 else: 17 print ‘登录失败‘ 18 19 def detail(user): 20 print ‘xxxxxxxxxxx‘ 21 22 if __name__ == ‘__main__‘: 23 user = raw_input(‘输入用户名:‘) 24 res = login(user) #检查用户是否合法 25 if res == ‘登录成功‘: 26 detail(user) # 27 else: 28 print ‘没奖金了‘ 29 ‘‘‘ 30 ‘‘‘ 31 def foo(name): 32 print name, ‘去砍柴‘ 33 34 def foo(name): 35 print name, ‘去吃饭‘ 36 37 def foo(name): 38 print name, ‘去xxx‘ 39 ‘‘‘ 40 41 ‘‘‘ 42 def foo(name,action): 43 print name, ‘去‘,action 44 45 foo(‘yang‘, ‘砍柴‘) 46 foo(‘shan‘, ‘xx‘) 47 foo(‘lei‘, ‘吃饭‘) 48 ‘‘‘ 49 50 ‘‘‘ 51 def foo(name,action=‘砍柴‘): 52 print name, ‘去‘,action 53 54 foo(‘yang‘, ‘砍柴‘) 55 foo(‘shan‘, ‘xx‘) 56 foo(‘lei‘, ‘吃饭‘) 57 foo(‘xu‘) 58 59 #默认参数放在后面 action=‘砍柴‘ 60 ‘‘‘ 61 ‘‘‘ 62 def foo(name,action=‘砍柴‘,where=‘北京‘): 63 print name,‘去‘,action,where 64 65 foo(‘yang‘, ‘砍柴‘).split() 66 foo(‘shan‘, ‘xx‘) 67 foo(‘lei‘, ‘吃饭‘) 68 foo(‘xu‘) 69 foo(‘kequn‘) 70 ‘‘‘ 71 72 ‘‘‘ 73 def show(**kargs): 74 for item in kargs.items(): 75 print item 76 77 user_dict = {‘k1‘:123, ‘k2‘:456} 78 #show(name=‘yangshanlei‘, age =[1,2,3,4]) 79 80 show (**user_dict) 81 ‘‘‘ 82 83 ‘‘‘ 84 #yield 85 86 def foo(): 87 yield 1 88 yield 2 89 90 re = foo() 91 92 for i in re: 93 print i 94 ‘‘‘ 95 96 ‘‘‘ 97 def alexreadlines(): 98 seek = 0 99 while True: 100 with open(‘D:/123/test.txt‘,‘r‘) as f: 101 f.seek(seek) 102 data = f.readline() 103 if data: 104 seek = f.tell() 105 yield data 106 else: 107 return 108 109 for item in alexreadlines(): 110 print item.strip(‘\n‘) 111 ‘‘‘ 112 ‘‘‘ 113 #lambda 就是匿名函数, 只能调用一次 114 temp = lambda x, y: x+y 115 print temp(4,10) 116 117 temp1 = lambda x,y,z: x+y+z 118 print temp1(4,10,23) 119 ‘‘‘ 120 121 ‘‘‘ 122 [i*2 for i in range(10)] #1 123 124 for i in range(10): 125 print i 126 127 map(lambda x:x*2, range(10)) #2 128 ‘‘‘ 129 ‘‘‘ 130 li = [‘手机‘,‘汽车‘,‘房‘] 131 for item in li: 132 print item 133 134 for item in enumerate(li): 135 print item[0], item[1] 136 137 for item in enumerate(li,1): 138 print item[0], item[1] 139 ‘‘‘ 140 141 ‘‘‘ 142 s = ‘i am {0},{1}‘ 143 print s.format(‘alex‘,‘yang‘) 144 ‘‘‘ 145 ‘‘‘ 146 print map(lambda x:x+1,[1,2,3]) 147 [2, 3, 4] #结果 148 ‘‘‘ 149 150 ‘‘‘ 151 #方法一 152 li = [11,22,33] 153 temp = [] 154 for item in li: 155 temp.append(item +100) 156 print temp 157 158 #方法二 159 def foo(arg): 160 return arg + 100 161 162 li = [11,22,33] 163 temp = [] 164 for item in li: 165 temp.append(foo(item)) 166 167 print temp 168 169 #方法三 170 print map(lambda x:x+100,[11,22,33 ]) 171 ‘‘‘ 172 173 ‘‘‘ 174 #filter 过滤条件 175 li = [11,22,33] 176 def foo(arg): 177 if arg < 22: 178 return True 179 else: 180 return False 181 temp = filter(foo,li) 182 print temp 183 ‘‘‘ 184 185 ‘‘‘ 186 #reduce 累加 累乘。。。 必须传2个值 187 li = [11,22,33] 188 print reduce(lambda x,y:x+y, li) 189 ‘‘‘
标签:item pytho 匿名 mat lte line input span items
原文地址:http://www.cnblogs.com/augustyang/p/6209765.html