tcokyasar
tcokyasar

Reputation: 592

For loop using tuples

I have the following list and dictionaries:

A = [1, 2, 4, 5, 6, 7, 9, 10, 11, 13, 14, 15, 16]
N_a = {1: (467979, 178581), 2: (178581, 467979), 4: (467979, 164786),
       5: (164786, 467979), 6: (467979, 164504), 7: (164504, 467979),
       9: (467979, 333092), 10: (333092, 149117), 11: (149117, 467979),
       13: (467979, 177073), 14: (177073, 467979), 15: (467979, 349340),
       16: (349340, 467979)}
E = {(333092, 467979): 15, (333092, 177073): 10, (333092, 164786): 13,
     (333092, 178581): 13, (333092, 164504): 17, (333092, 349340): 19,
     (333092, 149117): 13, (467979, 333092): 15, (467979, 177073): 12,
     (467979, 164786): 14, (467979, 178581): 17, (467979, 164504): 16,
     (467979, 349340): 18, (467979, 149117): 18, (177073, 333092): 11,
     (177073, 467979): 16, (177073, 164786): 17, (177073, 178581): 17,
     (177073, 164504): 18, (177073, 349340): 11, (177073, 149117): 15,
     (164786, 333092): 19, (164786, 467979): 18, (164786, 177073): 19,
     (164786, 178581): 14, (164786, 164504): 13, (164786, 349340): 10,
     (164786, 149117): 13, (178581, 333092): 15, (178581, 467979): 10,
     (178581, 177073): 12, (178581, 164786): 13, (178581, 164504): 18,
     (178581, 349340): 11, (178581, 149117): 13, (164504, 333092): 13,
     (164504, 467979): 13, (164504, 177073): 17, (164504, 164786): 10,
     (164504, 178581): 11, (164504, 349340): 19, (164504, 149117): 19,
     (349340, 333092): 10, (349340, 467979): 14, (349340, 177073): 17,
     (349340, 164786): 13, (349340, 178581): 12, (349340, 164504): 17,
     (349340, 149117): 12, (149117, 333092): 10, (149117, 467979): 10,
     (149117, 177073): 14, (149117, 164786): 15, (149117, 178581): 15,
     (149117, 164504): 16, (149117, 349340): 18}

I am trying to print the value of E(i,j) given A and N_a. Btw, A is a list of N_a's keys. Here is my attempt:

Input:

for a in A:
    for (i,j) in N_a[a]:
        print (E[(i,j)])

Output:

TypeError: 'int' object is not iterable

I also tried only i instead of (i,j) which also did not work. What am I doing wrong?

Upvotes: 0

Views: 39

Answers (1)

TrebledJ
TrebledJ

Reputation: 8987

You can get rid of the for-loop. N_a[a] already returns a (single) tuple for you to work with, so there's nothing to loop over. Simply pass it into E[].

for a in A:
    print(E[N_a[a]])

Upvotes: 1

Related Questions