Python exercise 7 - List comprehension
Let’s say I give you a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.
Code:
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
b=[i for i in a if i%2==0]
print (b)
本文通过一个具体的Python编程实例,展示了如何使用列表推导式从一个列表中筛选出所有偶数元素,形成一个新的列表。这一技巧是Python编程中高效处理数据的重要手段。

3646

被折叠的 条评论
为什么被折叠?



