Problem in Dict class and merging large amount of elements per Python Dict

Asked

Viewed 105 times

0

Hi, I’m having trouble with dict. I have two lists: lista1 and lista2, both the same size. When I execute the command: dicionario = dict(zip(lista1, lista2)) produces a dictionary "smaller" than the size of the list. I did a lot of tests and that’s really what happens. In each loop I made a print of the size of lista1 and lista2 and they are always the same size. I have separated the operations of zip and dict and I always printed both. Result: the dict is not concatenating correctly. It is worth remembering that the keys to my dict sane double and the values are lists of lists (i.e., lista1 = 52.84 and lista2 = [[1, 2, 3],[1, 2, 4],[5, 6, 7]] for example). Can the fact of being a large amount of data influence? In this case each list has 100 positions, as I said. A lista1 is a list of 100 positions composed of values double and the lista2 is a list of lists (lista2 = [list1, list2, list3, ..., list_100]) which has 100 positions and each element [list1, list2, list3, ..., list_100] etc. has size 80. Could I explain? Urgent help haha

  • 1

    It is possible that the supposed result (Dict) has equal keys, so only the last key/value is considered

1 answer

0

Allan,

According to the documentation of the zip function:

The iterator stops when the shortest iterable input is exhausted.

Therefore, there must be some error in the validation of your data that is not able to identify that one of the lists is smaller than the other. As for the size of the data, I replicated your problem using 100 times larger lists to demonstrate that there are no problems.

# Criamos a lista de floats com 10000 posições
lista1 = list(np.random.random_sample(10000))
print(len(lista1) == 10000)
True

# Segunda lista de 10000 listas com 8000 posições cada.
lista2 = [[' '] * 8000 for x in range(0, 10000)]
print(len(lista2) == 10000)
True
print(all([len(x) == 8000 for x in lista2]) == True)
True

# Geramos o dicionário
dicionario = dict(zip(lista1, lista2))
print(len(dicionario) == 10000)
True

# Validamos os dados
print(all([len(y) == 8000 for x, y in dicionario.items()]))
True

In this way we validate the obtaining of a dictionary with 10000 keys, each corresponding to a list of 8000 positions.

Browser other questions tagged

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