How to multiply the number of lists by an integer?

Asked

Viewed 675 times

1

If I have an input integer and an input list, it is possible to multiply the integer by the list?

M = int(input())

C = (eval('[' + input() + ']'))

That is, to have '’M'' lists according to the integer I put in M, hence I was forming the lists. The integer M defines the number of lists.

ex: M =2 C = [1,2,3] [4,5,6]

  • 1

    I read and reread several times the title of the question and the text of the question (I believe a lot of people did it) itself and I still can’t understand what you really want. You could rewrite your text?

  • Okay, it’s clearer now?

  • Aren’t you missing your multiplication operation? The answer wouldn’t be 2,4,6?

  • No, it’s like if M = 2, I could put random values but only in 2 lists. The number M defines the number of lists.

  • You through an M defines how many list you will read? That’s it?

  • Exact, but all with the same number of elements.

  • What is the doubt?

  • how do I do this, '’M'' lists with 3 variables. M being an incial input, and then the lists the second input.

  • All lists in just one input? There are easier methods for this.

  • That. Type x = int(input()) ; x=10 ; c = Eval('[' + input() + ']') ; ''2,3,4 ..... 5,8,2'' 10 such lists

  • It must be so?

  • No, that’s the way I thought

Show 7 more comments

2 answers

1


A better way to do this, always try to do something in Python the easiest way possible.

n = int(input()) #Aqui você lê o numero de listas que você deseja.
colecao = [] #Aqui você cria uma matriz vazia.

for _ in range(n): #Um laco de repeticao para ler as listas.
  x = list(map(int,input().split(','))) #Lê a lista, splita ela pela virgula e transforma tudo em int, depois transforma no tipo lista.
  colecao.append(x) #Adiciono ao fim da coleção
print(colecao) #Printo a coleção

Example of Inputs and Outputs:

    3  
1,2,3  
4,5,6  
7,8,9 
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  • Thank you very much.

  • If it is the answer you want please confirm it.

  • One last question, could I sum up the elements of a single position of the following lists? like 1+4+7 (position 0)

  • Create a repeat loop that passes through all lists and a variable to store the sum. Start it with 0, loop through all lists by summing.

  • Thank you, my man.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.