Inserted into a list of lists in python

0

I would like to populate a list where each position is a list.

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

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

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

but my problem 'and insert in the internal list, since every time I insert, all internal lists are of the same value. ex: lista = [[1,2],[1,2]]

NOTE: the input can change as soon as the internal list n has a defined size

    
asked by anonymous 22.06.2018 / 04:35

3 answers

1

First, because of the reported problem with lists getting the same values, you're initializing in the wrong way. If you do:

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

You will create a list with 13 lists, but all will be the same reference. If you add a number to 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.

Lists of empty 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 look the way you expect:

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

So, just add the numbers in their positions:

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

Looking like this:

[[], [27, 92], [], [], [95], [44, 70], [45, 97], [], [73], [], [49], [], []]
    
22.06.2018 / 14:59
2

The problem is in creating lists of lists. If you had created the list of lists as put up, with all built-in lists explicitly created, the problem would not happen - why each inner 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 full-length multiplier as a way to concatenate a sequence with itself. In this scenario, all entries in the built-in list 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, since you only want mod% 13, the solution might be to simply declare the internal lists explicitly, as you put in the question statement. But even that is a bit inconvenient and unreadable (someone reviewing the code would have to keep counting the 13 repetitions) - and for larger numbers it is not feasible.

The correct thing is to use 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 for in a single expression:

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

In this case, the part of the expression [] that creates a new list is executed once for each value of i, and becomes a member of the external list - and the problem you describe will not happen. >     

22.06.2018 / 14:56
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 I said before, I believe that dictionaries can be an interesting choice for a key / value case. Unless you have another specific reason to use the list.

    
22.06.2018 / 06:20