How do I make Maps and lambdas work with a matrix in python 3.4?

前端 未结 5 1967
有刺的猬
有刺的猬 2020-12-20 07:58

I often avoid me to write posts like this but I\'m new in python 3.4 and I need a help with this code.

Suppose we have the following list:

v = [ [ x,         


        
5条回答
  •  -上瘾入骨i
    2020-12-20 08:45

    If you just have a set amount of elements to join you can use str.format:

    print(list(map(lambda x: "{}{}".format(*x) , v)))  
    
    ['a1', 'a2', 'a3', 'a4', 'b1', 'b2', 'b3', 'b4', 'c1', 'c2', 'c3', 'c4']
    

    You can also do it for any amount of elements using "{}" * len(x):

    print(list(map(lambda x: ("{}" * len(x)).format(*x) , v)))
    

    *x unpacks the sequence, "{}" * len(x) creates a {} for each element in each sublist.

    Or unpack in a list comp:

    print([ "{}{}".format(*x) for x in v])
    

提交回复
热议问题