The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Corresponding order of lists
Discuss Corresponding order of lists in the Python Programming forum on Dev Shed. Corresponding order of lists Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 21st, 2012, 03:29 AM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
|
|
|
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
|

November 21st, 2012, 09:52 AM
|
 |
Contributing User
|
|
|
|
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!
|

November 21st, 2012, 02:12 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
|
|
|
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.
|

November 21st, 2012, 08:25 PM
|
 |
Contributing User
|
|
|
|
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.
|

November 21st, 2012, 09:21 PM
|
|
Contributing User
|
|
Join Date: Oct 2012
Posts: 44
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
|
|
|
Thank you.. that still helps me.
|

November 22nd, 2012, 12:09 PM
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|