1.day13题目
2,用列表推导式做下列小题
(1)过滤掉长度小于3的字符串列表,并将剩下的转换成大写字母
(2)求(x,y)其中x是0-5之间的偶数,y是0-5之间的奇数组成的元祖列表
(3)求M中3,6,9组成的列表M = [[1,2,3],[4,5,6],[7,8,9]]
(4)求出50以内能被3整除的数的平方,并放入到一个列表中。
(5)构建一个列表:['python1期', 'python2期', 'python3期', 'python4期', 'python6期', 'python7期', 'python8期', 'python9期', 'python10期']
(6)构建一个列表:[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
(7)构建一个列表:[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
(8)有一个列表l1 = ['alex', 'WuSir', '老男孩', '太白']将其构造成这种列表['alex0', 'WuSir1', '老男孩2', '太白3']
(9)有以下数据类型:
x = {
'name':'alex',
'Values':[{'timestamp':1517991992.94,
'values':100,},
{'timestamp': 1517992000.94,
'values': 200,},
{'timestamp': 1517992014.94,
'values': 300,},
{'timestamp': 1517992744.94,
'values': 350},
{'timestamp': 1517992800.94,
'values': 280}
],}
将上面的数据通过列表推导式转换成下面的类型:
[[1517991992.94, 100], [1517992000.94, 200], [1517992014.94, 300], [1517992744.94, 350], [1517992800.94, 280]]
2.题目详解
点击查看详细内容
2-1 lst = ['Alex','Jack','Anna','Jason','Pako','aj'] ll = [i.upper() for i in lst if len(i)>3] print(ll)
2-2
lst = [(i,j) for i in range(5) if i%20 for j in range(5) if j%21]
print(lst)lst = [(i,j) for i in range(0,5,2) for j in range(1,5,2)]
print(lst)2-3
M = [[1,2,3],[4,5,6],[7,8,9]]
lst = [i[2] for i in M]
print(lst)M = [[i-2,i-1,i] for i in [3,6,9]]
print(M)2-4
lst = [i**2 for i in range(51) if i%3==0]
print(lst)2-5
lst = ['python%s期'%i for i in range(1,11)]
print(lst)2-6
lst = [(i,el) for i,el in enumerate(range(1,7))]
print(lst)lst = [(i,i+1) for i in range(6)]
print(lst)2-7
lst = [i for i in range(0,20,2)]
print(lst)2-8
l1 = ['alex', 'WuSir', '老男孩', '太白']
new_lst = ['%s%s'%(el,i) for i,el in enumerate(l1)]
print(new_lst)
2-9
x={
'name':'alex',
'Values':[{'timestamp':1517991992.94,'values':100},
{'timestamp':1517992000.94,'values':200},
{'timestamp':1517992014.94,'values':300},
{'timestamp':1517992744.94,'values':350},
{'timestamp':1517992800.94,'values':280},
]}
lst = [[i['timestamp'],i['values']] for i in x['Values']]
print(lst)