python - How to print with inline if statement? -
this dictionary corresponds numbered nodes:
{0: true, 1: true, 2: true, 3: false, 4: false, 5: false, 6: true, 7: true, 8: false, 9: false}
using 2 print statements, want print marked , unmarked nodes follows:
marked nodes:
0 1 2 6 7
unmarked nodes:
3 4 5 8 9
i want close to:
print("marked nodes: %d" key in markeddict if markeddict[key] = true) print("unmarked nodes: %d" key in markeddict if markeddict[key] = false)
you can use list comprehensions:
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 i, value in nodes.items() if value]) print("unmarked nodes: ", *[i i, value in nodes.items() if not value])
output:
marked nodes: 0 1 2 6 7 unmarked nodes: 3 4 5 8 9
Comments
Post a Comment