从列表中删除重复项,并在另一个列表中的同一索引中删除元素

发布于 2025-02-07 19:27:44 字数 380 浏览 3 评论 0原文

我有两个列表,一个带有单词,另一个带有这样的单词类型,

['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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

草莓味的萝莉 2025-02-14 19:27:45

输出的字典(请参见下文),这将是 更容易的

使用列表

# Inputs
list1 = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
list2 = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']

# New lists
new_list1 = []
new_list2 = []

# Loop through elements of input lists
for a, b in zip(list1, list2):
    # If it isn't a duplicate, append to the new lists
    if a not in new_list1:
        new_list1.append(a)
        new_list2.append(b)

# Output new lists
print(new_list1)
print(new_list2)

['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']

带有字典

# This uses the fact that dictionaries have unique keys, so this cancels out all the duplicates
x = dict(zip(
    ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert'],
    ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
))

# Output dictionary
print(x)

# Output keys (equivalent of new_list1)
print(list(x.keys()))

# Output values (equivalent of new_list2)
print(list(x.values()))

输出:

{'fish': 'animal', 'Robert': 'person', 'dog': 'animal', 'ball': 'object', 'cat': 'animal'}
['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']

This would be much easier with dictionaries (see below)

With lists

# Inputs
list1 = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
list2 = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']

# New lists
new_list1 = []
new_list2 = []

# Loop through elements of input lists
for a, b in zip(list1, list2):
    # If it isn't a duplicate, append to the new lists
    if a not in new_list1:
        new_list1.append(a)
        new_list2.append(b)

# Output new lists
print(new_list1)
print(new_list2)

Output:

['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']

With a dictionary

# This uses the fact that dictionaries have unique keys, so this cancels out all the duplicates
x = dict(zip(
    ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert'],
    ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
))

# Output dictionary
print(x)

# Output keys (equivalent of new_list1)
print(list(x.keys()))

# Output values (equivalent of new_list2)
print(list(x.values()))

Output:

{'fish': 'animal', 'Robert': 'person', 'dog': 'animal', 'ball': 'object', 'cat': 'animal'}
['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']
递刀给你 2025-02-14 19:27:45

只需循环并使用zip函数

脚本

# Input Vars
a = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
b = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']

# Output Vars
c = []
d = []

# Process
for e, f in zip(a, b):
    if(e not in c):
        c.append(e)
        d.append(f)

# Print Output
print(c)
print(d)

输出

['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']

just do a loop and use the zip function

Script

# Input Vars
a = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
b = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']

# Output Vars
c = []
d = []

# Process
for e, f in zip(a, b):
    if(e not in c):
        c.append(e)
        d.append(f)

# Print Output
print(c)
print(d)

Output

['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']
触ぅ动初心 2025-02-14 19:27:45

您可以使用set进行删除,然后使用index仅获取word_type list> list

word = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
word_type = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
unique_word = list(set(word))
updated_word_type = [word_type[word.index(_)] for _ in unique_word]

utput word_type 强>

# unique_word
['dog', 'fish', 'Robert', 'ball', 'cat']

# updated_word_type
['animal', 'animal', 'person', 'object', 'animal']

You can use a set to de-duplicate and then use index to get only the first occurrence from the word_type list

word = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
word_type = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
unique_word = list(set(word))
updated_word_type = [word_type[word.index(_)] for _ in unique_word]

Output

# unique_word
['dog', 'fish', 'Robert', 'ball', 'cat']

# updated_word_type
['animal', 'animal', 'person', 'object', 'animal']
我不会写诗 2025-02-14 19:27:45

您可以使用ZIP两个列表,并使用 只能仅重复一次键,然后将键和值作为您想要的两个列表返回:

lst1 = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
lst2 = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
res = dict(zip(lst1, lst2))
# res -> {'Robert': 'person','ball': 'object', 'cat': 'animal', 'dog': 'animal', 'fish': 'animal'}
list(res.keys())
# ['fish', 'Robert', 'dog', 'ball', 'cat']

list(res.values())
# ['animal', 'person', 'animal', 'object', 'animal']

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:

lst1 = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
lst2 = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
res = dict(zip(lst1, lst2))
# res -> {'Robert': 'person','ball': 'object', 'cat': 'animal', 'dog': 'animal', 'fish': 'animal'}
list(res.keys())
# ['fish', 'Robert', 'dog', 'ball', 'cat']

list(res.values())
# ['animal', 'person', 'animal', 'object', 'animal']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文