Python从二维列表中删除元素

时间:2022-09-17 22:51:01

Trying to remove min and max values from two dimensional list in array. My code:

尝试从数组中的二维列表中删除最小值和最大值。我的代码:

myList = [[1, 3, 4], [2, 4, 4], [3, 4, 5]]
maxV = 0
minV = myList[0]0]
for list in myList:
   for innerlist in list:
      if innerlist > maxV:
         maxV = innerlist
      if innerlist < minV:
         minV = innerlist
   innerlist.remove(maxV)
   innerlist.remove(minV)
print(myList)

This causes me some erros, which i not particulary understand. I'm quite sure that innerlist is not array but ordinary variable. But still i think it should be somehow possible to remove min and max elements from two dimensional list. I mean I need to remove in every innerlist in my list highest and lowest values. LF help! Regards.

这引起了我一些错误,我并不特别理解。我很确定innerlist不是数组而是普通变量。但我仍然认为应该以某种方式从二维列表中删除min和max元素。我的意思是我需要删除列表中最高和最低值的每个内部列表。 LF帮忙!问候。

5 个解决方案

#1


11  

Just for the sake of showing a much simpler way of doing this using list comprehensions, the sorted method and slicing:

只是为了使用列表推导显示更简单的方法,排序的方法和切片:

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

n = [sorted(l)[1:-1] for l in d]

print(n)

# [[3], [4], [4]]

Some reading material on each of the items used to solve this problem:

用于解决此问题的每个项目的一些阅读材料:

To take care of duplicates, this answer by Padraic is very well done.

为了照顾重复,Padraic的这个答案做得非常好。

#2


3  

If you want to remove all occurrences, you will have to find the min and max and remove all occurrence from each sublist:

如果要删除所有匹配项,则必须找到最小值和最大值,并从每个子列表中删除所有匹配项:

def remove(l):
    for sub in l:
        t = {min(sub), max(sub)}
        sub[:] = (ele for ele in sub if ele not in t)


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

remove(l)

Which will give you:

哪个会给你:

[[3], [2], [4]]

To find the min and max in a single pass you can use a helper function:

要在单个过程中查找最小值和最大值,您可以使用辅助函数:

def min_max(sub):
    # all numbers are > float("-inf") and < float("inf")
    mx, mn = float("-inf"), float("inf")
    for ele in sub:
        if ele < mn:
            mn = ele
        if ele > mx:
            mx = ele
    return {mn, mx}

def remove(l):
    for sub in l:
        # find min and max
        mn_mx = min_max(sub)
        # update sublist so all occurrences of either are removed
        sub[:] = (ele for ele in sub if ele not in mn_mx)

Even if your own logic worked and you wanted to remove all the elements equal to the max, it would not work using remove as it will only remove the first occurrence each time.

即使你自己的逻辑工作并且你想要删除所有等于max的元素,它也不会使用remove,因为它只会删除每次的第一次出现。

In [8]: l = [1,2,3,4,4]

In [9]: l.remove(4)

In [10]: l
Out[10]: [1, 2, 3, 4]

Based on one of your comments you seem to have strings in your sublists which will error when compared to an int, if the string is always the first element you can slice it off:

基于您的一条评论,您似乎在子列表中有字符串,与int相比会出错,如果字符串始终是第一个可以将其切掉的元素:

   from itertools import islice

   def remove(l):
        for sub in l:
            sub = sub[1:]
            mn_mx = min_max(sub)
            sub[:] = (ele for ele in sub if ele not in mn_mx)

#3


0  

try this approach:

尝试这种方法:

foreach innerlist:

foreach内部列表:

  1. sort the array
  2. 对数组进行排序
  3. remove the first element
  4. 删除第一个元素
  5. remove the last element
  6. 删除最后一个元素

#4


0  

It should work like this:

它应该像这样工作:

myList = [[1, 3, 4], [2, 4, 4], [3, 4, 5]]
for list in myList:
   maxV = list[0] #initialise them here
   minV = list[0] #because you look for max in list
   for value in list:
      if value> maxV:
         maxV = innerlist
      if value< minV:
         minV = innerlist
   list.remove(maxV) #remove from list
   list.remove(minV)
print(myList)

Your errors where:

