How to split the instance of a class through outrw

Asked

Viewed 24 times

0

To be more precise, yesterday I opened a question asking for help in creating a class that acted as a creator of dictionaries, and promptly I was answered, follows the class: `

class tuplas:

def __init__(self, keys, palavras):
    self.keys = list(keys)
    self.palavras = list(palavras)

def __getitem__(self, key):
    index = self.keys.index(key)
    return self.palavras[index]

def to_dict(self):
    return {key: palavra for key, palavra in zip(self.keys, self.palavras)}

def __repr__(self):
    return f'Tabela Completa: <{self.to_dict()}>'

What was a hand on the wheel, because I was able to advance the project well, but I came across another difficulty, I need a class that divides the dictionary that resulted from the above class into several parts, according to the information passed by the user "quantidadeP", follows the draft:

class pagina(tuplas):

def __init__(self, keys, palavras, quantidadeP):
    self.quantidadeP = quantidadeP

def __getitem__(self, key):
    index = self.keys.index(key)
    return self.palavras[index]

def quebra(self):
  • 1

    What does "split into several parts" mean? What happens if the user passes keys as a size 10 sequence, and quantidadeP=3?

1 answer

1


Here is a basic example capable of dividing the list of tuples livro in a list of dictionaries páginas, with the amount of keys quantidadeP:

chaves = ['one','two','three','four','five','six','seven','eight','nine','ten']
valores = ['um','dois','tres','quatro','cinco','seis','sete','oito','nove','dez']

quantidadeP = 3

livro = list(zip(chaves, valores))

paginas = [dict(livro[i:i+quantidadeP]) for i in range(0, len(livro), quantidadeP)]

print(paginas)

Exit to quantidadeP = 2:

[
    {'one': 'um', 'two': 'dois'},
    {'three': 'tres', 'four': 'quatro'},
    {'five': 'cinco', 'six': 'seis'},
    {'seven': 'sete', 'eight': 'oito'},
    {'nine': 'nove', 'ten': 'dez'}
]

Exit to quantidadeP = 3:

[
    {'one': 'um', 'two': 'dois', 'three': 'tres'},
    {'four': 'quatro', 'five': 'cinco', 'six': 'seis'},
    {'seven': 'sete', 'eight': 'oito', 'nine': 'nove'},
    {'ten': 'dez'}
]

Exit to quantidadeP = 5:

[
    {'one': 'um', 'two': 'dois', 'three': 'tres', 'four': 'quatro', 'five': 'cinco'},
    {'six': 'seis', 'seven': 'sete', 'eight': 'oito', 'nine': 'nove', 'ten': 'dez'}
]

Encapsulating your idea in a class:

class Livro:
    def __init__(self, chaves, valores, quantidade):
        aux = list(zip(chaves, valores))
        self.livro = dict(aux)
        self.paginas = [dict(aux[i:i+quantidade])
            for i in range(0, len(aux), quantidade)]

    def __getitem__(self, key):
        return self.livro[key]

    def __repr__(self):
        return f'Tabela Completa: <{self.livro}>'

    def get_pagina(self, n):
        return self.paginas[n]

Testing:

chaves = ['one','two','three','four','five','six','seven','eight','nine','ten']
valores = ['um','dois','tres','quatro','cinco','seis','sete','oito','nove','dez']

x = Livro(chaves, valores, 3)

print(x)
print(x['five'])
print(x.get_pagina(2))
  • Yes, I already knew this exit, but for the requirement of the teacher, I need to implement it in a class, can help me ?

  • @barrosfilho_: Made!

  • Master, it worked more than perfectly! Very obg! But for learning reasons, can you do me two favors ? The first would be to comment on the code above, per line. And the second would be, more a doubt. It is plausible to pass to the book class already the result of the class Tupla (that q is in the question), because it would already cut halfway, or so is better ?

Browser other questions tagged

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