简体   繁体   English

从 python 中的字典创建子字典

[英]Create sub-dictionary from dictionary in python

Function criarB return: Function criarB 返回:

{269476: 200129, 209624: 200129 ...}
 200129 position in aux2, '200129 and 209624 are keys'

How to split dict according to keys, if keys % 10 = 0 must be stored in list[0]如何根据键拆分dict,如果keys % 10 = 0 必须存储在list[0]中

list = [[], [], ...]

Class complete: Class 完成:

class Buckets:

def __init__(self, keys, palavras, tamanhoP):
    self.listaBuckts = dict()
    self.listaHash = list()
    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 self.listaBuckts

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

    for r in range(0, len(buckets)):
        for s in range(0, len(self.listaBuckts)):
            if s % 3 == 0:
                buckets[r].append([v + count for v in self.listaHash[s:s + 3]])
        count += 1
    return buckets[0]

the "indexing" function uses a list that contains only indexes, how do I use the keys of the self.buckets dict and divide the dict according to the function “索引” function 使用仅包含索引的列表,我如何使用 self.buckets 字典的键并根据 function 划分字典

Attempt:试图:

class Buckets:

def __init__(self, keys, palavras, tamanhoP):
    self.listaBuckts = dict()
    self.listaHash = list()
    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 self.listaBuckts

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

    for x in self.listaBuckts:
        i = x % 10
        test[i].append([x, dict[x]])
    return test[0]

If you want to split the dictionary into two lists based on keys.如果要根据键将字典拆分为两个列表。 I think the follow can work我认为以下可以工作

dict #The dictionary returned from function crairB
a = [] #List to save keys and values for key%10==0
b = [] #List for key%10!=0
for x in dict:
    if x%10==0:
        a.append([x,dict[x]]) #Here I have saved both keys and values in the list, you can edit according to your need.
    else:
        b.append([x,dict[x]])

Edit:编辑:

If you have to split the data into 2D list with rows equal to keys % 10, then I think the following can work.如果您必须将数据拆分为行等于键 % 10 的二维列表,那么我认为以下方法可以工作。

list = [[] for _ in range(10)]

for x in dict:
    i = x%10
    list[i].append([x,dict[x]])

You could use a list comprehension:您可以使用列表理解:

listOfDicts = [{k:v for k,v in dictionary.items() if k%10==i} for i in range(10)]

or a loop:或循环:

listOfDicts = [ dict() for _ in range(10) ]
for key,value in dictionary.items():
    listOfDicts[key%10].update({key:value})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM