繁体   English   中英

词典列表到相关词典列表

[英]List of Dictionaries to a lists of correlated dictionaries

我有这个清单:

list = [ { 1: [3], 2: [6] }, { 3: [4,5], 6: [8] }, { 4: [7], 5: [7] } ]

它代表了我拥有的所有对象的对象ID之间的整体相关性。

我要从此列表中提取的内容:

l1 = [ { 1: [3] }, { 3: [4,5] }, { 4: [7], 5: [7] } ]

l2 = [ { 2: [6] }, { 6: [8] } ]

让我给一些解释!

为了清楚起见,请在下面找到图片:

在此处输入图片说明

因此,您可以看到,每个对象都由其ID表示。 要形成“列表”,我从左侧外围对象的ID开始,此类ID将是第一个字典(“列表”的关键字)的键,这些键的值是连接在列表上的对象的ID。正确(我们将值放在列表中)。 这些值将作为第二个字典的键(如果您从上一个字典重复了值,请不要重复键,只需对它一次即可),并重复这样的步骤,直到到达最后一个ID。

这些对象根据其ID之间的关系形成图案。 在这个问题上,我将得到它们正在形成的图案数量。 在给链接的图片中,它们正在形成两种模式。

模式:具有以某种方式彼此关联的ID的对象组

它为我工作:

my_dict = [ { 1: [3], 2: [6] }, { 3: [4,5], 6: [8] }, { 4: [7], 5: [7] } ]
new_dict = []

# read
def read(n, p, dictionary):
  a = list(dictionary[n])[p]
  if len(new_dict)<=p:
    new_dict.append([])
  new_dict[p].append({a : dictionary[n][a]})
  bucle(dictionary[n][a], n+1, p, dictionary)
def bucle(key, n, p, dictionary):
  try:
    for keys in key:
      new_dict[p].append({keys: dictionary[n][keys]})
      bucle(dictionary[n][keys], n+1, p, dictionary)
  except:
    pass
read(0, 0, my_dict)
read(0, 1, my_dict)
print(new_dict)

结果:

new_dict = [[{1: [3]}, {3: [4, 5]}, {4: [7]}, {5: [7]}], [{2: [6]}, {6: [8]}]]
new_dict[0] = [{1: [3]}, {3: [4, 5]}, {4: [7]}, {5: [7]}]
new_dict[1] = [{2: [6]}, {6: [8]}]

暂无
暂无

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

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