#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep 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. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    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 and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep 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.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    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.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    44
    Rep Power
    2
    Thank you.. that still helps me.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    19
    Rep 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.

IMN logo majestic logo threadwatch logo seochat tools logo