gpt4 book ai didi

python - 修改大型列表,而python中没有任何循环

转载 作者:行者123 更新时间:2023-12-04 13:01:48 24 4
gpt4 key购买 nike

我的 list 是:

a=[1,2,3,4]

现在,我希望我的列表为:
a=[-1,-2,-3,-4]

如何在不使用任何循环的情况下以这种方式更改列表?

更新:这可能是一个很大的列表,大约10000个元素。

最佳答案

使用Python的map功能

a[:] = map(lambda x: -x, a)

这是上面链接中对map函数的描述:

map(function, iterable, ...)
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

关于python - 修改大型列表,而python中没有任何循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8437024/

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