g = os.walk(list_path)
# b=[ j for j in g]
self.img_files = ['%s\\%s' % (i[0], j) for i in g if i[0].endswith('JPEGImages') for j in i[-1] if j.endswith('jpg')]
列表表达式
程序一:
常规写法:
1 2 3 4 5 |
egg_list = [] for i in range ( 100 ): egg_list.append( 'egg%s' % i) print (egg_list) |
列表表达式写法:
1 2 |
l = [ 'egg%s' % i for i in range ( 100 ) if i > 0 ] #列表写法:[命令+循环语句]。'egg%s' %i 这句话在列表中,所以不用append命令写入列表中 print (l) |
程序二:
常规写法:
1 2 3 4 5 6 7 8 |
l = [ 1 , 2 , 3 , 4 ] s = 'hello' l1 = [] for num in l: for s1 in s: t = (num,s1) l1.append(t) print (l1) |
列表表达式写法:
1 2 |
l1 = [(num,s1) for num in l if num > 0 for s1 in s] #if num >0 这句判断可以去掉 print (l1) |
程序三:
常规写法:
1 2 3 4 5 6 7 8 9 |
import os
#查看xuyaping文件夹所有的绝对路径 g = os.walk( 'F:\\xuyaping' ) file_path_list = [] for i in g: # print(i) for j in i[ - 1 ]: file_path_list.append( '%s\\%s' % (i[ 0 ],j)) print (file_path_list) |
列表表达式写法:
1 2 3 |
g = os.walk( 'F:\\xuyaping' ) l1 = [ '%s\\%s' % (i[ 0 ],j) for i in g for j in i[ - 1 ]] print (l1) |
生成器表达式
相比列表表达式,只不过将[]换成了(),更加省内存。
程序一:
列表表达式写法:
1 2 |
l = [ 'egg%s' % i for i in range ( 10000 )] print (l) |
生成器表达式写法:
1 2 3 4 5 6 |
g = l = ( 'egg%s' % i for i in range ( 10000 )) print (g) print ( next (g)) print ( next (g)) for i in g: print (i) |
程序二:
常规写法:
1 2 3 4 5 6 |
f = open ( 'a.txt' ) l = [] f.seek( 0 ) #光标移动到文档首行首位 for line in f: line = line.strip() l.append(line) print (l) |
列表表达式写法:
1 2 3 4 |
f = open ( 'a.txt' ) f.seek( 0 ) l1 = [line.strip() for line in f] print (l1) |
生成器表达式写法:
1 2 3 4 5 |
f = open ( 'a.txt' ) f.seek( 0 ) g = (line.strip() for line in f) print (g) print ( next (g)) |
程序三:
生成器表达式写法:
1 2 3 4 |
f = open ( 'a.txt' ) g = (line.strip() for line in f) #g为迭代器 l = list (g) #list(可迭代对象),迭代取出g中的所有内容 print(l) |
1 2 3 4 5 6 7 |
- - - - >[ 'asdfasdfasdfasdfasdf' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , '123123123123' , ' ', ' asdfasdfasdfasdf'] nums_g = (i for i in range ( 3 )) # print(sum([1,2,3,4])) print ( sum (nums_g)) #sum(可迭代对象),迭代将g中的所有元素相加 |
a.txt
程序四:
常规方法:
1 2 3 4 5 6 7 |
money_l = [] with open ( 'b.txt' ) as f: for line in f: goods = line.split() res = float (goods[ - 1 ]) * float (goods[ - 2 ]) money_l.append(res) print (money_l)
- - - - >[ 30.0 , 1000000.0 , 6000.0 , 90000.0 , 30.0 ] |
生成器表达式写法:
1 2 3 4 |
f = open ( 'b.txt' ) g = ( float (line.split()[ - 1 ]) * float (line.split()[ - 2 ]) for line in f) print ( sum (g))
- - - - > 1096060.0 |
程序五:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
res = [] with open ( 'b.txt' ) as f: for line in f: # print(line) l = line.split() # print(l) d = {} d[ 'name' ] = l[ 0 ] d[ 'price' ] = l[ 1 ] d[ 'count' ] = l[ 2 ] res.append(d) print (res)
- - - - >[{ 'name' : 'apple' , 'price' : '10' , 'count' : '3' }, { 'name' : 'tesla' , 'price' : '1000000' , 'count' : '1' }, { 'name' : 'mac' , 'price' : '3000' , 'count' : '2' }, { 'name' : 'lenovo' , 'price' : '30000' , 'count' : '3' }, { 'name' : 'chicken' , 'price' : '10' , 'count' : '3' }] |
生成器表达式写法:
1 2 3 4 5 6 7 8 9 10 11 |
with open ( 'b.txt' ) as f: res = (line.split() for line in f) print (res) dic_g = ({ 'name' :i[ 0 ], 'price' :i[ 1 ], 'count' :i[ 2 ]} for i in res) print (dic_g) apple_dic = next (dic_g) print (apple_dic[ 'count' ]) apple_dict = next (dic_g) print (apple_dict)
- - - - >{ 'name' : 'tesla' , 'price' : '1000000' , 'count' : '1' } |
b.txt
1 2 3 4 5 6 7 |
#取出单价>10000 with open ( 'b.txt' ) as f: res = (line.split() for line in f) # print(res) dic_g = ({ 'name' :i[ 0 ], 'price' :i[ 1 ], 'count' :i[ 2 ]} for i in res if float (i[ 1 ]) > 10000 ) print (dic_g)
- - - - > object at 0x0000000001E05888 > print ( list (dic_g))
- - - - > [{ 'name' : 'tesla' , 'price' : '1000000' , 'count' : '1' }, { 'name' : 'lenovo' , 'price' : '30000' , 'count' : '3' }] |