Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 21st, 2012, 04:29 AM
 emo.vs.elmo
Contributing User

Join Date: Oct 2012
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 2
Corresponding order of lists

Hi i was just wondering.. i dont know why i cant seem to remember how to do this but:

How do you get the index of one list to correspond to another?

For example:

List_a = [0, 1, 2, 3, 4] <this list holds indexes

List_b = [[2,1,3,1], [4, 3, 1, 1]]

the end of this should be:

[6, 4, 4, 2]

So we are basically using List_a as our index for List_b.. How do we do that? so confused,

Any sort of help is appreciated

#2
November 21st, 2012, 10:52 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,217
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 35 m 4 sec
Reputation Power: 455
Code:
```a = [0, 1, 2, 3, 4] #this list holds indexes

b = [[2,1,3,1],
[4,3,1,1]]
#  +  -------
#    [6,4,4,2]

[6,  # because b[0][a[0]] + b[1][a[0]] is 6
4,  # because b[0][a[1]] + b[1][a[1]] is 4
4,  ...
2]  # because b[0][a[3]] + b[1][a[3]] is 2
# no more because max(len(a),max(len(B) for B in b)) == 4```

is this what you mean?
__________________
[code]Code tags[/code] are essential for python code!

#3
November 21st, 2012, 03:12 PM
 emo.vs.elmo
Contributing User

Join Date: Oct 2012
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 2
Yes thats what i mean..

SO you corespond the order of one list with another by giving the index to the list everytime??? for example..

a[0]b[0]..

but these are two sperate lists in two separate variables.

#4
November 21st, 2012, 09:25 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,217
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 35 m 4 sec
Reputation Power: 455
Code:
```a = [0, 1, 2, 3, 4]

b = [[2,1,3,1], [4, 3, 1, 1]]

c = []

for A in a:
try:
c.append(sum(B[A]for B in b))
except:
break

print(c)```
This solves the problem you requested. I can't tell you any more about it.

#5
November 21st, 2012, 10:21 PM
 emo.vs.elmo
Contributing User

Join Date: Oct 2012
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 2
Thank you.. that still helps me.

#6
November 22nd, 2012, 01:09 PM
 russ123
Registered User

Join Date: Nov 2012
Posts: 19
Time spent in forums: 8 h 21 m 37 sec
Reputation Power: 0
I think a different way to look at it which might be easier to understand is this.

Code:
```columns = [0, 1, 2, 3]

grid = [[2, 1, 3, 1],
[4, 3, 1, 1]]

# Now take note:
#     col col col col
# row  2   1   3   4
# row  4   3   1   1
#
# You want to go through each column and sum the column up.

result = [0, 0, 0, 0]
for col in columns:
for row in grid:
result[col] += row[col]```

It works for a grid of any number of rows, but if you want it to work for any number of columns too then you have to initiate your results list with something like:
Code:
`result = [0 for col in range(len(grid[0]))]`

If you want to specify the exact number of columns to add, like say 2, and also do away with the columns list (which is your list_a):
Code:
```result = [0 for col in range(len(2))]
for col in range(len(result)):```

But the other users code will work much better.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Corresponding order of lists