如何在列表中找到具有相同元素的子列表的索引?

时间:2020-12-11 07:33:52

I have a list of different sublists:

我有一个不同的子列表列表:

lst = [
    ['Alex', 'Peeters', 22],
    ['Jim', 'West', 22],
    ['Alex', 'Wesley', 25],
    ['Jim', 'West', 24]
]

How can I find the indices of the sublists in my list with the same first name and surname so I can delete the one with the highest age?

如何找到列表中具有相同名字和姓氏的子列表的索引,以便删除年龄最大的子列表?

So in this case lst[1][0] == lst[3][0] and lst[1][1] == lst[3][1] because lst[1][2] < lst[3][2] lst.remove[3]

所以在这种情况下lst [1] [0] == lst [3] [0]和lst [1] [1] == lst [3] [1]因为lst [1] [2]

I've got already this, but then I get an IndexError because my len(lst) changes wile removing.

我已经有了这个,但后来我得到了一个IndexError,因为我的len(lst)改变了删除。

for i in range (len(lst)):
  for j in range (len(lst)):
    if lst[i][0] == lst [j][0] and lst[i][1] == lst [j][1] and i != j:
      if lst[i][2] < lst[j][2]:
           lst.remove(lst[j])
      else:
            lst.remove(lst[i])

4 个解决方案

#1


You can use itertools.groupby within a list comprehension if the order of the elements is not of importance

如果元素的顺序不重要,您可以在列表推导中使用itertools.groupby

>>> [min(y) for x,y in itertools.groupby(sorted(lst), key = lambda x:x[:2])]
[['Alex', 'Peeters', 22], ['Alex', 'Wesley', 25], ['Jim', 'West', 22]]

#2


You could store the sublists to remove in another list and remove them after finding all to be removed.

您可以存储要在另一个列表中删除的子列表,并在找到要删除的所有列表后将其删除。

to_remove = []
for i in range (len(lst)):
  for j in range (len(lst)):
    if lst[i][0] == lst [j][0] and lst[i][1] == lst [j][1] and i != j:
      if lst[i][2] < lst[j][2]:
           to_remove.append(j)
      else:
            to_remove.append(i)
for sublist in to_remove:
    lst.pop(sublist)

This makes it a bit lengthy but easier to understand and debug.

这使它有点冗长,但更容易理解和调试。

#3


This could be one way to achieve this:

这可能是实现这一目标的一种方法:

lst = [['Alex', 'Peeters', 22], ['Jim', 'West', 22],['Alex', 'Wesley', 25],['Jim', 'West', 24]]

result = []

for each in set(map(lambda x: tuple(x[:2]), lst)):
    _min_lst = []
    for i in range(len(lst)):
        if each[0] in lst[i] and each[1] in lst[i]:
            _min_lst.append(lst[i][2])
    result.append(list(each)+[min(_min_lst)])

print result

Yields:

[['Alex', 'Peeters', 22], ['Jim', 'West', 22], ['Alex', 'Wesley', 25]]

#4


another solution...

items_to_remove = []
i, j = 0, 0
for x in lst:
    j = 0
    for y in lst:
        if x[0] == y[0] and x[1] == y[1] and i != j and x[2] >= y[2]:
            items_to_remove.append(x)
        j += 1
    i += 1


for item in items_to_remove:
    lst.remove(item)

#1


You can use itertools.groupby within a list comprehension if the order of the elements is not of importance

如果元素的顺序不重要,您可以在列表推导中使用itertools.groupby

>>> [min(y) for x,y in itertools.groupby(sorted(lst), key = lambda x:x[:2])]
[['Alex', 'Peeters', 22], ['Alex', 'Wesley', 25], ['Jim', 'West', 22]]

#2


You could store the sublists to remove in another list and remove them after finding all to be removed.

您可以存储要在另一个列表中删除的子列表,并在找到要删除的所有列表后将其删除。

to_remove = []
for i in range (len(lst)):
  for j in range (len(lst)):
    if lst[i][0] == lst [j][0] and lst[i][1] == lst [j][1] and i != j:
      if lst[i][2] < lst[j][2]:
           to_remove.append(j)
      else:
            to_remove.append(i)
for sublist in to_remove:
    lst.pop(sublist)

This makes it a bit lengthy but easier to understand and debug.

这使它有点冗长,但更容易理解和调试。

#3


This could be one way to achieve this:

这可能是实现这一目标的一种方法:

lst = [['Alex', 'Peeters', 22], ['Jim', 'West', 22],['Alex', 'Wesley', 25],['Jim', 'West', 24]]

result = []

for each in set(map(lambda x: tuple(x[:2]), lst)):
    _min_lst = []
    for i in range(len(lst)):
        if each[0] in lst[i] and each[1] in lst[i]:
            _min_lst.append(lst[i][2])
    result.append(list(each)+[min(_min_lst)])

print result

Yields:

[['Alex', 'Peeters', 22], ['Jim', 'West', 22], ['Alex', 'Wesley', 25]]

#4


another solution...

items_to_remove = []
i, j = 0, 0
for x in lst:
    j = 0
    for y in lst:
        if x[0] == y[0] and x[1] == y[1] and i != j and x[2] >= y[2]:
            items_to_remove.append(x)
        j += 1
    i += 1


for item in items_to_remove:
    lst.remove(item)