Is there a way to merge multiple list index by index?

前端 未结 4 1876
粉色の甜心
粉色の甜心 2021-01-03 07:18

For example, I have three lists (of the same length)

A = [1,2,3]
B = [a,b,c]
C = [x,y,z]

and i want to merge it into something like: [[1,a,

相关标签:
4条回答
  • 2021-01-03 07:52

    To get a list of lists, you can use the built-in function zip() and list comprehension to convert each element of the result of zip() from a tupleto a list:

    A = [1, 2, 3]
    B = [4, 5, 6]
    C = [7, 8, 9]
    
    X = [list(e) for e in zip(A, B, C,)]
    
    print X
    >>> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
    
    0 讨论(0)
  • 2021-01-03 08:02

    I was just wondering the same thing. I'm a total noob using code academy. This is what i came up to combine two lists index at index

    toppings = ['pepperoni', 'pineapple', 'cheese', 'sausage', 'olives', 'anchovies',    'mushrooms']
    
    prices = [2,6,1,3,2,7,2]
    
    num_pizzas = len(toppings)
    
    print("We sell "+str(num_pizzas)+" different kinds of pizza!")
    
    ***pizzas = list(zip(toppings, prices))***
    
    print (pizzas)
    

    the list pizzas printed out ...[('pepperoni', 2), ('pineapple', 6), ('cheese', 1), ('sausage', 3), ('olives', 2), ('anchovies', 7), ('mushrooms', 2)]

    0 讨论(0)
  • 2021-01-03 08:06

    It looks like your code is meant to say answer = [], and leaving that out will cause problems. But the major problem you have is this:

    answer = answer.extend(temp)
    

    extend modifies answer and returns None. Leave this as just answer.extend(temp) and it will work. You likely also want to use the append method rather than extend - append puts one object (the list temp) at the end of answer, while extend appends each item of temp individually, ultimately giving the flattened version of what you're after: [1, 'a', 'x', 2, 'b', 'y', 3, 'c', 'z'].

    But, rather than reinventing the wheel, this is exactly what the builtin zip is for:

    >>> A = [1,2,3]
    >>> B = ['a', 'b', 'c']
    >>> C = ['x', 'y', 'z']
    >>> list(zip(A, B, C))
    [(1, 'a', 'x'), (2, 'b', 'y'), (3, 'c', 'z')]
    

    Note that in Python 2, zip returns a list of tuples; in Python 3, it returns a lazy iterator (ie, it builds the tuples as they're requested, rather than precomputing them). If you want the Python 2 behaviour in Python 3, you pass it through list as I've done above. If you want the Python 3 behaviour in Python 2, use the function izip from itertools.

    0 讨论(0)
  • 2021-01-03 08:18

    Assuming you are doing this for class and not learning all of the tricks that make Python a great tool here is what you need. You had two problems, first if you want to extend then you do it in place but your desired result shows that you want to append, not extend

    def merger(A,B,C):
        answer = []
        for y in range (len(A)):
            a=A[y]
            b=B[y]
            c=C[y]
            temp = [a,b,c]
            answer.append(temp)
       return answer
    
    
    >>> answer
    [[1, 'a', 'x'], [2, 'b', 'y'], [3, 'c', 'z']]
    
    0 讨论(0)
提交回复
热议问题