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

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

使用對(duì)稱差異獲取已刪除元素的索引

使用對(duì)稱差異獲取已刪除元素的索引

手掌心 2021-05-14 18:14:32
CSV文件中有兩列:oldCol1 = [1, 2, 3, 4, 5]oldCol2 = ['A', 'B', 'C', 'D', 'E']現(xiàn)在,我更新csv并添加新行newCol1 = [1, 2, 3, 4, 5, 6]newCol2 = ['A', 'B', 'C', 'D', 'E', 'A']我只想獲取新添加的元素。所以,我正在嘗試:newListCol1 = list(set(oldCol1).symmetric_difference(newCol1))現(xiàn)在,我的問題是如何從第二列中獲取新添加的元素?#Here, I want to get two lists: [6] and ['A']. 謝謝你的幫助!更新:新添加的元素可以在列表中的任何位置(而不僅僅是結(jié)尾)-造成混亂的原因!
查看完整描述

3 回答

?
縹緲止盈

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

#if they can be anywhere


#mehtod 1

from collections import Counter 


oldCol1 = [1, 2, 3, 4, 5]


oldCol2 = ['A', 'B', 'C', 'D', 'E']


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


newCol1_1 = [1, 2, 3, 4, 5, 6, 6, 7, 7] #different example


newCol2 = ['A', 'B', 'C', 'D', 'E', 'A']


print(list((Counter(newCol1) - Counter(oldCol1)))) # returns a list of unique value

print(list((Counter(newCol2) - Counter(oldCol2))))



new_item_added_dict = Counter(newCol1_1) - Counter(oldCol1)

print( list(new_item_added_dict.elements())) # elements() returns an iterator

# if you want all the new values even duplicates like in newCol1_1 

# ie if you want ans to be [6, 6, 7, 7] then use elements()


# else use list() if you just want unique value updates [6,7]

print( list(new_item_added_dict))


 # output

 # [6]

 # ['A']

 # [6, 6, 7, 7]

 # [6, 7]


#--------------------------------------------------------------------- 


#method 2

from collections import defaultdict

oldCol1 = [1, 2, 3, 4, 5]

newCol1 = [1, 2, 3, 4, 5, 6]  # -->[6]

# [1, 2, 3, 4, 5, 6, 5] --> [6,5]


new_item_list = []

oldlist_dict = defaultdict(lambda:0) #default value of key is 0 and with defualtdict you will not key error


for item in oldCol1:

    oldlist_dict[item] += 1


for item in newCol1:

    if item in oldlist_dict and oldlist_dict[item] > 0:

        oldlist_dict[item] -=1

    else:

        # its a new item 

        new_item_list.append(item)


print(new_item_list)



#--------------------------------------------------------------------- 


#if new items are always appended ie added to end of old list

print(newCol1[len(oldCol1):])  


print(newCol2[len(oldCol2):])


print(newCol1_1[len(oldCol1):])  


查看完整回答
反對(duì) 回復(fù) 2021-05-25
?
慕碼人2483693

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

如果您知道“新添加的元素”始終附加在列表的末尾,則只需從舊列表的長(zhǎng)度開始進(jìn)行切片。IE


old_til_here = len(oldCol1)

list_of_new_elements_col_1 = newCol1[old_til_here:]

list_of_new_elements_col_2 = newCol2[old_til_here:]


查看完整回答
反對(duì) 回復(fù) 2021-05-25
?
慕工程0101907

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

您將需要獲取第一個(gè)索引中不存在的索引,因此僅使用不帶有symmetric_difference的集合。使用enumerate()可使索引更容易。


oldCol1 = [1, 2, 3, 4, 5]


oldCol2 = ['A', 'B', 'C', 'D', 'E']


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


newCol2 = ['A', 'B', 'C', 'D', 'E', 'A']


indexes = [i for i, v in enumerate(newCol1) if v not in set(oldCol1)]


resultCol1 = [newCol1[i] for i in indexes]

resultCol2 = [newCol2[i] for i in indexes]


print(resultCol1, resultCol2)


查看完整回答
反對(duì) 回復(fù) 2021-05-25
  • 3 回答
  • 0 關(guān)注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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