你的错误在哪里:

  1. minV = myList[0]0] a [ to little
  2. minV = myList [0] 0] a [很少
  3. maxV = 0 works only if the list is always positive
  4. maxV = 0仅在列表始终为正时才起作用
  5. maxV and minV should be inside the first loop
  6. maxV和minV应该在第一个循环内
  7. innerlist.remove(maxV) should be list.remove(maxV)
  8. innerlist.remove(maxV)应该是list.remove(maxV)

I also renamed innerList to value

我还将innerList重命名为value

#5


-1  

Unindent your removes, to take them out of the loop and:

Unindent你的删除,把他们带出循环,并:

maxV = -10000000
minV = 10000000   # !!

#1


11  

Just for the sake of showing a much simpler way of doing this using list comprehensions, the sorted method and slicing:

只是为了使用列表推导显示更简单的方法,排序的方法和切片:

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

n = [sorted(l)[1:-1] for l in d]

print(n)

# [[3], [4], [4]]

Some reading material on each of the items used to solve this problem:

用于解决此问题的每个项目的一些阅读材料:

To take care of duplicates, this answer by Padraic is very well done.

为了照顾重复,Padraic的这个答案做得非常好。

#2


3  

If you want to remove all occurrences, you will have to find the min and max and remove all occurrence from each sublist:

如果要删除所有匹配项,则必须找到最小值和最大值,并从每个子列表中删除所有匹配项:

def remove(l):
    for sub in l:
        t = {min(sub), max(sub)}
        sub[:] = (ele for ele in sub if ele not in t)


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

remove(l)

Which will give you:

哪个会给你:

[[3], [2], [4]]

To find the min and max in a single pass you can use a helper function:

要在单个过程中查找最小值和最大值,您可以使用辅助函数:

def min_max(sub):
    # all numbers are > float("-inf") and < float("inf")
    mx, mn = float("-inf"), float("inf")
    for ele in sub:
        if ele < mn:
            mn = ele
        if ele > mx:
            mx = ele
    return {mn, mx}

def remove(l):
    for sub in l:
        # find min and max
        mn_mx = min_max(sub)
        # update sublist so all occurrences of either are removed
        sub[:] = (ele for ele in sub if ele not in mn_mx)

Even if your own logic worked and you wanted to remove all the elements equal to the max, it would not work using remove as it will only remove the first occurrence each time.

即使你自己的逻辑工作并且你想要删除所有等于max的元素,它也不会使用remove,因为它只会删除每次的第一次出现。

In [8]: l = [1,2,3,4,4]

In [9]: l.remove(4)

In [10]: l
Out[10]: [1, 2, 3, 4]

Based on one of your comments you seem to have strings in your sublists which will error when compared to an int, if the string is always the first element you can slice it off:

基于您的一条评论,您似乎在子列表中有字符串,与int相比会出错,如果字符串始终是第一个可以将其切掉的元素:

   from itertools import islice

   def remove(l):
        for sub in l:
            sub = sub[1:]
            mn_mx = min_max(sub)
            sub[:] = (ele for ele in sub if ele not in mn_mx)

#3


0  

try this approach:

尝试这种方法:

foreach innerlist:

foreach内部列表:

  1. sort the array
  2. 对数组进行排序
  3. remove the first element
  4. 删除第一个元素
  5. remove the last element
  6. 删除最后一个元素

#4


0  

It should work like this:

它应该像这样工作:

myList = [[1, 3, 4], [2, 4, 4], [3, 4, 5]]
for list in myList:
   maxV = list[0] #initialise them here
   minV = list[0] #because you look for max in list
   for value in list:
      if value> maxV:
         maxV = innerlist
      if value< minV:
         minV = innerlist
   list.remove(maxV) #remove from list
   list.remove(minV)
print(myList)

Your errors where:

你的错误在哪里:

  1. minV = myList[0]0] a [ to little
  2. minV = myList [0] 0] a [很少
  3. maxV = 0 works only if the list is always positive
  4. maxV = 0仅在列表始终为正时才起作用
  5. maxV and minV should be inside the first loop
  6. maxV和minV应该在第一个循环内
  7. innerlist.remove(maxV) should be list.remove(maxV)
  8. innerlist.remove(maxV)应该是list.remove(maxV)

I also renamed innerList to value

我还将innerList重命名为value

#5


-1  

Unindent your removes, to take them out of the loop and:

Unindent你的删除,把他们带出循环,并:

maxV = -10000000
minV = 10000000   # !!