列表生成器

Posted 森森2017

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了列表生成器相关的知识,希望对你有一定的参考价值。

列表生成器

# # 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
    )

 


 

以上是关于列表生成器的主要内容,如果未能解决你的问题,请参考以下文章

列表推导式生成器表达式

python-----列表生成式和列表生成器表达

列表生成式的进化版——生成器

列表生成式的进化版——生成器

列表生成器&生成器&迭代器

python笔记十(列表生成式生成器)