比较python中的多维数组

时间:2018-01-11 08:49:21

标签: python multidimensional-array

我有一个名为old_arr的多维数组,就像这个[[8,8,8,8,0,0,0,0,6,6,5,5],[...]]那样我有一个更新的多维数组new_arr就像这样[[9,9,6,7,3,6,5,0,6,4,3,4],[...]]我要做的就是更新new_arr,以便如果其中的值对应0中的old_arr,那么该值应为0,否则保留新值。所以在上面的示例中,new_arr看起来像[[9,9,6,7,0,0,0,0,6,4,3,4],[...]],其中3,6,5替换为0.任何建议?

此外,我想知道是否可以将单元格更新为0,只有当其中4个周围的相邻单元格的值为0时才更新?像new_arr和old_arr一样是多维数组(列表),它们表示行和列,因此它们就像一个大表,如下图所示,如果old_arr中的相应单元格为0,则new_arr中的蓝色单元格将仅更新为零其4个相邻单元格为0(照片中的白色单元格)

enter image description here

所以我需要检查所有8个目标细胞(有时是6或7个细胞,取决于它位于中间(8)或边缘(7)或角落(6)的细胞位置),如果它们是零或不是并计算它们,如果计数为4或更多,则将单元格值设置为0.

所以如果old_arr是

[[8,8,8,8,0,0,0,0,6,6,5,5],
 [8,8,8,8,0,x,0,0,6,6,5,5],
 [8,8,8,8,0,0,0,0,6,6,5,5],
 [8,8,8,8,0,0,0,0,6,6,5,5],....]

Where x is a zero

而new_arr是

[[9,9,6,7,3,6,5,0,6,4,3,4],
 [9,9,6,7,3,6,5,0,6,4,3,4],
 [9,9,6,7,3,6,5,0,6,4,3,4],
 [9,9,6,7,3,6,5,0,6,4,3,4],....]

对于突出显示的单元格,new_arr中的相应单元格将为零,因为old_arr中突出显示的单元格为0,并且其相邻单元格中的4个以上也为零。

更新了new_arr

[[9,9,6,7,3,0,0,0,6,4,3,4],
 [9,9,6,7,0,0,0,0,6,4,3,4],
 [9,9,6,7,0,0,0,0,6,4,3,4],
 [9,9,6,7,0,0,0,0,6,4,3,4],....]

5 个答案:

答案 0 :(得分:2)

假设old_arrnew_arr长度相同,您可以这样做:

old_arr = [[8,8,8,8,0,0,0,0,6,6,5,5], [8,8,7,0,0,0,0,0,6,6,5,5]]
new_arr = [[9,9,6,7,3,6,5,0,6,4,3,4], [9,0,6,7,4,6,5,0,6,4,3,4]]

new_arr = [[x if old[i] else 0 for i, x in enumerate(new)] for old, new in zip(old_arr, new_arr)]

print(new_arr)

输出:

[[9, 9, 6, 7, 0, 0, 0, 0, 6, 4, 3, 4], [9, 0, 6, 0, 0, 0, 0, 0, 6, 4, 3, 4]]

<强>更新

这是一个处理相邻细胞的强力解决方案:

old_arr = [[8,8,8,8,0,0,0,0,6,6,5,5],
           [8,8,8,8,0,0,0,0,6,6,5,5],
           [8,8,8,8,0,0,0,0,6,6,5,5],
           [8,8,8,8,0,0,0,0,6,6,5,5]]

new_arr = [[9,9,6,7,3,6,5,0,6,4,3,4],
           [9,9,6,7,3,6,5,0,6,4,3,4],
           [9,9,6,7,3,6,5,0,6,4,3,4],
           [9,9,6,7,3,6,5,0,6,4,3,4]]

def first_last(row, next_row, old, new):
    for i in range(len(new[row])):
        count = 0
        if old[row][i] == 0:
            if old[row][i-1] == 0:
                count += 1
            if old[row][i+1] == 0:
                count += 1
            if old[next_row][i] == 0:
                count += 1
            if old[next_row][i-1] == 0:
                count += 1
            if old[next_row][i+1] == 0:
                count += 1  

        if count > 4:
            new[row][i] = 0

