Concatenate several np arrays in python

后端 未结 2 873
孤街浪徒
孤街浪徒 2021-01-12 20:59

I have several bumpy arrays and I want to concatenate them. I am using np.concatenate((array1,array2),axis=1). My problem now is that I want to make the number

相关标签:
2条回答
  • 2021-01-12 21:38

    Do you need to use numpy? Even if you do, you can convert numpy array to python list, run the following and covert back to numpy.array.

    Adding to lists in python will concatenate them...

    x1=[1,0,1]
    x2=[0,0,1]
    x3=[1,1,1]
    
    def conc_func(*args):
        xt=args[0]
        print(args)
        for a in args[1:]:
            xt+=a
        print (xt)
        return xt
    
    xt=conc_func(x1,x2,x3)
    
    0 讨论(0)
  • 2021-01-12 21:44

    concatenate can accept a sequence of array-likes, such as args:

    In [11]: args = (x1, x2, x3)
    
    In [12]: xt = np.concatenate(args)
    
    In [13]: xt
    Out[13]: array([1, 0, 1, 0, 0, 1, 1, 1, 1])
    

    By the way, although axis=1 works, the inputs are all 1-dimensional arrays (so they only have a 0-axis). So it makes more sense to use axis=0 or omit axis entirely since the default is axis=0.

    0 讨论(0)
提交回复
热议问题