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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -