Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
multiply all lists in list of lists

I have a list of masks and I want to obtain the resulting mask by multiplying all of them. My Donkey Kong approach is the following:

a = [[1, 1], [1, 0], [1, 0]]
b = a[0]
for i in range(1, len(a)):
    b = b * np.array(a[i])

which I think it works as returns [1,0] as value of b.

Is there a nicer way of doing this?


EDIT: I am looking for common ranges in the mask. To find all the non zero ranges I do the following: label = 0

for i in range(1, len(labels)):
   label = label + np.array(labels[i])
label = [1 if x > 0 else 0 for x in label]
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Take a look at np.prod, which returns the product of array elements over a given axis:

import numpy as np
a = [[1, 1], [1, 0], [1, 0]]
np.prod(a, axis=0)
over 4 years ago · Santiago Trujillo Relatório

0

I see that you are already using numpy so it can be used like other answers suggested. But still, a nice built-in solution only using reduce can be:

from functools import reduce

a = [[1, 1], [1, 0], [1, 0]]

def element_wise_multiply(list1, list2):
    return [x*y for x,y in zip(list1, list2)]

b = reduce(element_wise_multiply, a)

# Or as a lambda:
b = reduce(lambda list1, list2: [x*y for x,y in zip(list1, list2)], a)

This takes every two sub-lists and reduces them to one by multiplying all index-matching elements by using zip. It also gives:

[1, 0]
over 4 years ago · Santiago Trujillo Relatório

0

A built-in way with all that short circuits (no other answer short circuits now):

>>> [all(column) for column in zip(*a)]
[True, False]

where zip(*a) kindof transposes to produce "columns" as sublists here and we check if all entries are truthful. all has a short circuting behaviour.

You can int(all(sub)) to cast results to integer after all'in the sublists.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda