基于另一个数组查找数组中最大的元素并将其删除(Python3.x)

2024-04-19 07:58:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python-3.x 我有两个数组,我想根据(e)列表中最大的两个数字删除(x)和(e)中的两行:

 index
    0    x=[0 0 0 1 0]             e=[ [12]
    1      [1 1 1 1 0]                 [6 ] 
    2      [0 0 1 0 0] delete this row [20] --> the 1st largest number
    3      [1 1 1 1 1]                 [15] 
    4      [0 1 0 0 0]                 [0 ]
    5      [0 1 1 0 0] delete this row [18] --> the 2nd largest number
    6      [1 0 1 1 0]                 [11]

我尝试了很多方法,但由于某些原因,我无法解决它例如,我尝试了以下方法:

l3 = list(zip(x, e))
#print(l3)
def maxx(n):
    max_val = 0
    index_val = 0
    l4 = l3[:n]

    for x,y in l4:
        if max_val < (y):
            max_val = (y)
            elem = (x, y)
            index_val = l3.index(elem)


    print ("max: ", max_val)
    print ("index of values:", index_val)
n = 20
maxx(n)

此代码只返回一个索引值,如果最大的数字不在索引“0”中,则返回错误!!!!你知道吗

我也尝试过这个代码,但它不能正常工作!!!地址:

r = x[e.argpartition(2)[18:], :]
print (r)

如果您有任何建议,我们将不胜感激


Tags: the方法numberindex数字valthisdelete
3条回答

您也可以解决您的问题,而无需使用numpy如以下示例所示:

x = [[0, 0, 0, 1, 0], [1, 1, 1, 1, 0], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 1, 0, 0, 0], [0, 1, 1, 0, 0], [1, 0, 1, 1, 0]]
e = [[12], [6], [20], [15], [0], [18], [11]]

def get_new_modified_list(a = list, b = list, max_index = 2):
    indexs = []
    a_copy = [k[0] for k in a]
    b_copy = b[:]
    for v in range(max_index):
        m = max(a_copy)
        # Find the max with it's occurrences
        indexs += [k for k in range(len(a_copy)) if a_copy[k] == m]

        i = 0
        for j in indexs:
            a_copy.pop(j-i)
            b_copy.pop(j-i)
            i+=1
        indexs = []

    return b_copy, a_copy

print(get_new_modified_list(e, x))

输出:

(
  [
   [0, 0, 1, 0, 0], [1, 1, 1, 1, 1],
   [0, 1, 0, 0, 0], [0, 1, 1, 0, 0],
   [1, 0, 1, 1, 0]
 ],
 [
   [12], [6], [15], [0], [11]
 ]
)

你可以这样做:

def delete_row(x, e):
    # Find the max value and index
    index, value = max(enumerate(e), key=lambda x: x[1][0])

    # Delete the rows
    del x[index]
    del e[index]


x = [
    [0, 0, 0, 1, 0],
    [1, 1, 1, 1, 0],
    [0, 0, 1, 0, 0],
    [1, 1, 1, 1, 1],
    [0, 1, 0, 0, 0],
    [0, 1, 1, 0, 0],
    [1, 0, 1, 1, 0]
]

e = [
    [12], [6], [20], [15],
    [0], [18], [11]
]

# Delete 2 rows
for _ in range(2):
    delete_row(x, e)

上述代码的结果将是:

x = [
    [0, 0, 0, 1, 0],
    [1, 1, 1, 1, 0],
    [1, 1, 1, 1, 1],
    [0, 1, 0, 0, 0],
    [1, 0, 1, 1, 0]
]

e = [
    [12], [6], [15],
    [0], [11]
]

我希望它能帮助你:)

您可以这样使用argpartition,首先对e数组求反,这样最大值的索引将在结果的开头进行分区,分区后,删除前两个索引并对剩余的索引进行排序:

x[np.sort((-e.ravel()).argpartition(2)[2:]),:]

#matrix([[0, 0, 0, 1, 0],
#        [1, 1, 1, 1, 0],
#        [1, 1, 1, 1, 1],
#        [0, 1, 0, 0, 0],
#        [1, 0, 1, 1, 0]])

使用的数据:

x = np.array([[0, 0, 0, 1, 0],
        [1, 1, 1, 1, 0],
        [0, 0, 1, 0, 0],
        [1, 1, 1, 1, 1],
        [0, 1, 0, 0, 0],
        [0, 1, 1, 0, 0],
        [1, 0, 1, 1, 0]])

e = np.array([[12],[6],[20],[15],[0],[18],[11]])

相关问题 更多 >