Is there a way to create a list for each item in another list?

1

Suppose I have a list like this:

l=[20,30,40]

How do I create 3 lists like this:

l1=[2,0]
l2=[3,0]
l3=[4,0]

PS: I came up with this question to find how to multiply each digit of a number in a list. For example, from the list [22,23,24] I would like to obtain [4,6,8]. If there is a more efficient way to solve this, the explanation would be useful, thank you.

    
asked by Felka98 24.05.2018 в 04:22
source

2 answers

1
l=[20,30,40]
r = [[int(c) for c in str(e)] for e in l]
print(r)
[[2, 0], [3, 0], [4, 0]]

As you see, the trick is to pass each element ( e ) of the list l to string, then iterate through each of the characters ( c ) of that string and convert it to int . The syntax of list comprehensions allows you to express all of it in a single line. If you prefer more the syntax of loops it would be like that (much more complicated in my opinion):

r = []
for e in l:
  aux = []
  for c in str(e):
    aux.append(int(c))
  r.append(aux)
print(r)

The product

And answering your "true" question, calculate the product of the digits of each number, we have two approaches:

  • Use functional programming to operate on each item in the list r (which is itself a sub-list), reducing the sub-list to a single data (using functools.reduce() , and applying the operator of multiplication to all of them).
  • Use a for loop for the same thing.
  • Functional programming hides loops, allowing shorter programs, but sometimes more difficult to read. In the case of the function functools.reduce() is one of those cases in which, to make the program more compact, it loses legibility (in fact Guido himself, creator of python, discourages it). It would be like this:

    from functools import reduce
    from operator import mul
    
    l = [123, 35, 11111]
    r = [[int(c) for c in str(n)] for n in l]
    p = [reduce(mul, sublist) for sublist in r]
    print(p)
    
    [6, 15, 1]
    

    In this case a solution with loops is, for my taste, more readable:

    l = [123, 35, 11111]
    for e in l:
      product = 1
      for digit in str(e):
        product *= int(digit)
      print(product)
    
    6
    15
    1
    

    Note that both the functional version and the loop version serve data of any length.

        
    answered by 24.05.2018 / 09:29
    source
    1
    lista = [22, 23, 24]
    numIzq = []
    numDer = []
    
    # Calcula el digito de la izquierda (Solo menores a 100)
    for i in range(len(lista)):
        numIzq.insert(i, lista[i] * 0.1)
        numIzq[i] = int(numIzq[i] // 1)
    print(numIzq)
    
    # Calcula el digito de la derecha (solo menores a 100)
    for k in range(len(lista)):
        numDer.insert(k, numIzq[k] * 10)
        numDer[k] = lista[k] - numDer[k]
    print(numDer)
    
    # Sobreescribo la lista inicial con el culculo entre listas izq y der
    for j in range(len(lista)):
        lista[j] = numIzq[j] * numDer[j]
    print(lista)
    

    I only did it for numbers less than 100. If you understand the logic (and my way of coding) you can calmly do it for anyone. PS: Go putting print () in all places that could yield a result. In this way, you can visualize each one and understand the process

        
    answered by 24.05.2018 в 07:10