gpt4 book ai didi

python - 如何从数组生成字典的字典?

转载 作者:行者123 更新时间:2023-12-02 06:30:35 25 4
gpt4 key购买 nike

我有一个像这样的字符串数组:

Array = ['ABC', 'DEF', 'GHI']

如何将其映射到具有以下形状的字典的字典中?

dictionary = {
'ABC': {'DEF': auxFunction(ABC,DEF), 'GHI': auxFunction(ABC,GHI)},
'DEF': {'ABC': auxFunction(DEF,ABC), 'GHI': auxFunction(DEF,GHI)},
'GHI': {'ABC': auxFunction(GHI,ABC), 'DEF': auxFunction(GHI,DEF)},
}

最佳答案

你可以用两个字典理解来做到这一点

dictionary = {name: {x: auxFunction(name, x) for x in array if x != name} for name in array}

{'ABC': {'DEF': auxFunction(ABC, DEF), 'GHI': auxFunction(ABC, GHI)},
'DEF': {'ABC': auxFunction(DEF, ABC), 'GHI': auxFunction(DEF, GHI)},
'GHI': {'ABC': auxFunction(GHI, ABC), 'DEF': auxFunction(GHI, DEF)}}

关于python - 如何从数组生成字典的字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60146105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com