1. 程式人生 > >python-----列表生成式和列表生成器表達

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(r'E:\test') 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(r'E:\test')
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'}]