S
S
stepan-neretin72021-06-10 21:40:15
Python
stepan-neretin7, 2021-06-10 21:40:15

How to compare two arrays?

It is necessary to compare two arrays, taking into account the order and taking into account that if an element in the first array = -1, then in the second it can be anything.

table = [
    [1, 0, -1],
    [-1, 0, 0],
    [-1, 0, 0]
]
tb = 

For example, there is a matrix table and tb
It is necessary to check that all elements of the array from table are in tb, taking into account the sequence and -1
Important note: -1 can only occur in table
If it were not for this nuance with -1, then I would do this
if all(t in tb for t in table):
   ...

Tell me how to make it much more beautiful? It is desirable in general in one line, but it is clear not to the detriment of readability

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MinTnt, 2021-06-11
@MinTnt

Well...

table = [
    [1, 0, -1],
    [-1, 0, 0],
    [-1, 0, 0]
]
tb = 


n = [[i for k,i in enumerate(x) if k not in d] in [[i for k,i in enumerate(l) if k not in d] for l in tb] for x, d in zip(table, [[k for k,i in enumerate(x) if i==-1] for x in table])]
print(n)

A
aRegius, 2021-06-11
@aRegius

from itertools import compress


def equal_but_one(data, storage, skip_value=-1):
    mask = [number != skip_value  for number in data]
    data_compressed = list(compress(data, mask))
    return data_compressed in (list(compress(item, mask)) for item in storage)

all(equal_but_one(data, tb) for data in table)

L
longclaps, 2021-06-11
@longclaps

One-liners suck

for row in table:
    print(not all(any((a - b) * (a + 1) for a, b in zip(row, r)) for r in tb))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question