这个字典对应编号的节点:
{0: True, 1: True, 2: True, 3: False, 4: False, 5: False, 6: True, 7: True, 8: False, 9: False}
使用两个打印语句,我想打印标记和未标记的节点如下:
标记节点:
0 1 2 6 7
未标记节点:
3 4 5 8 9
我想要接近于:
print("Marked nodes: %d" key in markedDict if markedDict[key] = True)
print("Unmarked nodes: %d" key in markedDict if markedDict[key] = False)
最佳答案
您可以使用列表理解:
nodes = {0: True, 1: True, 2: True,
3: False, 4: False, 5: False,
6: True, 7: True, 8: False, 9: False}
print("Marked nodes: ", *[i for i, value in nodes.items() if value])
print("Unmarked nodes: ", *[i for i, value in nodes.items() if not value])
输出:
Marked nodes: 0 1 2 6 7
Unmarked nodes: 3 4 5 8 9
关于python - 如何使用内联 if 语句打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35234846/