Inserted in a list of lists in python

Asked

Viewed 5,999 times

0

I would like to fill in a list where each position of the same was a list.

lista = [[], [], []]

have an input: 44 45 49 70 27 73 92 97 95

and I would like to list the numbers so that the Index is mod of each number by 13.

Ex: 44%13 = 5
27%13 = 1

lista = [[ ],[27,92],[ ],[ ],[95],[44,70],[45,97],[ ],[73],[ ],[49],[ ],[ ]]
          0     1     2   3    4     5       6     7    8   9   10   11  12

however my problem 'and insert in the internal list, since every time q I will insert, all internal lists have the same value. ex: lista = [[1,2],[1,2]]

OBS.: the input can change the internal list soon n has a set size

3 answers

2

The problem lies in its creation of lists. If you had created the list as you put it above, with all internal lists explicitly created, the problem would not happen - because each internal list is a separate object:

In [1]: a = [[], []]

In [2]: a[0] is a[1]
Out[2]: False

In [3]: a[0].append("teste")

Out[4]: [['teste'], []]

But to shorten the code, you must have used integer multplication as a way to concatenate a sequence with itself. In this scenario, all internal list entries are references to the same external list:

In [6]: b = [[],] * 2

In [7]: b
Out[7]: [[], []]

In [8]: b[0] is b[1]
Out[8]: True

In [9]: b[0].append("teste")

In [10]: b
Out[10]: [['teste'], ['teste']]

In your case, as only mod %13 wants, the solution could be to simply state the internal lists explicitly, as you put in the question statement. But, even this is a little inconvenient and little readable (someone reviewing the code would have to keep counting the 13 repeats) - and for larger numbers is unfeasible.

The correct then is to use one for so that a new list is created at each loop execution. In the case of Python we have the convenient syntax of list-comprehensions that allows the for in a single expression:

lista = [[] for i in range(13)] 

In that case, the part of the words [] which creates a new list runs once for each value of i, and becomes a member of the external list - and the problem you describe will not happen.

1


First, by the problem reported about lists being at the same values, you are getting started wrong. If you do:

>>> lista = [[]]*13
>>> print(lista)
[[], [], [], [], [], [], [], [], [], [], [], [], []]

You will create a list with 13 lists, but they will all be the same reference. If you add a number in one of them:

>>> lista[0].append(1)
>>> print(lista)
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]

All will receive the value.

Empty list lists (Python)

You need to do:

>>> lista = [[] for _ in range(13)]

So each sub-list will be an independent list and when you add a value, it will be the way you expect it:

>>> lista[0].append(1)
>>> print(lista)
[[1], [], [], [], [], [], [], [], [], [], [], [], []]

So just add the numbers in the respective positions:

for numero in numeros:
    lista[numero % 13].append(numero)

Thus remaining:

[[], [27, 92], [], [], [95], [44, 70], [45, 97], [], [73], [], [49], [], []]

0

#INPUT

listaNum = '44 45 49 70 27 73 92 97 95'.split(' ')

#LIST

listaRes = []
for num in listaNum:
    listaRes.append([int(num) % 13, int(num)])
print(listaRes)

#DICTIONARY

dictRes = dict()
for num in listaNum:
    dictRes[int(num) % 13] = int(num)
print(dictRes)

As already commented, I believe that dictionaries can be an interesting choice for a key/value case. Unless you have another specific reason to use the list.

  • 1

    That’s not exactly his problem! He needs to add num in a list in the index int(num) % 13, what would have the list he put in question: [[ ],[27,92],[ ],[ ],[95],[44,70],[45,97],[ ],[73],[ ],[49],[ ],[ ]]

Browser other questions tagged

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