python(19)- 列表生成式和生成器表达式练习Ⅰ

列表表达式

程序一:

常规写法:

egg_list=[]

for i in range(100):
    egg_list.append('egg%s' %i)
print(egg_list)

列表表达式写法:

l=['egg%s' %i for i in range(100) if i > 0]  #列表写法:[命令+循环语句]。'egg%s' %i 这句话在列表中,所以不用append命令写入列表中
print(l)

 

程序二:

常规写法:

l=[1,2,3,4]
s='hello'
l1=[]
for num in l:
    for s1 in s:
        t=(num,s1)
        l1.append(t)
print(l1)

列表表达式写法:

l1=[(num,s1) for num in l if num > 0 for s1 in s]  #if num >0 这句判断可以去掉
print(l1)

 

程序三:

常规写法:

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)

列表表达式写法:

g=os.walk('F:\\xuyaping')
l1=['%s\\%s' %(i[0],j) for i in g for j in i[-1]]
print(l1)

 

 

生成器表达式

相比列表表达式,只不过将[]换成了(),更加省内存。

程序一:

列表表达式写法:

l=['egg%s' %i for i in range(10000)]
print(l)

生成器表达式写法:

g=l=('egg%s' %i for i in range(10000))
print(g)
print(next(g))
print(next(g))
for i in g:
    print(i)

 

程序二:

常规写法:

f=open('a.txt')
l=[]
f.seek(0) #光标移动到文档首行首位 for line in f: line=line.strip() l.append(line) print(l)

列表表达式写法:

f=open('a.txt')
f.seek(0)
l1=[line.strip() for line in f]
print(l1)

生成器表达式写法:

f=open('a.txt')
f.seek(0)
g=(line.strip() for line in f)
print(g)
print(next(g)) 

  

程序三:

生成器表达式写法:

f=open('a.txt')
g=(line.strip() for line in f)  #g为迭代器

l=list(g)   #list(可迭代对象),迭代取出g中的所有内容 
print(l)
---->['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中的所有元素相加 
 1                  asdfasdfasdfasdfasdf
 2      123123123123
 3      123123123123
 4      123123123123
 5      123123123123
 6      123123123123
 7      123123123123
 8      123123123123
 9      123123123123
10      123123123123
11      123123123123
12      123123123123
13      123123123123
14      123123123123
15      123123123123
16      123123123123
17      123123123123
18      123123123123
19      123123123123
20      123123123123
21 123123123123
22 
23 asdfasdfasdfasdf
a.txt

 

程序四:

常规方法:

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]

生成器表达式写法:

f=open('b.txt')
g=(float(line.split()[-1])*float(line.split()[-2]) for line in f)

print(sum(g))
---->1096060.0

程序五:

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'}]

生成器表达式写法:

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'}
1 apple 10 3
2 tesla 1000000 1
3 mac 3000 2
4 lenovo 30000 3
5 chicken 10 3
b.txt

 

#取出单价>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)
----> <generator object <genexpr> at 0x0000000001E05888> print(list(dic_g))
----> [{'name': 'tesla', 'price': '1000000', 'count': '1'}, {'name': 'lenovo', 'price': '30000', 'count': '3'}]

  

posted @ 2017-04-12 23:35  许二哈哈哈  阅读(2268)  评论(0编辑  收藏  举报