Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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_List_List Comprehension - Fatal编程技术网

Python 列表理解输出

Python 列表理解输出,python,list,list-comprehension,Python,List,List Comprehension,我试图通过传递一个函数列表来理解列表理解,如下面的代码所示 def fun1(x): x.append(5) print(" In Fun 1:") print(x) return x def fun2(x): x.append(6) return x def fun3(x): x.append(7) return x int_list = [1, 2, 3, 4] funs_family = (fun1, fun2, fun3) new_list =

我试图通过传递一个函数列表来理解列表理解,如下面的代码所示

def fun1(x):
  x.append(5)
  print(" In Fun 1:")
  print(x)
  return x 

def fun2(x):
  x.append(6)
  return x

def fun3(x):
  x.append(7)
  return x

int_list = [1, 2, 3, 4]
funs_family = (fun1, fun2, fun3)

new_list = [fun(int_list) for fun in funs_family ]
print(new_list)
我期待着新清单的结果是

[1,2,3,4,5] [1,2,3,4,5,6] [1,2,3,4,5,6,7]
但实际结果是

[1,2,3,4,5,6,7] [1,2,3,4,5,6,7] [1,2,3,4,5,6,7] 

有人能解释为什么实际结果与预期结果不同吗?

在您的
fun\u系列方法中返回一个新列表,您将不会看到问题:

def fun1(x): 
    x.append(5) 
    print("In Fun 1:") 
    print(x) 
    return list(x)

def fun2(x): 
    x.append(6) 
    return list(x)

def fun3(x): 
    x.append(7) 
    return list(x)

int_list = [1, 2, 3, 4] 
funs_family = (fun1, fun2, fun3)

new_list = [fun(int_list) for fun in funs_family]
print(new_list)
>> [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7]]

在您的
fun_系列
方法中返回一个新列表,您将不会看到问题:

def fun1(x): 
    x.append(5) 
    print("In Fun 1:") 
    print(x) 
    return list(x)

def fun2(x): 
    x.append(6) 
    return list(x)

def fun3(x): 
    x.append(7) 
    return list(x)

int_list = [1, 2, 3, 4] 
funs_family = (fun1, fun2, fun3)

new_list = [fun(int_list) for fun in funs_family]
print(new_list)
>> [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7]]

这里有一个类似的例子供您参考

int_list = [1, 2, 3, 4]
funs_family = (fun1, fun2, fun3)

new_list = [int_list for fun in funs_family]
int_list.append(5)
print(new_list)

请注意,
new\u list
完全不依赖于函数,事实上它们从未被调用过。但是打印的是三次
[1,2,3,4,5]
。这是因为
new\u list
中的三个列表都指向内存中的同一个列表。因此,当您更改原件时,它会更改所有原件。

这里有一个类似的示例供您查看

int_list = [1, 2, 3, 4]
funs_family = (fun1, fun2, fun3)

new_list = [int_list for fun in funs_family]
int_list.append(5)
print(new_list)

请注意,
new\u list
完全不依赖于函数,事实上它们从未被调用过。但是打印的是三次
[1,2,3,4,5]
。这是因为
new\u list
中的三个列表都指向内存中的同一个列表。因此,当您更改原始对象时,它会更改所有对象。

因为您要添加到列表中,所以它实际上是同一个对象。最后,您将打印三次完全相同的列表。相反,您可能想做的是在函数中创建一个新列表,并向其中添加int

x = [1, 2, 3, 4]
x_copy = [i for i in x]
x_copy.append(5)

因为您要附加到列表,所以它实际上是同一个对象。最后,您将打印三次完全相同的列表。相反,您可能想做的是在函数中创建一个新列表,并向其中添加int

x = [1, 2, 3, 4]
x_copy = [i for i in x]
x_copy.append(5)

每个函数都返回
x

所以

…返回三份
x
,理解完成后,所有副本都等于
[1,2,3,4,5,6,7]

您需要的是
x
的副本,您可以使用
[:]
:

new_list = [fun(int_list)[:] for fun in funs_family ]

每个函数都返回
x

所以

…返回三份
x
,理解完成后,所有副本都等于
[1,2,3,4,5,6,7]

您需要的是
x
的副本,您可以使用
[:]
:

new_list = [fun(int_list)[:] for fun in funs_family ]

这是因为列表是可变的所有三个函数都获取对同一列表的引用,而不是副本,
new\u list
包含对该列表的三个引用。这是因为列表是可变的所有三个函数都获取对同一列表的引用,而不是副本,而
new_list
包含对该列表的三个引用。