简体   繁体   English

如何在不更改列表的情况下更改列表内的值(Python)

[英]How to change value inside list without changing list (Python)

How do you change a value from positive to negative inside a list when you need it, without changing list itself. 如何在需要时将列表中的值从正值更改为负值,而不更改列表本身。

Example(sorry will be a bit of Maya, but i think even people who doesn't know Maya will get it) : 例子(抱歉会有点Maya,但我认为即使是不认识Maya的人也能理解):

someList = [1.7, 18.9, -0.3]
cmds.joint(n="test", p=someList)

and I want in this case, someList = [-1.7, 18.9, -0.3] - "1.7" make negative. 并且我想在这种情况下, someList = [-1.7, 18.9, -0.3] -“ 1.7” someList = [-1.7, 18.9, -0.3]负数。

So I was wondering if there is a way to make it without creating different list or changing existing one? 所以我想知道是否有一种方法可以在不创建其他列表或更改现有列表的情况下进行?

someList[0] = -someList[0]

where 0 is index. 其中0是索引。 If you don't know index and just value say 1.7 如果您不知道索引而只是说1.7

indx = someList.index(1.7)
someList[indx] = -someList[indx]

您可以重新初始化相同的索引

someList[0]=-someList[0]

If you don't want to change someList , you can try this: 如果您不想更改someList ,可以尝试以下操作:

someList = [1.7, 18.9, -0.3]
cmds.joint(n="test", p=[-someList[0]] + someList[1:])

It makes 1.7 negative and concatenates it to the rest of the someList . 它使1.7为负数,并将其连接到someList的其余部分。

This is job for list comprehension. 这是用于列表理解的工作。 Multiply by (-1) on the fly: 乘以(-1):

for x in [[x*-1] for x in someList]:
    print(x)

Output: 输出:

[-1.7]
[-18.9]
[0.3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python tkinter mainloop中定义一个列表而不改变它的值 - How to define a list in python tkinter mainloop without changing it's value 我们如何在不更改 python 上的原始列表的情况下更改列表的索引? - How do we change the indexes of a list without changing the original list on python? 更改列表内的变量的值而无需直接引用该变量 - Changing the value of a variable that is inside of a list without directly referencing the variable 如何在不删除/更改原始元素及其值的情况下将列表/数组中元素的索引更改为另一个位置/索引 - How to change the index of an element in a list/array to another position/index without deleting/changing the original element and its value 如何将字典的值更改为“在列表中设置” - How to change value of dictionary into 'set inside of list' 如何更改列表中包含的引用的值? - How to change the value of a reference contained inside a list? 更改列表Python中的值 - change value in list Python 在python中更改列表的值 - change value of list in python 如何在循环和更改列表长度内过滤Python 3中的数字列表 - how to filter a numbers list in Python 3 inside a loop & change list length 如何在python列表中的元组中识别值? - How to identify a value inside of a tuple in a python list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM