Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 括号内的循环是什么_Python_Generator Expression - Fatal编程技术网

Python 括号内的循环是什么

Python 括号内的循环是什么,python,generator-expression,Python,Generator Expression,我添加了一些评论,希望对您有所帮助: 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) 输出: # create a list with 10 elements my_list = [1,2,3,4,5,6,7,8,9,10] # generate a list based on my_lis

我添加了一些评论,希望对您有所帮助:

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)
输出:

# 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)

这是一个元组理解。这个元组创建了一个元组,其中包含“我的_”列表中所有值大于3的项。@Mandera错了!世界上根本就不存在一个大家庭。这是一个数组,它不会创建元组。可能是一个更好的复制目标:@Georgy Cool!非常感谢。
4
5
6
7
8
9
10