2

I have a multidimensional array called old_arr which is like this [[8,8,8,8,0,0,0,0,6,6,5,5],[...]] then I have an updated multidimensional array new_arr like this [[9,9,6,7,3,6,5,0,6,4,3,4],[...]] What I want to do is to update the new_arr so that if the value in it corresponds to a 0 in old_arr then the value should be 0 otherwise keep the new value. So in the above example the new_arr would look like this [[9,9,6,7,0,0,0,0,6,4,3,4],[...]] where the 3,6,5 where replaced by 0. Any advice?

Also I want to know if it is possible to update the cell to 0 only if 4 out of it's 8 surrounding neighbour cells have the value 0 as well? Like new_arr and old_arr are multidimensional array (lists) which represents rows and cols so they are like a big table as shown in the below image where the blue cell in the new_arr will only be updated to zero if the corresponding cell in the old_arr is 0 and 4 of its neighbour cells are 0 (white cells in the photo)

enter image description here

So I need to check all 8neighbour cells (sometimes 6 or 7 depending on the cell position where it's in the middle (8) or edges(7) or corners (6) ) if they are zeros or not and count them, if the count is 4 or more then set the cell value to 0.

So if old_arr is

[[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

And new_arr is

[[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 the highlighted cell, the corresponding cell in the new_arr will be zero because the highlighted cell in old_arr is 0 and more than 4 of its neighbor cells are zeros as well.

Updated new_arr is

[[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],....]
6
  • Their are no array lists in python? Array lists are only in java. Commented Jan 11, 2018 at 8:50
  • By "array list", do you mean "list"? or "list of lists"? because you don't mean "array list". Commented Jan 11, 2018 at 8:58
  • @khelwood I mean multidimensional array as shown in the example Commented Jan 11, 2018 at 8:59
  • Do you mean a numpy array? Or a list of lists? Commented Jan 11, 2018 at 9:19
  • For your highlighted cell [8,8,8,8,0,**0**,0,0,6,6,5,5], only 3 of the neighbouring cells are 0? Unless I'm not looking at it right. Commented Jan 12, 2018 at 10:23

5 Answers 5

3

Assuming old_arr and new_arr are the same length, you could do something like this:

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)

which outputs:

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

UPDATE:

Here is a brute force solution that deals with neighbouring cells:

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)

Which Outputs:

[[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]]

Note: This could be made better, but you can optimize it to your liking.

Sign up to request clarification or add additional context in comments.

9 Comments

Is it possible to update it to 0 only if 4 out of it's surrounding neighbour cells have the value 0 as well? Like they are multidimensional array (lists) which represents rows and cols so they are like a big table. So in this image i.sstatic.net/vKthv.jpg the blue cell in the new_arr will only be updated to zero if the corresponding cell in the old_arr is 0 and 4 of it's neighbour cells are 0 (white cells in the photo)
@Tak definetly possible, I'll update my answer. Can you elaborate "4 out of its surrounding neighbour cells"? Is that 2 on each side of the zero? Or is that 4 on each side of the zero? I don't understand this.
I check all 8neighbour cells (sometimes 6 or 7 depending on the cell position) if the are zeros or not and count them, if the count is 4 or more then set the cell value to 0
@Tak Sorry I was a bit busy, forgot to respond. Could you perhaps post a example input and expected output for this behaviour? This would make it alot easier.
Question updated, please let me know if I need to clarify anything more. Thanks
|
2

A simple solution with a list comprehension:

>>> 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]

Comments

2

Lists are modifiable sequences in Python so you can change them in place, which can make sense for large data sets. You can do that simply with 2 nested loops:

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

3 Comments

Gives a Type Error range() integer end argument expected, got list
@SergeBallesta but how to save the updates back to new_arr?
@Tak: the updates are done directly in new_arr. What is your problem?
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]]

Comments

0

A simple approach would be something like this:

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)

output:

[[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]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.