2016-11-01 8 views
0

サブ(親子)のようなキーにIDを格納するのに問題があります。私はそれに数時間を費やし、これを達成する方法を考え出すことができませんでした。私が期待している出力は、この記事の最後です。どんな助けも素晴らしいだろう。出力は(同様にで)このように見えるようにしようとするとグループアイテムをまとめて辞書whileループ

import sys 
import collections 

dict = collections.OrderedDict() 
dict["A.1"] = {"parent_child":0} 
dict["A.1.1"] = {"parent_child":1} 
dict["A.1.1.1"] = {"parent_child":2} 
dict["A.1.1.2"] = {"parent_child":2} 
dict["A.1.1.3"] = {"parent_child":2} 
dict["A.1.2"] = {"parent_child":1} 
dict["A.1.2.1"] = {"parent_child":2} 
dict["A.1.2.2"] = {"parent_child":2} 
dict["A.1.2.2.1"] = {"parent_child":3} 
dict["A.1.2.2.2"] = {"parent_child":3} 
dict["A.1.2.3"] = {"parent_child":2} 
dict["A.1.3"] = {"parent_child":1} 
dict["A.1.4"] = {"parent_child":1} 


print(dict) 

new_dict = {} 

p = 0 # previous index 
i = 0 # current 
n = 1 # next index 

current_PC = 0 # current parent_child 
next_PC = 0 # next parent_child 

previous_id = "" 
current_id = "" 
next_id = "" 

change_current = True 
change = True 

lst = [] 

while(True): 
    if change_current: 
     current_id = dict.keys()[i] 
     current_PC = dict.values()[i]["parent_child"] 
     change_current = False 

    try: 
     next_id = dict.keys()[n] 
     next_PC = dict.values()[n]["parent_child"] 
    except: 
     pass # it will go out of index 

    print("KEY {0}".format(current_id)) 

    if next_PC > current_PC: 
     if next_PC - current_PC == 1: 
      lst.append(next_PC) 
      next_PC += 1 
      print("next_PC: {0}".format(next_PC)) 

    if next_PC == current_PC: 
     new_dict[current_id] = lst 
     lst = [] 
     break 

print(new_dict) 

、new_dictは、次のようになります。これは、あなたが求めている出力を与える

new_dict["A.1"] = ["A.1.1", "A.1.2", "A.1.3", "A.1.4"] 
new_dict["A.1.1"] = ["A.1.1.1", "A.1.1.2", "A.1.1.3"] 
new_dict["A.1.1.1"] = [] 
new_dict["A.1.1.2"] = [] 
new_dict["A.1.1.3"] = [] 
new_dict["A.1.2"] = ["A.1.2.1", "A.1.2.2", "A.1.2.3"] 
new_dict["A.1.2.1"] = [] 
new_dict["A.1.2.2"] = ["A.1.2.2.1", "A.1.2.2.2"] 
new_dict["A.1.2.2.1"] = [] 
new_dict["A.1.2.2.2"] = [] 
new_dict["A.1.2.3"] = [] 
new_dict["A.1.3"] = [] 
new_dict["A.1.4"] = [] 

答えて

1

。あなたが希望する出力で{"parent_child":...}が表示されなかったので、何も進んでいませんでした。

options = ["A.1","A.1.1","A.1.1.1","A.1.1.2","A.1.1.3","A.1.2","A.1.2.1","A.1.2.2","A.1.2.2.1","A.1.2.2.2","A.1.2.3","A.1.3","A.1.4"] 


new_dict = {} 

for i, key in enumerate(options): 
     new_dict[key] = [] 
     ls = [] 
     for j, opt in enumerate(options): 
      if (key in opt) and (len(opt)-len(key)==2): 
       new_dict[key].append(opt) 
    print(new_dict) 

EDIT

彼は `A.1.10`を持っている場合はどう@Ranbir Aulakh

options = ["A.1","A.1.1","A.1.1.1","A.1.1.2","A.1.1.3","A.1.2","A.1.2.1","A.1.2.2","A.1.2.2.1","A.1.2.2.2","A.1.2.3","A.1.3","A.1.4"] 


new_dict = {} 

for i, key in enumerate(options): 
     new_dict[key] = [] 
     ls = [] 
     for j, opt in enumerate(options): 
      if (key in opt) and (len(opt.split("."))-len(key.split("."))==1):#(len(opt)-len(key)==2): 
       new_dict[key].append(opt) 
    print(new_dict) 
+1

のコメントを使用していますか?それはA.1の下に置かれません。おそらく 'len(opt)-len(key)== 2'を' len(opt.split( "。")) - len(key.split( "。"))== 1'に変更したいと考えています。それはそれを修正できるはずです。 –

関連する問題