Python-二维数组中的Shift / Delete元素 [英] Python - Shift/Delete Elements in a 2-Dimensional Array

查看:108
本文介绍了Python-二维数组中的Shift / Delete元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来移动和删除二维数组中的元素。

I need help with shifting and deleting elements in a 2-dimensional array.


  1. 如果列表中的值为负,并且它们是位于其上方的列表,并且在同一位置具有正值。它应该向下移动所有内容,导致负值消失。

  1. If the value in a list is negative and their is a list above it with positive values in the same location. It should shift everything down, causing the negative values to disappear.

如果上面没有任何列表,或者上面列表中的相应值仅为0。它将用0代替负值。

If there isn't any list above it or the corresponding values in the list above are just 0. It will replace the negative values with 0.

注意:正值永远不会消失,它们只能在需要时向下移动。仅负值(低于-100)消失。

Note: The positive values should never disappear, they can only move down when needed. Only the negative values (below -100) disappear.

这些示例应能更好地解释:

These examples should explain it better:

方案1:

数据: [[0,0,0,0,0],[0,0,0,0,0 ],[1、2、1、0、0],[2、1、2、0、0],[-103,-103,-103、0、0]]

期望: [[0,0,0,0,0],[0,0,0,0, 0],[0、0、0、0、0],[1、2、1、0、0],[2、1、2、0、0]]

方案2:

数据: [[0,0,0,0,0],[0,0,0,0,0],[0,2,-101,-101,-101],[0,1,2,3, 2],[0、3、3、2、3]]

期望: [[0,0,0,0,0],[0,0,0,0,0],[0,2,0,0,0],[0,1,2,3,2] ,[0、3、3、2、3]]

方案3:(这是唯一

DATA: [[0,0,0,0 ,0],[0、0、0、0、0],[1、3、1、0、0],[-102,-102,-102、0、0],[3、1、3, 0,0]]

期望: [[0,0,0,0,0],[0,0,0,0,0],[0, 0、0、0、0],[1、3、1、0、0],[3、1、3、0、0]]

def move(data):

    c_count = 4

    while c_count >= 0:

        count = len(data) - 1
        prev = count - 1

        while count > 0 and prev >= 0:
            if data[count][c_count] < -100:

                while prev >= 0 and data[prev][c_count] == 0:
                    prev -= 1

                data[count][c_count] = data[prev][c_count]
                data[prev][c_count]= 0

            count -= 1
            prev -= 1

        c_count -= 1

    return data

my_data = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 3, 1, 0, 0], [-102, -102, -102, 0, 0], [3, 1, 3, 0, 0]] 
x = move(my_data) # This is (scenario 3) is the only one that works. 
print(x)

非常感谢您的帮助!我已经坚持了一段时间。

Thanks so much for your help! I have been stuck on this for a while.

推荐答案

我单独处理列,而不处理完整行。

I work with columns separately, not with full rows.


  • 从下至上搜索列

  • 查找负值

  • 在上方找到正值(先大于零,然后

  • 如果未找到,则将零替换为负数

  • 如果找到,则将所有值下移至

    移动高于高于-1 第1行第2行行2 等。

  • search in column from bottom to top
  • find negative value
  • find positive value (bigger then zero) above
  • if not found then put zero in place of negative
  • if found then move down all value above
    move above to row, above-1 to row-1, above-2 to row-2, etc.

顺便说一句:当行在另一行下面显示时,搜索解决方案更加容易。

BTW: it is easier to search solution when rows are displayed one below another.

def move(data):

    # work in column, not with full rows
    for col in range(len(data)):

        # move from bottom to top
        for row in range(len(data[0])-1, -1, -1):

            # check if negative value
            if data[row][col] < 0:
                print('debug: negative:', data[row][col])

                # find positive value above
                above = row-1
                while above > -1 and data[above][col] <= 0:
                    above -= 1

                # check if found positive value 
                if above == -1:
                    # put zero if not found value above
                    print('debug: put zero')
                    data[row][col] = 0
                else:
                    # move down all values above 
                    print('debug: move down', above+1, 'element(s)')
                    while above > -1:
                        data[row][col] = data[above][col]
                        data[above][col] = 0
                        row -= 1
                        above -= 1

    return data

# --- function to run one scenario, display data and check result ---

def run(data, expect):
    print('data:')
    print('\n'.join(str(row) for row in data))
    print()
    result = move(data)
    print()
    print('result:')
    print(result)
    print('expect:')
    print(expect)
    print('expect == result:', expect == result)
    print('---')

# --- scenarios ---

def scenario1():

    DATA = [
        [0, 0, 0, 0, 0], 
        [0, 0, 0, 0, 0], 
        [1, 2, 1, 0, 0], 
        [2, 1, 2, 0, 0], 
        [-103, -103, -103, 0, 0]
    ]

    EXPECT = [
        [0, 0, 0, 0, 0], 
        [0, 0, 0, 0, 0], 
        [0, 0, 0, 0, 0], 
        [1, 2, 1, 0, 0], 
        [2, 1, 2, 0, 0]
    ]

    run(DATA, EXPECT)

def scenario2():

    DATA = [
        [0, 0, 0, 0, 0], 
        [0, 0, 0, 0, 0], 
        [0, 2, -101, -101, -101], 
        [0, 1, 2, 3, 2], 
        [0, 3, 3, 2, 3]
    ]

    EXPECT = [
        [0, 0, 0, 0, 0], 
        [0, 0, 0, 0, 0], 
        [0, 2, 0, 0, 0], 
        [0, 1, 2, 3, 2], 
        [0, 3, 3, 2, 3]
    ]

    run(DATA, EXPECT)

def scenario3(): #(This is the only one that I got working in my code below.)

    DATA = [
        [0, 0, 0, 0, 0], 
        [0, 0, 0, 0, 0], 
        [1, 3, 1, 0, 0], 
        [-102, -102, -102, 0, 0], 
        [3, 1, 3, 0, 0]
    ]

    EXPECT = [
        [0, 0, 0, 0, 0], 
        [0, 0, 0, 0, 0], 
        [0, 0, 0, 0, 0], 
        [1, 3, 1, 0, 0], 
        [3, 1, 3, 0, 0]
    ]

    run(DATA, EXPECT)

# --- start scenarios ---

scenario1()
scenario2()
scenario3()

结果:

data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 2, 1, 0, 0]
[2, 1, 2, 0, 0]
[-103, -103, -103, 0, 0]

debug: negative: -103
debug: move down 4 element(s)
debug: negative: -103
debug: move down 4 element(s)
debug: negative: -103
debug: move down 4 element(s)

result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 2, 1, 0, 0], [2, 1, 2, 0, 0]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 2, 1, 0, 0], [2, 1, 2, 0, 0]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 2, -101, -101, -101]
[0, 1, 2, 3, 2]
[0, 3, 3, 2, 3]

debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero
debug: negative: -101
debug: put zero

result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 1, 2, 3, 2], [0, 3, 3, 2, 3]]
expect == result: True
---
data:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 3, 1, 0, 0]
[-102, -102, -102, 0, 0]
[3, 1, 3, 0, 0]

debug: negative: -102
debug: move down 3 element(s)
debug: negative: -102
debug: move down 3 element(s)
debug: negative: -102
debug: move down 3 element(s)

result:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 3, 1, 0, 0], [3, 1, 3, 0, 0]]
expect:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 3, 1, 0, 0], [3, 1, 3, 0, 0]]
expect == result: True
---

这篇关于Python-二维数组中的Shift / Delete元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