从列表中删除重复项,并在另一个列表中的同一索引中删除元素
我有两个列表,一个带有单词,另一个带有这样的单词类型,
['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
我需要在第一个列表中删除重复项,并在删除单词的相同索引中删除类型。
最后,我需要这样的事情:
['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']
我该怎么做?
I have two lists, one with word and another one with word type like this
['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
I need to remove duplicates in the first list and to remove the type at the same index of the word removed.
In the end I need something like this:
['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
输出的字典(请参见下文),这将是 更容易的
使用列表
:
带有字典
输出:
This would be much easier with dictionaries (see below)
With lists
Output:
With a dictionary
Output:
只需循环并使用zip函数
脚本
输出
just do a loop and use the zip function
Script
Output
您可以使用
set
进行删除,然后使用index
仅获取word_type
list> listutput word_type 强>
You can use a
set
to de-duplicate and then useindex
to get only the first occurrence from theword_type
listOutput
您可以使用ZIP两个列表,并使用 只能仅重复一次键,然后将键和值作为您想要的两个列表返回:
You can use zip two lists and use the idea of
dictionary
that keys can be repeated only one time then return keys and values as two lists that you want: