嵌套字典不按要求更新

时间:2019-09-29 10:18:37

标签: python python-3.x list dictionary

我正在尝试创建一个嵌套字典,该字典具有子字典键作为列表的其余元素。以更简单的方式进行解释:

必填输出:

payable = { 'A':{'B':0,'C':0},'B':{'A':0,'C':0}....}

输出:

payable = { 'A':{'B':0,'C':0,'A':0},'B':{'A':0,'C':0,'B':0}....}

我不需要 'A' 主键下的 'A' 子键。我该如何解决?

names = ['A', 'B', 'C']  
payable = dict.fromkeys(names, {})  
for mainKey in payable.keys():
    for subKey in names:
        if(mainKey != subKey):
            payable[mainKey][subKey] = 0 
print(payable) 

3 个答案:

答案 0 :(得分:5)

使用dict.fromkeys创建时,所有键都在同一个dict上。

尝试以payable的理解来创建dict(换句话说,为每个键新建dict),它可以正常工作:

names = ['A','B','C']  
payable= {k:{} for k in names}
for mainKey in payable.keys():
    for subKey in names:
        if(mainKey!=subKey):
            payable[mainKey][subKey]=0 
print(payable) 

输出:

{'A': {'B': 0, 'C': 0}, 'B': {'A': 0, 'C': 0}, 'C': {'A': 0, 'B': 0}}

答案 1 :(得分:2)

像这样的简单dict理解也可以工作:

sudo tmutil listlocalsnapshots /
com.apple.TimeMachine.2019-09-25-135035
com.apple.TimeMachine.2019-09-28-232824
com.apple.TimeMachine.2019-09-29-012547
com.apple.TimeMachine.2019-09-29-022054
com.apple.TimeMachine.2019-09-29-031621
com.apple.TimeMachine.2019-09-29-052318
com.apple.TimeMachine.2019-09-29-070831
com.apple.TimeMachine.2019-09-29-092229
com.apple.TimeMachine.2019-09-29-101756
com.apple.TimeMachine.2019-09-29-114952

tmutil deletelocalsnapshots 2019-09-25-135035
tmutil deletelocalsnapshots 2019-09-28-232824

说明:

  • 使用列表切片获取所有其他元素:here is the moving div <html> <head> <title>Move Div</title> <script language ="javascript"> <!-- function MoveDiv() { div = document.getElementById("myDiv"); div.style.left = parseInt(div.style.left) + 100 + "px"; } //--> </script> </head> <body> <input type="button" value="Move Div" onclick="MoveDiv()" /> <div id="myDiv" style="border: 10px solid black; background-color: Yellow; width: 100px; height: 30px; position: absolute; top: 1px; left: 100px;"></div> </body> </html>
  • 使用Check this将字典值初始化为零
  • 使用dict.fromkeys遍历<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> here is the moving div <html> <head> <title>Move Div</title> <script language ="javascript"> <!-- function MoveDiv(){ if ($('#myDiv').css == marginTop: '-=15px' ) { div = document.getElementById("myDiv"); div.style.left = parseInt(div.style.left) + 100 + "px"; }} //--> </script> </head> <body> <input type="button" value="Move Div" onclick="MoveDiv()" /> <div id="myDiv" style="border: 10px solid black; background-color: Yellow; width: 100px; height: 30px; position: absolute; top: 1px; left: 100px;"></div> </body> </html>的索引和项目

另外,如果我们不关心键顺序,则可以使用集差异,如enumerate在注释中所示:

>>> names = ['A', 'B', 'C']
>>> {x: dict.fromkeys(names[:i] + names[i+1:], 0) for i, x in enumerate(names)}
{'A': {'B': 0, 'C': 0}, 'B': {'A': 0, 'C': 0}, 'C': {'A': 0, 'B': 0}}

答案 2 :(得分:0)

这是一种解决方法

names = ['A','B','C'] 
payable = dict()
for key in names:
    tmp = dict()
    for sub in names:
        if sub == key:
          tmp[sub] = 0
    payable[key] = tmp
print(payable)
# {'A': {'A': 0}, 'B': {'B': 0}, 'C': {'C': 0}}