How can I remove duplication of 2 separate which is interrelated with each other (PYTHON)

≯℡__Kan透↙ 提交于 2019-12-13 08:16:22

问题


After reading so many title, I couldn't solved the problem below. Does anyone can help me please ?

For instance, I have 2 list (list_I and list_II) which is interrelated with each other.

list_I = [123, 453, 444, 555, 123, 444]

list_II = [A, A, B, C, A, B]

What I hope to get is:

New_list_I = [123, 453, 444, 555]


New_list_II = [A , A, B, C]

I use these two list as a body part of e-mail. That's why I need 2 separate (but on the other hand interrelated) list.

I'm able to send an e-mail right now. But because of the duplication problem it doesn't work how I want to.

P.S : I hope I explained the problem well but any question please don't hesitate to ask me.


回答1:


Looks like a job very well suited for dict:

list_I = [123, 453, 444, 555, 123, 444]    
list_II = ['A', 'A', 'B', 'C', 'A', 'B']

res = {}    
for elem, elem2 in zip(list_I, list_II):
    res[elem] = elem2    
print(res)

OUTPUT:

{123: 'A', 453: 'A', 444: 'B', 555: 'C'}

And if you want the lists, you can separate the keys and values from the dict:

print([k for k,v in res.items()])
print([v for k,v in res.items()])

OUTPUT:

[123, 453, 444, 555]
['A', 'A', 'B', 'C']



回答2:


list_I = [123, 453, 444, 555, 123, 444]
list_II = ['A', 'A', 'B', 'C', 'A', 'B']

New_list_I = []
New_list_II = []

for index, item in enumerate(list_I):
    if item not in New_list_I:
        New_list_I.append(item)
        New_list_II.append(list_II[index])

print(New_list_I)
print(New_list_II)

OUTPUT:

[123, 453, 444, 555]
['A', 'A', 'B', 'C']


来源:https://stackoverflow.com/questions/55240641/how-can-i-remove-duplication-of-2-separate-which-is-interrelated-with-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!