0

I have 3 lists

> a= ones((10,1))
> 
> 
>  x_1=[1,2,3,4...,10] (i.e. list of 10 elements)
> 
>  y_1=[2,1,4,7,2,..20] (list of 10 elements)

I want to join these 3 list and make it as 2d list or matrix as:

> mat=[a x_1 y_1]

like in MATLAB where a will be 1 column x_1 second column and y_1 third column.

> mat= [[1,1,2],[1,2,1],[1,3,4],[]......[]]

I tried

> np.matrix([[a,[[i] for i in x_1],[[i] for i in y_1]]])

but it gave an error as matrix must be 2 dimensionl

How can I do this ?

Also, if I have 2D arrays i.e.

>  a=np.ones((3,2))
> 
> x_1=[[1,2],[2,3],[2,4]]
> 
> y_1=[[3,4],[5,6],[3,4]]

Then how can I concatenate these arrays and make it as

> c=[[1,1,2,3,4],[1,2,3,5,6],[1,2,4,3,4]]

In matlab it is written as:

> c=[a,x_1,y_1]

How can I do this as well?

2
  • Could you please clarify if a is a 2D python array. Commented Nov 4, 2017 at 16:27
  • Yes a is 2d python array. Sorry I missed np before ones. I have updated it now. So a is [[1,1],[1,1],[1,1]] Commented Nov 4, 2017 at 19:25

2 Answers 2

2

If you want to stay within "standard" Python (no numpy), then to get a transposed 2D list you could do the following:

mat = [p for p in zip(a, x_1, y_1)]

If you need a numpy array anyway, then:

import numpy as np
mat = np.array([a, x_1, y_1]).T

NOTE: in the above example replace np.array with np.matrix if that is what you want.

If your a array is a 2D array such as, e.g., np.ones((10,1)) or np.ones((10,3)) then you can use one of the *stack() functions in numpy:

mat = np.vstack([np.asarray(a).T, x_1, y_1]).T
Sign up to request clarification or add additional context in comments.

8 Comments

Your solution is great, but it is not working with 3 lists. i.e. I have a=ones((10,1)) too and when I am doing c=[a,x_1,y_1] it gives an error as "could not broadcast input array from shape (10,1) into shape (10)"
Actually it works, sorry I was using it wrong. I used a=[1]*10 instead of np.ones((10,1)), then it worked. Thanx a lot.
@Eyshika If a = np.ones((10, 1)) then you can either change the way it is defined, i.e., a = np.ones(10) to make it 1D or in my solution replace a with [x[0] for x in a].
@Eyshika a=[1]*10 if OK here but you have to be careful about it in general.
Ok I got it, but if I want to have 10,3 ones values so np.ones(10) will not work and np.ones(10,3) gives error then how will I use it there.
|
1

2D lists in python are different than in MATLAB!

If I have two lists

A = ["red", "green", "blue"]
B = ["orange", "yellow", "purple"]

And I wanted to create a 2D list out of these two, I could just write:

C = [A, B]

Then, C would be equal to [["red", "green", "blue"], ["orange", "yellow", "purple"]]

I could access elements of C such as:

C[0][1]
>> "red"
C[1][2]
>> "purple"
C[0]
>> ["red", "green", "blue"]

To answer your specific question, you have lists x_1 and y_1, and you want to create mat, a 2D list containing x_1 and y_1. You can just write,

mat = [x_1 y_1]

This list will be 2x10. You want to transpose this list, so you should be able to use zip to do:

mat = map(list, zip(*mat))

5 Comments

Thank you for answering this question. i tried your method. It doesn't work. When I am doing [x_1, y_1] it makes a list of length size =2 i.e. it is adding list as a another row or another list inside list, whereas I want to add in as in column wise.. with length of new list as 10.
Ah I see, so what you want is the same result but rotated? So this method gives you 2 by 10, but you want 10 by 2?
just updated the answer to include a list transpose at the end!
Works perfectly now.. Thanx!
I have one concern what if list is list of list then will this method work ? i.e. if a=[[1,2,3],[1,2,3]] and v= [[2,3,4],[2,3,4]] and I want it to convert as c=[[1,2,3,2,3,4],[1,2,3,2,3,4]] then will this work ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.