[英]How to define a list in python tkinter mainloop without changing it's value
[英]How to change value inside list without changing list (Python)
如何在需要時將列表中的值從正值更改為負值,而不更改列表本身。
例子(抱歉會有點Maya,但我認為即使是不認識Maya的人也能理解):
someList = [1.7, 18.9, -0.3]
cmds.joint(n="test", p=someList)
並且我想在這種情況下, someList = [-1.7, 18.9, -0.3]
-“ 1.7” someList = [-1.7, 18.9, -0.3]
負數。
所以我想知道是否有一種方法可以在不創建其他列表或更改現有列表的情況下進行?
someList[0] = -someList[0]
其中0是索引。 如果您不知道索引而只是說1.7
indx = someList.index(1.7)
someList[indx] = -someList[indx]
您可以重新初始化相同的索引
someList[0]=-someList[0]
如果您不想更改someList
,可以嘗試以下操作:
someList = [1.7, 18.9, -0.3]
cmds.joint(n="test", p=[-someList[0]] + someList[1:])
它使1.7為負數,並將其連接到someList
的其余部分。
這是用於列表理解的工作。 乘以(-1):
for x in [[x*-1] for x in someList]:
print(x)
輸出:
[-1.7]
[-18.9]
[0.3]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.