gpt4 book ai didi

python - python 中扩展运算符的非就地版本

转载 作者:行者123 更新时间:2023-12-01 11:36:46 27 4
gpt4 key购买 nike

什么是简洁的 pythonic 非就地版本的 extend?我写腻了

x = [ 1,2,3 ]
x.extend([4,5])
f(x)

只想:

 f( x.extend( [4,5], inplace=False ) )

或者其他什么

最佳答案

只使用 + 运算符怎么样

def f(l):
return l

x = [1,2,3]

>>> id(x)
48474312 # Note the id

>>> x = f(x + [4,5])
[1, 2, 3, 4, 5]

>>> id(x)
43637016 # Different id, that means not in-place

关于python - python 中扩展运算符的非就地版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26125838/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com