python里面列表可以同时删除吗_在python中从列表中删除项,同时对其进行迭代
本問題已經有最佳答案,請猛點這里訪問。
我正在為錦標賽應用程序編寫循環算法。
當玩家數量為奇數時,我將'DELETE'添加到玩家列表中,但稍后,當我想從包含'DELETE'的日程表列表中刪除所有項目時,我不能——總是留下一個。請看一下代碼——問題很簡單,我想是關于列表的;我就是看不到。
"""
Round-robin tournament:
1, 2, 3, 4, | 5, 6, 7, 8 ? ?=> ?1, 2, 3, 4 ?=> rotate all but 1 => ?1, 5, 2, 3 ?=> repeat => ? ?1, 6, 5, 2 ?...
5, 6, 7, 8 ? ? ? ? ? ? ?6, 7, 8, 4 ? ? ? ? ?7, 8, 4, 3
in every round pick l1[0] and l2[0] as first couple, after that l1[1] and l2[1]...
"""
import math
lst = []
schedule = []
delLater = False
for i in range(3): ? ? ? ? ? ? ? ? ?#make list of numbers
lst.append(i+1)
if len(lst) % 2 != 0: ? ? ? ? ? ? ? #if num of items is odd, add 'DELETE'
lst.append('DELETE')
delLater = True
while len(schedule) < math.factorial(len(lst))/(2*math.factorial(len(lst) - 2)): #!(n)/!(n-k)
mid = len(lst)/2
l1 = lst[:mid]
l2 = lst[mid:]
for i in range(len(l1)):
schedule.append((l1[i], l2[i])) ? ? ? ? #add lst items in schedule
l1.insert(1, l2[0]) ? ? ? ? ? ? #rotate lst
l2.append(l1[-1])
lst = l1[:-1] + l2[1:]
if delLater == True: ? ? ? ? ? ? ? ?#PROBLEM!!! One DELETE always left in list
for x in schedule:
if 'DELETE' in x:
schedule.remove(x)
i = 1
for x in schedule:
print i, x
i+=1
schedule[:] = [x for x in schedule if 'DELETE' not in x]
請參閱有關在迭代列表時從列表中刪除的其他問題。
+1用于使用[:]僅修改同一列表對象。
為什么要使用就地分配?@麥晉桁可能沒有什么好處。它短暫地節省了一點內存,而且速度慢了一點。很可能兩者都不重要。
@AGF:只需盡可能少地修改現有代碼。就個人而言,我只需要在任何地方使用生成器,或者編寫一個不需要偽條目的算法,但這不是問題所在。
@AGF可讀性。引入另一個列表對象將引入在讀取代碼時要記住的另一個變量。
@迅猛龍物體身份有什么關系?您仍然可以使用相同的名稱,不需要新的變量:schedule = [x for x in schedule if 'DELETE' not in x]。
@agf:當必須保持所有綁定到列表的名稱同步時,使用就地分配。
要在遍歷列表時從列表中刪除元素,需要向后:
if delLater == True:
for x in schedule[-1::-1]):
if 'DELETE' in x:
schedule.remove(x)
更好的選擇是使用列表理解:
schedule[:] = [item for item in schedule if item != 'DELETE']
現在,你可以用schedule =代替schedule[:] =,有什么區別?一個例子是:
schedule = [some list stuff here] ?# first time creating
modify_schedule(schedule, new_players='...') # does validation, etc.
def modify_schedule(sched, new_players):
# validate, validate, hallucinate, illustrate...
sched = [changes here]
此時,modify_schedule所做的所有更改都已丟失。為什么?因為它沒有就地修改列表對象,而是將名稱sched重新綁定到一個新列表,使原始列表仍然綁定到調用方的名稱schedule。
因此,您是否使用list_object[:] =取決于您是否希望將任何其他名稱綁定到您的列表以查看更改。
在正確的設計中,def返回分配回schedule的sched,或者以一種被認為是不好的形式,但邏輯設計,您沒有通過計劃,而是使用global schedule。無論您是否使用就地分配,上面的示例設計都是錯誤的,因為在您更改schedule時,不清楚sched和schedule指的是相同的東西。
還有,你試過你的循環版本嗎?我不知道它應該如何工作,但至少有幾個錯誤。您試圖刪除列表中不知道的內容(enumerate中的索引),而您的切片值似乎沒有意義。
這不是使用slices反轉列表的正確方法,請嘗試schedule[-1::-1],或者更好的方法是reversed(schedule),因為slice無論如何都會構建一個新的列表。
@f.j類似于for _ in range(sum('DELETE' in x for x in schedule)): schedule.remove(x)的東西也可以工作——因為remove無論如何都會進行搜索,所以您實際上只是使用匹配的數量,而不是它們的位置,所以只需取消這兩個操作的同步,即使您想使用循環,也無需反向操作。
@F.J:啊,謝謝。固定的。
@阿格夫:不,不幸的是——我的主要觀點是駁斥你的說法,即[:]是不必要的,是浪費時間。循環版本已修復,謝謝。
@AGF:所以把調用站點改為schedule[:] = modify_schedule(schedule, ...)--我的觀點仍然是多個名稱綁定到一個列表對象,并決定是否保持同步。
代碼與schedule[:]=[x對于schedule中的x,如果'delete'不在x]中,則可以正常工作。我對python和編程是個新手,所以總的來說有一些錯誤,希望我能了解更多,這樣我就可以討論它們了。感謝大家的幫助。
在對列表進行迭代時,不應修改該列表:
for x in schedule:
if 'DELETE' in x:
schedule.remove(x)
相反,嘗試:
schedule[:] = [x in schedule where 'DELETE' not in x]
有關詳細信息,請參見在迭代時從列表中刪除項
沒有必要從其他答案中復制原位作業,這并不好。
謝謝你的回答和信息。老實說,我仍然不知道如何正確地詢問關于Python的問題,也不知道在哪里尋找答案。我不知道在迭代過程中刪除項目是不同的。
@羅迪克:我很高興。這個問題是每個Python編碼人員遲早會遇到的問題。
@agf:如果不使用[:]會產生錯誤,那么使用它會更好。詳情請參閱我的答案。
不要修改正在迭代的序列。
schedule[:] = [x for x in schedule if 'DELETE' not in x]
總結
以上是生活随笔為你收集整理的python里面列表可以同时删除吗_在python中从列表中删除项,同时对其进行迭代的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java定义私有变量_Java Refl
- 下一篇: python scipy.optimiz