2019/10/22 23:09

# 列表解析式

#不用列表解析式
l1 = list(range(10))
l2 = []
for i in l1:
l2.append((i+1)**2)
print(l2)

#用列表解析式
l2 = [(i+1)**2 for i in range(10)]
print(l2)
print(type(l2))

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
<class 'list'>

#不用列表解析
even = []
for i in range(10):
if i%2==0:
even.append(i)
print(even)

#用列表解析
[i for i in range(10) if i%2==0]结果为：
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]

newlist = [print(i) for i in range(10)]
print(newlist)

0
1
2
3
4
5
6
7
8
9
[None, None, None, None, None, None, None, None, None, None]

[i for i in range(20) if i%2==0 elif i%3==0]

File "<ipython-input-110-8aea5f5e5abc>", line 1
[i for i in range(20) if i%2==0 elif i%3==0]
^
SyntaxError: invalid syntax

## 列表解析式进阶

[expr for item in iterable if cond1 if cond2] 这等价于：

ret = []
for item in iterable:
if cond1:
if cond2:
ret.append(expr)

[i for i in range(20) if i%2==0 and i%3==0]

[0, 6, 12, 18]

[i for i in range(20) if i%2==0 if i%3==0]

[0, 6, 12, 18]

[expr for i in iterable1 for j in iterable2 ] 等价于：

ret = []
for i in iterable1:
for j in iterable2:
ret.append(expr)

[(x, y) for x in 'abcde' for y in range(3)]

[('a', 0),
('a', 1),
('a', 2),
('b', 0),
('b', 1),
('b', 2),
('c', 0),
('c', 1),
('c', 2),
('d', 0),
('d', 1),
('d', 2),
('e', 0),
('e', 1),
('e', 2)]

[[x, y] for x in 'abcde' for y in range(3)]

[['a', 0],
['a', 1],
['a', 2],
['b', 0],
['b', 1],
['b', 2],
['c', 0],
['c', 1],
['c', 2],
['d', 0],
['d', 1],
['d', 2],
['e', 0],
['e', 1],
['e', 2]]

[{x: y} for x in 'abcde' for y in range(3)]

[{'a': 0},
{'a': 1},
{'a': 2},
{'b': 0},
{'b': 1},
{'b': 2},
{'c': 0},
{'c': 1},
{'c': 2},
{'d': 0},
{'d': 1},
{'d': 2},
{'e': 0},
{'e': 1},
{'e': 2}]
[(i,j) for i in range(7) if i>4 for j in range(20,25) if j>23]

[(i,j) for i in range(7) for j in range(20,25) if i>4 if j>23]

[(i,j) for i in range(7) for j in range(20,25) if i>4 and j>23]

[(5, 24), (6, 24)]

1. 返回1-10平方的列表
2. 有一个列表lst = [1,4,9,16,2,5,10,15]，生成一个新列表，要求新列表元素是lst相邻2项的和
3. 打印九九乘法表
#返回1-10平方的列表

[i**2 for i in range(1,11)]

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

#有一个列表lst = [1,4,9,16,2,5,10,15]，生成一个新列表，要求新列表元素是lst相邻2项的和
lst = [1,4,9,16,2,5,10,15]
[lst[i]+lst[i+1] for i in range(len(lst)-1)]

[5, 13, 25, 18, 7, 15, 25]

#打印九九乘法表
print("\n".join(["".join(["%s*%s=%-3s" %(x,y,y*x) for x in range(1,y+1)]) for y in range(1,10)]))

1*1=1
1*2=2  2*2=4
1*3=3  2*3=6  3*3=9
1*4=4  2*4=8  3*4=12 4*4=16
1*5=5  2*5=10 3*5=15 4*5=20 5*5=25
1*6=6  2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7  2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8  2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9  2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

[print("{}*{}={:<3}{}".format(j,i,i*j,"\n" if i==j else ""),end="") for i in range(1,10) for j in range(1,i+1)]

1*1=1
1*2=2  2*2=4
1*3=3  2*3=6  3*3=9
1*4=4  2*4=8  3*4=12 4*4=16
1*5=5  2*5=10 3*5=15 4*5=20 5*5=25
1*6=6  2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7  2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8  2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9  2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
[None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None]
import random

["{:04}.{}".format(n,"".join([random.choice(bytes(range(97,123)).decode()) for _ in range(10)])) for n in range(1,101)]

import random
["{:04}.{}".format(i,"".join([chr(random.randint(97,122)) for j in range(10)])) for i in range(1,101)]

import string
["{:>04}.{}".format(i,"".join((random.choice(string.ascii_lowercase)) for _ in range(0,10))) for i in range(1,101)]

0
0 收藏

0 评论
0 收藏
0