0

I am trying to two shorthand If Else statements into a more conventional format, in order to understand it.

For example, from:

if-expression if (condition) else else-expression

to:

if condiction:
    if-expression
else:
    else-expression

Here are the actual statements I'm trying to breakdown:

Statement 1:

nc = [node_colors[node] if node in node_colors else 'none' for node in projected_routes.nodes()]

Statement 2:

ns = [20 if node in node_colors else 0 for node in projected_routes.nodes()]

Here are my attempts but they do not return the same results as above. I'm not sure what is the role of [] here.

Statement 1:

if node in node_colors:
    nc = node_colors[node]
else:
    for node in projected_routes.nodes():
        'none'

Statement 2:

if node in node_colors:
    ns = 20
else:
    for node in projected_routes.nodes():
        0

Where am I doing wrong? Please someone help me out with this.

1 Answers1

0

Perhaps this will work.

Statement 1:

nc=[]

for node in projected_routes.node():
    if node in node_colors:
        nc.append(node_colors[node])
    else:
        nc.append('none')

Statement 2:

ns=[]
for node in projected_routes.node():
    if node in node_colors:
        ns.append(20)
    else:
        ns.append(0)

On your tries, you have some mistakes:

  • order/logic mistakes (where/which loop/conditional to use)

  • assignment instead of append

    if node in node_colors:
                nc = node_colors[node]

Above, the 'value' of nc will be replaced for each iteration.

for node in projected_routes.nodes():
        0

Above sample, 0 is 'left in the air'; not appended, not assigned.

About the logic (learning):

[ expression if conditional else other thing for this many times ] 

Translates to:

for this many times:
    if conditional: 
        do this thing
    else:
        do something else
andreis11
  • 1,133
  • 1
  • 6
  • 10