def middle(old, new):
    for i, l in enumerate(new[1:-1]):
        for j in range(len(l)):
            count = 0
            if old[i][j] == 0:
                if old[i][j-1] == 0:
                    count += 1
                if old[i][j+1] == 0:
                    count += 1
                if old[i-1][j] == 0:
                    count += 1
                if old[i-1][j-1] == 0:
                    count += 1
                if old[i-1][j+1] == 0:
                    count += 1
                if old[i+1][j] == 0:
                    count += 1
                if old[i+1][j-1] == 0:
                    count += 1
                if old[i+1][j+1] == 0:
                    count += 1

            if count > 4:
                l[j] = 0

# first row
first_last(0, 1, old_arr, new_arr)

# middle rows
middle(old_arr, new_arr)

# last row
first_last(-1, -2, old_arr, new_arr)

print(new_arr)

哪个输出:

[[9, 9, 6, 7, 3, 0, 0, 0, 6, 4, 3, 4], 
 [9, 9, 6, 7, 0, 0, 0, 0, 6, 4, 3, 4], 
 [9, 9, 6, 7, 0, 0, 0, 0, 6, 4, 3, 4], 
 [9, 9, 6, 7, 3, 0, 0, 0, 6, 4, 3, 4]]

注意:这可以做得更好,但您可以根据自己的喜好对其进行优化。

答案 1 :(得分:2)

具有列表理解的简单解决方案:

>>> old_arr=[9,9,6,7,3,6,5,0,6,4,3,4]
>>> new_arr=[8,8,8,8,0,0,0,0,6,6,5,5]
>>> [new_arr[i] if old_arr[i] else 0 for i in range(len(new_arr))]
[9, 9, 6, 7, 0, 0, 0, 0, 6, 4, 3, 4]

答案 2 :(得分:2)

列表是Python中可修改的序列,因此您可以将它们到位更改,这对大型数据集有意义。您只需使用2个嵌套循环即可完成此操作:

for i, l in enumerate(new_arr):
    for j in range(len(l)):
        if old_arr[i][j] == 0:
            l[j] = 0

答案 3 :(得分:0)

def custom_compare(list_old, list_new):
a=[]
for num, each_list_old in enumerate(list_old):
    for num1,each_list_new_entry in enumerate(list_new[num]):
        if each_list_old[num1] != 0:
            a.append(each_list_new_entry)
        else:
            a.append(0)
    list_new[num] = a
    a=[]
print "Finally new_arr = ",list_new

Eg: old_arr = [[8,8,8,8,0,0,0,0,6,6,5,5],[1,2,3,4,5,6,7,8]] new_arr = [[9,9,6,7,3,6,5,0,6,4,3,4],[8,3,4,5,6,78,8,9]] custom_compare(old_arr, new_arr) Finally new_arr = [[9, 9, 6, 7, 0, 0, 0, 0, 6, 4, 3, 4], [8, 3, 4, 5, 6, 78, 8, 9]]

答案 4 :(得分:0)

一个简单的方法是这样的:

first=[[8,8,8,8,0,0,0,0,6,6,5,5],[8,8,8,8,0,0,0,0,6,6,5,5],[8,8,8,8,0,0,0,0,6,6,5,5],[8,8,8,8,0,0,0,0,6,6,5,5]]
second = [[9,9,6,7,3,6,5,0,6,4,3,4],
 [9,9,6,7,3,6,5,0,6,4,3,4],
 [9,9,6,7,3,6,5,0,6,4,3,4],
 [9,9,6,7,3,6,5,0,6,4,3,4]]

for first_1,second_1 in zip(first,second):
    for index,value in enumerate(first_1):
        if value==0:
            try:
                if first_1[index-1]==0 and first_1[index+1]==0:
                    second_1[index]=0
                    second_1[index-1]=0
                    second_1[index+1]=0
            except IndexError:
                pass
print(second)

输出:

[[9 9 6 7 0 0 0 0 6 4 3 4]
 [9 9 6 7 0 0 0 0 6 4 3 4]
 [9 9 6 7 0 0 0 0 6 4 3 4]
 [9 9 6 7 0 0 0 0 6 4 3 4]]