# python列表推导式笔试题_Python面试题之列表推导式

[[0,0,0,0,0,],[0,1,2,3,4,],[0,2,4,6,8,],[0,3,6,9,12,]]

(考察列表生成式和基本逻辑推理)

list1 =[]for in range(4):

temp=[]for j in range(5):

temp.append(j*i)

list1.append(temp)print(list1)

list1 =[[ i*j for j in range(5)] for i in range(4)]print(list1)

Python列表推导式介绍

>>>list(range(10))

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

>>>list1 = [0,1,2,3,4,5,6,7,8,9]>>>list2 =[]>>>for i inlist1:

... list2.append(i+100)

...>>>list2

[100,101,102,103,104,105,106,107,108,109]

>>>list2 = list(map(lambda x:x+100,list1))>>>list2

[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]

>>>list2 = [i+100 for i inlist1]>>>listt2

[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]

1. 只有输出表达式和输入序列的基本列表推导式

>>>list1 = [1,2,3]>>>[x*10 for x inlist1]

[10,20,30]

2. 带有条件判断的列表推导式

>>>list1 = [1,2,3]>>>[x*10 for x in list1 if x%2!=0] #把list1中的奇数乘以10

[10,30]

3.多个输入序列的列表推导式

list1 = ["A","B","C"]

list2= ["X","Y","Z"]>>>[x+y for x in list1 for y inlist2]

['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']

06-26 7686
11-05 285
12-21 78
10-10 140
08-20 165
06-01 40
06-09 2万+
04-23 100
04-19 1795
08-24 570
09-01 50
07-01 54