码迷,mamicode.com
首页 > 其他好文 > 详细

列表生成器

时间:2018-05-29 00:13:52      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:oat   user   split   charm   price   os.walk   pycha   声明式   imp   

列表生成器

# # egg_list=[]
#
# # for i in range(50):
# #     egg_list.append("egg%s" %i)
# #
# # print(egg_list)
#
# #
要输出跟上面一样的结果,可以:
# l=[‘egg%s‘ %i for i in range(100)]
# print(l)
#
# # 上面跟三元表达式很像
# name="jensen1"
# res="SB" if name=="jensen" else "wefewf"
# print(res)
#
# # i要大于50才执行
# l=[‘egg%s‘ %i for i in range(100) if i>50]
# print(l)
#
# aa=[1,2,3,4]
# s=‘hello‘
#
# l=[(num,s1) for num in aa for s1 in s]
# print(l)
#
# # 上面等同于
# l=[]
# for num in aa:
#     for s1 in s:
#         t=(num,s1)
#         l.append(t)
#
# print(l)
#
# # 当大于2时才执行之后的:
# l=[(num,s1) for num in aa if num>2 for s1 in s]
# print(l)

import os
g=os.walk(‘c:\\egon‘)
file_path=[]
for i in g:
    for j in i[-1]:
        ab=‘%s\\%s‘ %(i[0],j)
        file_path.append(ab)

print(file_path)

g=os.walk(‘c:\\egon‘)
g=[‘%s\\%s‘ %(i[0],j)  for i in g for j in i[-1]]
print(g)

 

 

# 100只蛋放在列表很占内存,所以把中括号变小括号,于是变生成器
# l=[‘egg%s‘ %i for i in range(100)]
# print(l)
#

# l=(‘egg%s‘ %i for i in range(100))
# print(l)
# print(next(l))
# 优点:省内存
#
# a=0
# aa=[]
# with open(r‘C:\Users\user\PycharmProjects\py_fullstack_s4\20180407\c.txt‘,‘r‘) as f:
#     for line in f:
#         line=line.strip() #去空格回车
#         aa.append(line)
#
# print(aa)
#
# # 如果文件内容太多,上面的程序就不行了
# f=open(‘c.txt‘)
# f.seek(0)
# bb=[line.strip()  for line in f]
# print(bb)
#
# f.seek(0)
# bb=(line.strip()  for line in f)
# print(bb)
# print(next(bb))
# l=list(bb)
# print(l)

# 生成器表达式
# bb.txt
# apple 10 3
# tesla 100000 1
# mac 3000 2
# lenovo 30000 3
# chicken 10 3

# print(sum([1,2,3,4]))
#
# nums=(i for i in range(5))
# print(sum(nums))

# money=[]
# with open("bb.txt","r") as f:
#     for line in f:
#         goods=line.split()
#         res=float(goods[-1])*float(goods[-2])
#         money.append(res)
#
# print(sum(money))
#
# f=open("bb.txt")
# g=(float(line.split()[-1])*float(line.split()[-2]) for line in f)
# print(sum(g))

# res=[]
# with open("bb.txt") as f:
#     for line in f:
#         l=line.split()
#         d={}
#         d[‘name‘]=l[0]
#         d[‘price‘] = l[1]
#         d[‘count‘] = l[2]
#         res.append(d)
#
# print(res)

# with open(‘bb.txt‘) as f:
#     res=(line.split() for line in f)
#     print(res)
#     dis_g=({‘name‘:i[0],‘price‘:i[1],‘count‘:i[2]} for i in res)
#     print(dis_g)
#     apple=next(dis_g)
#     print(apple[‘count‘])


# money1=next(money)


with open(‘bb.txt‘) as f:
    res=(line.split() for line in f)
    dic_g=({‘name‘:i[0],‘price‘:i[1],‘count‘:i[2]} for i in res if float(i[1])>10000)
    # print(next(dic_g))
   
for i in dic_g:
        print(i)

声明式编程
# 列表解析
l=[expression for item1 in iterable1 if condition1
              for item2 in iterable2 if condition2
              for item3 in iterable3 if condition3
   ]

# 生成器表达式
g=(expression for item1 in iterable1 if condition1
              for item2 in iterable2 if condition2
              for item3 in iterable3 if condition3
    )

 


 

列表生成器

标签:oat   user   split   charm   price   os.walk   pycha   声明式   imp   

原文地址:https://www.cnblogs.com/jensenxie/p/9102819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!