1
Time, has a class that creates dicts and stores them in a list slot:
class Pagina:
def __init__(self, keys, palavras, tamanhoP):
aux = list(zip(keys, palavras))
self.paginas = list()
for i in range(0, len(aux), tamanhoP):
self.paginas.append(dict(aux[i:i + tamanhoP]))
def __getitem__(self, num):
return f'Página {num}: {self.paginas[num]}'
def __repr__(self):
return f'Todas as páginas: {self.paginas}'
def get_key_pagina(self, num):
for i in range(0, len(self.paginas)):
if num in self.paginas[i]:
return f'Palavra na Página: {i}'
But now I need to create a new Dict in receiving the same Keys and the position in Dict of the above class, it is possible ?
Follow my attempt so far:
def novoDict(self, keys):
aux = list()
for ii in keys:
for i in range(0, len(self.paginas)):
if ii in self.paginas[i]:
aux.append(dict(aux[ii: i]))
return f'{aux[0]}'
But the following error returns to me:
dictionary update sequence element #0 has length 0; 2 is required
What would the new Dict look like, ex:
{key 0: Página 0}
Test example:
listaP = ['x', 'y', 'z', 'w']
listaI = [1, 2, 3, 4]
Instantiating the object:
x = Pagina(listaI, listaP, 2)
Exit:
Página 0: {1: 'x', 2: 'y'}
Página 1: {3: 'z', 4: 'w'}
What I want to get:
{'x': 0, 'y': 0, 'z': 1, 'w': 1}
I couldn’t understand what you wanted to do but
zip()
shall be applied on a rolling basis and in their caseii
is an element ofkeys
which must be of thenumpy.int32
andi
is the typeint
.– Augusto Vasques
So friend, I want to create a Dict like this: {key 0 : Key position in the list "pages"}, I saw the zip error, I tried to modify to the following: (I edited in the question), but now the program takes a long time to give me an answer to the function.
– barrosfilho_
Someone can help?
– barrosfilho_
It would not be the case to replace all this function
novoDict(self, keys)
by that line of codelist(zip(keys,self.paginas))
? I made a simplified example of what I understood of your problem https://repl.it/repls/BriskFaintFormats see if I interpreted it correctly?– Augusto Vasques
Senho Augusto, at first I have a list of Dict like this: {key: word}, where depending on the page size (information given by the user), I divide this list into slices and store in sub-lists (pages). From this I want to create a new dictionary only with the key and its position in the new list (i.e., on the page), e.g.: {key: page 0}
– barrosfilho_
A practical example for testing is missing. It would be nice to have data samples and an example showing the methods being applied to these data.
– Augusto Vasques
Let’s go continue this discussion in chat.
– barrosfilho_
I added a test example, if you can take a look
– barrosfilho_