簡體   English   中英

以特定樣式打印 python 字典

[英]print python dictionary in particular style

如何以這個給定的模式打印 Python 字典。 我在面試中被問到這個問題,我無法解決。

輸入字典:

{"abc":{"def":{"ghi":{"jkl":{"mno":{"pqr":{"stu":{"vwx":{"yz":"you are finally here !!!"}}}}}}}}}

所需的 output:

{"abc":["def","ghi","jkl","mno","pqr","stu","vwx","yz"],
 "def":["ghi","jkl","mno","pqr","stu","vwx","yz"],
 "ghi":["jkl","mno","pqr","stu","vwx","yz"],
 "jkl":["mno","pqr","stu","vwx","yz"],
 "mno":["pqr","stu","vwx","yz"],
 "pqr":["stu","vwx","yz"],
 "stu":["vwx","yz"],
 "vwx":["yz"],
 "yz":["you are finally here !!!"]}

這是一個快速的遞歸解決方案:

from pprint import pprint

data = {"abc":{"def":{"ghi":{"jkl":{"mno":{"pqr":{"stu":{"vwx":{"yz":"you are finally here !!!"}}}}}}}}}

def a_particular_style(data):
    ret = {}
    for k, v in data.items():
        if isinstance(v, dict):
            d = a_particular_style(v)
            ret.update(d)
            ret[k] = list(reversed(d))
        else:
            ret[k] = [v]
    return ret

pprint(a_particular_style(data))
{'abc': ['def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz'],
 'def': ['ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz'],
 'ghi': ['jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz'],
 'jkl': ['mno', 'pqr', 'stu', 'vwx', 'yz'],
 'mno': ['pqr', 'stu', 'vwx', 'yz'],
 'pqr': ['stu', 'vwx', 'yz'],
 'stu': ['vwx', 'yz'],
 'vwx': ['yz'],
 'yz': ['you are finally here !!!']}

由於 dict 的每個“級別”都是從下一個級別構建的,因此如果從最小的 dict 底部開始,則更容易想象這是如何工作的:

print(a_particular_style({"yz":"you are finally here !!!"}))
# {'yz': ['you are finally here !!!']}

print(a_particular_style({"vwx":{"yz":"you are finally here !!!"}}))
# {'vwx': ['yz'], 'yz': ['you are finally here !!!']}    

print(a_particular_style({"stu":{"vwx":{"yz":"you are finally here !!!"}}}))
# {'stu': ['vwx', 'yz'], 'vwx': ['yz'], 'yz': ['you are finally here !!!']}

# etc

像這樣使用遞歸的簡單 5 行解決方案應該可以工作:

>>> input_dict = {"abc":{"def":{"ghi":{"jkl":{"mno":{"pqr":{"stu":{"vwx":{"yz":"you are finally here !!!"}}}}}}}}}
>>> reshaper = lambda x, y=[]: (y, x) if type(x) == str else my_func(x[list(x)[0]], y+list(x))
>>> final_list = reshaper(input_dict)
>>> final_dict = {key: final_list[0][i+1:] for i, key in enumerate(final_list[0][:-1])}
>>> final_dict.update({final_list[0][-1]: final_list[1]})
>>> final_dict
{'abc': ['def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz'], 'def': ['ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz'], 'ghi': ['jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz'], 'jkl': ['mno', 'pqr', 'stu', 'vwx', 'yz'], 'mno': ['pqr', 'stu', 'vwx', 'yz'], 'pqr': ['stu', 'vwx', 'yz'], 'stu': ['vwx', 'yz'], 'vwx': ['yz'], 'yz': 'you are finally here !!!'}

這種方法(我認為可能是最簡單的解決方案之一)的作用是獲取字典並在字典中的字典中遞歸,將每個鍵附加到列表中,直到我們得到最終的字符串。

然后它獲取這個列表並遍歷列表以創建一個字典,其中值是索引大於鍵索引的切片。

a = {"abc":{"def":{"ghi":{"jkl":{"mno":{"pqr":{"stu":{"vwx":{"yz":"you are finally here !!!"}}}}}}}}}

d = {}
loop = True
while loop:
    for i in a:
        if i == 'yz':
            loop = False
        d[i] = []
        a = a[i]

for i in d:
    pos = list(d.keys())
    d[i] = pos[pos.index(i) + 1:]
print(d)

這是一個糟糕的解決方案。 我以某種方式使它工作,幾乎 如果有效,請點贊!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM