0

I want to create a variable D by combining two other variable x and y.

x has the shape [731] and y has the shape [146].

At the end D should be 2D so that D[0] contains all x-values and D[1] all y-values. I hope I explained it in a way someone can understand what I want to do. Can someone help me with this?

Leo
  • 113
  • 1
  • 4
  • 13

4 Answers4

2

It is a simple as: D = [x, y]

Hope it helped :)

Joonatan Samuel
  • 641
  • 4
  • 17
1

Nested lists will do that*:

D = [x, y]

print(D[0] == x)  # True
print(D[1] == y)  # True
print(D[1] == x)  # False

Note that the result cannot be interpreted as a 2D array, if that is what you have in mind. A 2D array would require each row (and column) to have the same number of elements. Accessing D[0][700] will work, while D[1][700] will fail.

* The terminology 'nested lists' assumes that x and y are lists. Enclosing them in another list [ ] makes them nested. However, if x and y are not lists but other types the principle is the same.

MB-F
  • 22,770
  • 4
  • 61
  • 116
0

I believe what you are trying to do is make a 2D-Array. Such that for each place (such as array[0]) in the array there is another array?

myArray=[[1,2],[3,4]]

Or maybe just a regular array..

Community
  • 1
  • 1
Jamin
  • 1,362
  • 8
  • 22
0

Is not possible to make arrays with different sizes as I understood you want to, and this is because a 2D-array is basically a table with rows and columns, and each row has the same number of columns, no matter what.

But, you can join the values in each variable and save the resulting strings in the array, and to use them again just split it back and parse the values to the type you need them.

Raven H.
  • 72
  • 10