[英]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.