列表解析式
列表解析式的语法:[返回值 for 元素 in 可迭代对象 if 条件],使用中括号[],内部是for循环,if条件语句可选,返回一个新的列表 。
列表解析式是一种语法糖,编译器会优化,不会因为简写而影响效率,反而因优化提高了效率。减少程序员工作量,减少出错。简化了代码,但可读性增强。
比如要生成一个列表,元素0~9,对每一个元素自增1后求平方返回新列表,下面看不用列表解析式和用列表解析式的代码。
#不用列表解析式
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'>
可以看到可读性更高,同时也减少了代码量。比如如果要获取10以内的偶数。
#不用列表解析
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]
结果为:
有这样的赋值语句newlist = [print(i) for i in range(10)],请问newlist的元素打印出来是什么?
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]
应该注意if条件后面不能在跟elif,比如获取20以内的偶数,如果数是3的倍数也打印,下面的代码是不行的。
[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)
比如20以内,既能被2整除又能被3整除的数。
[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)
相当于嵌套for循环。
[(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-10平方的列表
- 有一个列表lst = [1,4,9,16,2,5,10,15],生成一个新列表,要求新列表元素是lst相邻2项的和
- 打印九九乘法表
- "0001.abadicddws" 是ID格式,要求ID格式是以点号分割,左边是4位从1开始的整数,右边是10位随机小写英文字母。请依次生成前100个ID的列表
#返回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)]
以上三种方法都能生成