Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 21st, 2012, 03:29 AM
emo.vs.elmo emo.vs.elmo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 44 emo.vs.elmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old November 21st, 2012, 09:52 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old November 21st, 2012, 02:12 PM
emo.vs.elmo emo.vs.elmo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 44 emo.vs.elmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old November 21st, 2012, 08:25 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
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.

Reply With Quote
  #5  
Old November 21st, 2012, 09:21 PM
emo.vs.elmo emo.vs.elmo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 44 emo.vs.elmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 46 m 55 sec
Reputation Power: 1
Thank you.. that still helps me.

Reply With Quote
  #6  
Old November 22nd, 2012, 12:09 PM
russ123 russ123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 19 russ123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Corresponding order of lists

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap