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

Posted 小呆丶

tags:

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

列表表达式:

程序一:

常规写法:

L = []
for x in range(1, 11):
    L.append(x * x)
print(L)
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

列表生成式写法:

L = [x * x for x in range(1, 11)]
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

#写列表生成式时,把要生成的元素x * x放到前面,后面跟for循环,就可以把list创建出来,十分有用,多写几次,很快就可以熟悉这种语法。for循环后面还可以加上if判断,这样我们就可以筛选出仅偶数的平方:

L1 = [x * x for x in range(1, 11) if x % 2 == 0]
print(L1)
#[4, 16, 36, 64, 100]

程序二:

常规写法:

list = [1,2,3,4]
s = hello
list1 = []
for num in list:
    for s1 in s:
        t=(num,s1)
        list1.append(t)
print(list1)
#[(1, ‘h‘), (1, ‘e‘), (1, ‘l‘), (1, ‘l‘), (1, ‘o‘), (2, ‘h‘), (2, ‘e‘), (2, ‘l‘), (2, ‘l‘), (2, ‘o‘), (3, ‘h‘), (3, ‘e‘), (3, ‘l‘), (3, ‘l‘), (3, ‘o‘), (4, ‘h‘), (4, ‘e‘), (4, ‘l‘), (4, ‘l‘), (4, ‘o‘)]

列表生成式写法:

list1 = [(num,s1) for num in list for s1 in s]
print(list1)
#[(1, ‘h‘), (1, ‘e‘), (1, ‘l‘), (1, ‘l‘), (1, ‘o‘), (2, ‘h‘), (2, ‘e‘), (2, ‘l‘), (2, ‘l‘), (2, ‘o‘), (3, ‘h‘), (3, ‘e‘), (3, ‘l‘), (3, ‘l‘), (3, ‘o‘), (4, ‘h‘), (4, ‘e‘), (4, ‘l‘), (4, ‘l‘), (4, ‘o‘)]

程序三:

常规写法:

import os  # 查看test文件夹所有的绝对路径
g = os.walk(rE:	est)
file_path_list = []
for i in g:
    print(i)
    for j in i[-1]:
        file_path_list.append(%s\%s % (i[0], j))
        print(%s\%s % (i[0], j))
print(file_path_list)

列表生成式写法:

g = os.walk(rE:	est)
file_path_list = [%s\%s % (i[0], j) for i in g for j in i[-1]]
print(file_path_list)

生成器表达式:

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

程序一:

列表生成式写法:

L = [x * x for x in range(1, 11)]
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

#写列表生成式时,把要生成的元素x * x放到前面,后面跟for循环,就可以把list创建出来,十分有用,多写几次,很快就可以熟悉这种语法。for循环后面还可以加上if判断,这样我们就可以筛选出仅偶数的平方:

L1 = [x * x for x in range(1, 11) if x % 2 == 0]
print(L1)
#[4, 16, 36, 64, 100]

生成器表达式写法:

L11 = (x * x for x in range(1, 11))
print(L11)
print(next(L11))
print(next(L11))
print(next(L11))
for L in L11:
    print(L11)
L12 = (x * x for x in range(1, 11) if x % 2 == 0)
print(L12)
print(next(L12))
print(next(L12))
print(next(L12))
for L in L12:
    print(L12)

程序二:

常规写法:

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

列表表达式写法:

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中的所有元素相加
技术分享图片
asdfasdfasdfasdfasdf
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
     123123123123
123123123123

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‘}
技术分享图片
apple 10 3
tesla 1000000 1
mac 3000 2
lenovo 30000 3
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‘}]

 

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

python(列表推导式和生成器表达式)

Python:三元表达式列表推导式和生成器表达式

Python:三元表达式列表推导式和生成器表达式

列表生成式和三元表达式(python3入门)

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

Python列表生成式和生成器