Slice and store Ict in pyhon

Asked

Viewed 79 times

0

I have the following class:

class Buckts:

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

        aux = list(zip(keys, palavras))
        self.aux2 = list()

        for i in range(0, len(aux), tamanhoP):
            self.aux2.append(dict(aux[i:i + tamanhoP]))

That in the method below returns me a Dict (key + position of the same in aux2)

   def criarB(self):
        for i, pag in enumerate(self.aux2):
            for v in pag.values():
                self.listaBuckts[v] = i
        return self.listaBuckts

The goal now would be to separate the elements of Dict from a function, all elements must be stored according to the rest of the division of its KEY by 10, ie if the key = 10 it must be stored in bucktes[0] since the rest of the division is = 0.


    def indexar(self):
        buckets = [[] for _ in range(10)]

        for i in self.keys:
            for ii in range(10):
                if i % 10 == ii:
                    buckets[ii].append(i)
        return buckets

The above method is capable of performing the task, but only when I search in the Keys list and not in the Dict list. Buckts, from this I would like to break the Dict and not just the list of Keys:

If you need any further details, please don’t hesitate to ask.

Thanks in advance!

1 answer

0

Hey there, buddy! I believe you’re drawing a very complex line of reasoning for something that should be simple. If ,in the end, the goal is to separate into lists, I believe you should do this in a generic way:

def separador(item):
    return item%10

this will return an int that you can associate to your location where you will split.

Browser other questions tagged

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