0
Good guys, even for help here from the forum, I got a function that creates sub-list from sub-lists according to the division by 10, is easier to understand with the code:
def indexar(values):
# Contador iniciado em 0
count = 0
# Lista com até 10 listas
buckets = [[] for _ in range(10)]
# O índice vai de 0 até o tamanho da lista buckets
for r in range(0, len(buckets)):
# O índice vai de 0 até o tamanho da lista values
for s in range(0, len(values)):
# Verifica se r é multiplo de 3
if s % 3 == 0:
# Faz uma list comprehension pra armazenar em buckets no índice r
# os valores dentro do range atual começando pelo
# índice s e finalizando no índice s + 3
# e somando cada valor de values com o contador
buckets[r].append([v + count for v in values[s:s + 3]])
# Incrementa ao contador + 1
count += 1
# Retorna lista
return buckets
foo = [0, 100, 10]
print(indexar(foo))
# Saída: [[[0, 10, 20], [30, 40, 50], [60, 70, 80], [90]], [[1, 11, 21], [31, 41, 51], [61, 71, 81], [91]], [[2, 12, 22], [32, 42, 52], [62, 72, 82], [92]], [[3, 13, 23], [33, 43, 53], [63, 73, 83], [93]], [[4, 14, 24], [34, 44, 54], [64, 74, 84], [94]], [[5, 15, 25], [35, 45, 55], [65, 75, 85], [95]], [[6, 16, 26], [36, 46, 56], [66, 76, 86], [96]], [[7, 17, 27], [37, 47, 57], [67, 77, 87], [97]], [[8, 18, 28], [38, 48, 58], [68, 78, 88], [98]], [[9, 19, 29], [39, 49, 59], [69, 79, 89], [99]]]
But the curiosity hit, and I was in doubt whether to get the same result of the above function, not in a simple list, but in a list of Dict’s, using the elements Keys as parameters:
dict = {0: 123, 10: 456, 20: 789 ...}
onde as keys são os dois primeiros números dos elementos
The way out I’d like to be:
[[[0: 123, 10: 456, 20: 789], ...]]]
I’ve managed to create dictionaries from the rest of the division:
listOfDicts = [{k:v for k,v in dictionary.items() if k%10==i} for i in range(10)]
Now we have to divide the sublists ...
class buckets:
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]))
def criarB(self):
for i, pag in enumerate(self.aux2):
for v in pag.keys():
self.listaBuckts[v] = i
return '\n DICT COM TODOS OS ELEMENTOS DOS BUCKETS CRIADOS!'
# É criada uma função que receberá alguns parâmetros, como o dicionário, o tamanho do bucket e o tamanho do sub-dicionário.
def index_dict(self, tam_subdict):
# Contador iniciado em 0
count = 0
# Criar de 0 até tam_bucket de listas dentro do bucket
buckets = [[] for _ in range(10)]
# O índice r vai de 0 até tam_bucket
for r in range(0, len(buckets)):
# O índice vai de 0 até o tamanho das keys do dicionário com o step igual a tam_subdict
for s in range(0, len(list(self.listaBuckts.keys())), tam_subdict):
# Faz uma dict comprehension pra armazenar em no índice r do bucket
# e somando cada valor de k com o contador count
buckets[r].append(
{k + count: v for k, v in self.listaBuckts.items()}
)
# Incrementa ao contador + 1
count += 1
# Retorna lista com os dicionários
return buckets[0]
Victor, I tried to apply your role to my class, but do you believe that with a much larger dictionary, it leads to a memory error? Is there any way you can help me? I posted the full class ...
– jusintique
What is the output error?
– Victor Manhani
It crashes the pc for lack of memory,
– jusintique
Maybe it’s the size of the dictionary ?
– jusintique
This may be happening by the size of the dictionary you are passing to the function, I recommend you try to partition this dictionary into a separate file and call it so you have the expected output slowly, not to have a memory stackoverflow.
– Victor Manhani
Give me an example of how to do it ? I’m imagining a thousand things haha
– jusintique
I believe hehehe, well, your data source may be a txt file, or a json, or even a database (most recommended for doing better memory management). I ask to create another question here on Stackoverflow or you can check this link: https://stackoverflow.com/questions/519633/lazy-method-reading-big-file-in-python It will help you on how to load large files with little disk space or ram.
– Victor Manhani