# 用列表推导式做下列小题
# 1.过滤掉长度小于3的字符串列表,并将剩下的转换成大写字母
l1 = ['string', 'str', 'st']
li = [i.upper() for i in l1 if len(i) < 3]
# 2.求(x,y)其中x是0-5之间的偶数,y是0-5之间的奇数组成的元祖列表
li = [(x, y) for x in range(0, 6, 2) for y in range(1, 6, 2)]
print(li)
# 3.求M中3,6,9组成的列表M = [[1,2,3],[4,5,6],[7,8,9]]
M = [[1,2,3],[4,5,6],[7,8,9]]
li = [i[2] for i in M]
print(li)
# 4.求出50以内能被3整除的数的平方,并放入到一个列表中。
li = [i ** 2 for i in range(1, 50) if i % 3 == 0]
print(li)
# 5.构建一个列表:['python1期', 'python2期', 'python3期', 'python4期', 'python6期', 'python7期', 'python8期', 'python9期', 'python10期']
li = [f'python{i}期' for i in range(1, 11)]
print(li)
# 6.构建一个列表:[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
li = [(i, i + 1) for i in range(6)]
print(li)
# 7.构建一个列表:[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
li = [i for i in range(0, 20, 2)]
print(li)
# 8.有一个列表l1 = ['alex', 'WuSir', '老男孩', '太白']将其构造成这种列表['alex0', 'WuSir1', '老男孩2', '太白3']
l1 = ['alex', 'WuSir', '老男孩', '太白']
li = [l1[i] + str(i) for i in range(len(l1))]
print(li)
# 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]]
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}],
}
li = [[x['Values'][i]['timestamp'], x['Values'][i]['values']] for i in range(len(x['Values']))]
print(li)
# 10.用列表完成笛卡尔积
# 什么是笛卡尔积? 笛卡尔积就是一个列表,列表里面的元素是由输入的可迭代类型的元素对构成的元组,因此笛卡尔积列表的长度等于输入变量的长度的乘积。
magnitude_1 = input('PLS input magnitude 1:\n').split()
magnitude_2 = input('PLS input magnitude 2:\n').split()
cartesian = [(x, y) for x in magnitude_1 for y in magnitude_2]
print(cartesian)
# 11. 构建一个列表,列表里面是三种不同尺寸的T恤衫,每个尺寸都有两个颜色(列表里面的元素为元组类型)。
# colors = ['black', 'white']
# sizes = ['S', 'M', 'L']
colors = ['black', 'white']
sizes = ['S', 'M', 'L']
li = [(color, size) for color in colors for size in sizes]
print(li)
# 12. 构建一个列表,列表里面的元素是扑克牌除去大小王以后,所有的牌类(列表里面的元素为元组类型)。
# l1 = [('A','spades'),('A','diamonds'), ('A','clubs'), ('A','hearts')......('K','spades'),('K','diamonds'), ('K','clubs'), ('K','hearts') ]
l1 = ['A'] + [str(i) for i in range(2, 11)] + list('JQK')
l2 = ['spades', 'diamonds', 'clubs', 'hearts']
li = [(l1[i], l2[j]) for i in range(len(l1)) for j in range(len(l2))]
print(li)
# 13.简述一下yield 与yield from的区别。
# yield 定义生成器函数
# yield from 也是定义生成器函数,但是它可以将列表变成迭代器返回
# 14.看下面代码,能否对其简化?说说你简化后的优点?
# def chain(*iterables):
# for it in iterables:
# for i in it:
# yield i
#
#
# g = chain('abc', (0, 1, 2))
# print(list(g)) # 将迭代器转化成列表
# 优化内层循环,提高效率
def chain(*iterables):
for it in iterables:
yield from it
g = chain('abc', (0, 1, 2))
print(list(g))
# 15.看代码求结果(面试题):
# v = [i % 2 for i in range(10)]
# print(v)
# v = (i % 2 for i in range(10))
# print(v)
# for i in range(5):
# print(i)
# print(i)
# result:
# [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
# <generator object <genexpr> at 0x000002A8A63CD0F8>
# 0
# 1
# 2
# 3
# 4
# 4
# 16.看代码求结果:(面试题)***
def demo():
for i in range(4):
yield i
g = demo() # <generator object demo at 0x000001F272E146D0>
g1 = (i for i in g) # <generator object <genexpr> at 0x0000021B6AB8D0F8>
g2 = (i for i in g1) # <generator object <genexpr> at 0x0000023A615547D8>
print(list(g1)) # print(list((i for i in demo())))
print(list(g2)) # generator 'g1' StopIteration
# result:
# [0, 1, 2, 3]
# []
# 17.看代码求结果:(面试题)*****
def add(n, i):
return n + i
def test():
for i in range(4):
yield i
g = test()
for n in [1, 10]:
g = (add(n, i) for i in g)
print(list(g))
# result:
# [20, 21, 22, 23]
# 题目可以理解为:
# g = test()
# n = 1
# g = (add(n, i) for i in g)
# n = 10
# g = (add(n, i) for i in g)
# print(list(g)) # list((add(10, i) for i in (add(10, i) for i in test))))
# 这时候才开始执行g迭代器两次,可是这时候n已经定义为10,所以相当于在n为10时读取迭代器