![](/img/trans.png)
[英]How to sort list elements by specific indexes of specific values in python
[英]How to replace values at specific indexes of a python list?
如果我有一個清單:
to_modify = [5,4,3,2,1,0]
然后聲明另外兩個列表:
indexes = [0,1,3,5]
replacements = [0,0,0,0]
如何將to_modify
的元素作為 index 的indexes
,然后將to_modify
中的相應元素設置為replacements
,即運行后, indexes
應該是[0,0,3,0,1,0]
。
顯然,我可以通過 for 循環做到這一點:
for ind in to_modify:
indexes[to_modify[ind]] = replacements[ind]
但是還有其他方法可以做到這一點嗎? 我可以以某種方式使用operator.itemgetter
嗎?
您的代碼最大的問題是它不可讀。 Python代碼規則第一,如果它不可讀,沒有人會看它足夠長的時間來從中獲取任何有用的信息。 始終使用描述性變量名稱。 差點沒抓到你代碼的bug,用好名字再看一遍,慢動作重放風格:
to_modify = [5,4,3,2,1,0]
indexes = [0,1,3,5]
replacements = [0,0,0,0]
for index in indexes:
to_modify[indexes[index]] = replacements[index]
# to_modify[indexes[index]]
# indexes[index]
# Yo dawg, I heard you liked indexes, so I put an index inside your indexes
# so you can go out of bounds while you go out of bounds.
很明顯,當您使用描述性變量名稱時,您正在使用來自自身的值來索引索引列表,這在這種情況下沒有意義。
此外,當並行迭代 2 個列表時,我喜歡使用zip
函數(如果您擔心內存消耗,也可以使用izip
,但我不是那些迭代純粹主義者之一)。 所以試試這個。
for (index, replacement) in zip(indexes, replacements):
to_modify[index] = replacement
如果您的問題僅與數字列表有關,那么我會說@steabert 有您正在尋找的那些 numpy 東西的答案。 但是,您不能將序列或其他可變大小的數據類型用作 numpy 數組的元素,因此如果您的變量to_modify
中包含類似的內容,則最好使用 for 循環來執行此操作。
numpy 具有允許您使用其他列表/數組作為索引的數組:
import numpy
S=numpy.array(s)
S[a]=m
為什么不只是:
map(s.__setitem__, a, m)
有點慢,但我認為可讀:
>>> s, l, m
([5, 4, 3, 2, 1, 0], [0, 1, 3, 5], [0, 0, 0, 0])
>>> d = dict(zip(l, m))
>>> d #dict is better then using two list i think
{0: 0, 1: 0, 3: 0, 5: 0}
>>> [d.get(i, j) for i, j in enumerate(s)]
[0, 0, 3, 0, 1, 0]
您可以使用operator.setitem
。
from operator import setitem
a = [5, 4, 3, 2, 1, 0]
ell = [0, 1, 3, 5]
m = [0, 0, 0, 0]
for b, c in zip(ell, m):
setitem(a, b, c)
>>> a
[0, 0, 3, 0, 1, 0]
它是否比您的解決方案更具可讀性或效率? 我不知道!
對於 a 中的索引:
這將導致index
采用a
元素的值,因此將它們用作索引不是您想要的。 在 Python 中,我們通過實際迭代容器來迭代容器。
“但是等等”,你說,“對於a
每個元素,我需要使用m
的相應元素。我應該如何在沒有索引的情況下做到這一點?”
簡單的。 我們將a
和m
轉換為對的列表(a 中的元素,m 中的元素),並迭代這些對。 這很容易做到 - 只需使用內置庫函數zip
,如下:
for a_element, m_element in zip(a, m):
s[a_element] = m_element
要使其按照您嘗試的方式工作,您必須獲取要迭代的索引列表。 這是可行的:例如,我們可以使用range(len(a))
。 但不要那樣做! 這不是我們在 Python 中做事的方式。 實際上,直接迭代你想要迭代的東西是一個美麗的、解放思想的想法。
那么 operator.itemgetter 呢?
在這里並不重要。 operator.itemgetter
的目的是將索引的行為變成某種東西,變成類似函數的東西(我們稱之為“可調用”),以便它可以用作回調(例如,用於排序或最小/最大操作)。 如果我們在這里使用它,我們必須在每次循環中重新調用它來創建一個新的 itemgetter,這樣我們就可以立即使用它一次並把它扔掉。 在上下文中,這只是忙碌的工作。
您可以使用字典解決它
to_modify = [5,4,3,2,1,0]
indexes = [0,1,3,5]
replacements = [0,0,0,0]
dic = {}
for i in range(len(indexes)):
dic[indexes[i]]=replacements[i]
print(dic)
for index, item in enumerate(to_modify):
for i in indexes:
to_modify[i]=dic[i]
print(to_modify)
輸出將是
{0: 0, 1: 0, 3: 0, 5: 0}
[0, 0, 3, 0, 1, 0]
elif menu.lower() == "edit":
print ("Your games are: "+str (games))
remove = input("Which one do you want to edit: ")
add = input("What do you want to change it to: ")
for i in range(len(games)) :
if str(games[i]) == str(remove) :
games[i] = str(add)
break
else :
pass
pass
為什么不這樣使用呢? 直接從它被刪除的地方替換,無論如何你可以添加數組和do .sort .reverse 如果需要
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.