第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從列表中刪除特定項(xiàng)目及其在 Python 中的所有重復(fù)項(xiàng)

從列表中刪除特定項(xiàng)目及其在 Python 中的所有重復(fù)項(xiàng)

翻閱古今 2023-03-22 17:15:00
我想從列表中刪除一個(gè)特定元素及其所有重復(fù)項(xiàng)(不刪除其他元素的重復(fù)項(xiàng)),我可以使用多個(gè) for 循環(huán)手動(dòng)完成,但請(qǐng)建議任何內(nèi)置函數(shù)(如果存在)。l1 = [some elements with duplicates]for i in l1  count = count.l1(i)# Counted duplicates of i  #some stuff with count  ## here want to delete the element at the ith position with all its duplicates例如,我有一個(gè)列表 [2,3,4,6,2,4,3,2] # 這可能是未知的 我計(jì)算了 2 的重復(fù)項(xiàng)并刪除了 2 及其所有重復(fù)項(xiàng) duplicate_count_of_i=3,一些東西,刪除具有所有重復(fù)項(xiàng)的元素已更新列表 [3,4,6,4,3] 謝謝您的寶貴時(shí)間,祝您有愉快的一天。
查看完整描述

3 回答

?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果您只想要獨(dú)特的元素,請(qǐng)使用集合:


l1 = [some elements with duplicates] # [1, 2, 3, 1, 2]

l1 = list(set(l1))                   # [1, 2, 3]

但是如果你想完全刪除重復(fù)的元素,你可以這樣做


l1 = [some elements with duplicates]            # [1, 2, 3, 1, 2]

l1 = [elt for elt in l1 if l1.count(elt) == 1]  # [3]


查看完整回答
反對(duì) 回復(fù) 2023-03-22
?
慕容森

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

我無(wú)法完全理解“列表中的特定項(xiàng)目及其所有副本”是什么意思。


i) 你是指該項(xiàng)目的所有出現(xiàn) ii) 還是該項(xiàng)目的所有重復(fù)項(xiàng)


對(duì)于案例 1:


您可以使用 while 循環(huán)從列表中刪除元素的所有實(shí)例。


>>> l1 =  [1,2,3,3,4,4,5,5,5,6,6]    

>>> element_to_remove = 5

>>>

>>> while element_to_remove in l1:

...    l1.remove(element_to_remove)

>>> l1

[1,2,3,3,4,4,6,6]

對(duì)于案例 2:


>>> l1 =  [1,2,3,3,4,4,5,5,5,6,6]    

>>> element_to_remove = 5

>>>

>>> while l1.count(element_to_remove) > 1: #removing all the duplicates

...    l1.remove(element_to_remove)

>>> l1

[1,2,3,3,4,4,5,6,6]


查看完整回答
反對(duì) 回復(fù) 2023-03-22
?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊

使用Python 集來(lái)完成這項(xiàng)工作。它永遠(yuǎn)不會(huì)在您的列表中使用任何重復(fù)的項(xiàng)目。然后你可以將它轉(zhuǎn)換為列表

代碼

l1 = ["apple", "banana", "apple"]

li2 = list(set(l1))


print(li2)


# >>> ['banana', 'apple']


查看完整回答
反對(duì) 回復(fù) 2023-03-22
  • 3 回答
  • 0 關(guān)注
  • 136 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)