my_list = [1,2,3,4,5,6,7,8,9,10]
gen_comp = (item for item in my_list if item > 3)
for item in gen_comp:
print(item)
答案 0 :(得分:1)
我添加了一些评论,希望对您有所帮助
# create a list with 10 elements
my_list = [1,2,3,4,5,6,7,8,9,10]
# generate a list based on my_list and add only items if the value is over 3
# this is also known as tuple comprehension
# that one creates a tuple containing every item in my_list that have a value greater then 3.
gen_comp = (item for item in my_list if item > 3)
# print the new generated list gen_comp
for item in gen_comp:
print(item)
输出:
4
5
6
7
8
9
10