Conversion of lists and arrays


In the learning of python, the conversion between arrays and lists is often used. This article mainly introduces the problem of conversion between arrays and lists.

Convert a list to an array with np.array(a)

The program shows:

import numpy as np
import random

a=[[[random.randint(1,9) for x in range(5)]for i in range(3)]for j in range(2)]
print(type(a))
print(a)

b=np.array(a)
print(type(b))
print(b)

The output result is:

<class 'list'>

[[[2, 2, 1, 3, 5], [9, 6, 3, 7, 2], [4, 9, 7, 2, 5]], [[6, 4, 9, 4, 9], [1, 4, 7, 2, 5], [2, 6, 3, 6, 7]]]

<class 'numpy.ndarray'>

[[[2 2 1 3 5]
  [9 6 3 7 2]
  [4 9 7 2 5]]

 [[6 4 9 4 9]
  [1 4 7 2 5]
  [2 6 3 6 7]]]

From the above results, it can be seen that the conversion from the list to the array was successfully completed.

Special cases of converting lists to arrays (1)

import numpy as np
import random

f=[]
e=[random.randint(1,9) for x in range(5)]
f.append(np.array(e))
g=[random.randint(1,9) for x in range(5)]
f.append(g)
print(f)
f1=np.array(f, dtype=object)
print("转为数组后:")
print(f1)

The result is displayed as:

[array([7, 3, 7, 4, 2]), [7, 9, 8, 1, 5]]

转为数组后:

[[7 3 7 4 2]
 [7 9 8 1 5]]

It can be seen that f in the original program contains both array type and list type. Since the length of the array and the list are the same, all the data of f can be successfully converted into an array type to obtain f1.

Speaking of this, then you will think, if a list contains multiple sub-lists, and the amount of data in the sub-lists is not exactly the same, what will happen to the result?

The special case of converting list to array (2)

import numpy as np
import random

f=[]
e=[random.randint(1,9) for x in range(5)]
f.append(np.array(e))
# 此处做了修改
g=[random.randint(1,9) for x in range(4)]
f.append(g)
print(f)
f1=np.array(f, dtype=object)
print("转为数组后:")
print(f1)
print(len(f))

The result after this run is:

[array([7, 5, 3, 9, 4]), [8, 7, 9, 1]]

转为数组后:

[array([7, 5, 3, 9, 4]) list([8, 7, 9, 1])]

2

We can find that although there is no problem with the running result, the new f1 obtained is not what we want. We hope that f1 contains 9 elements, but we can see that there are only 2 elements through len(f1).

It can be seen from this that in order to convert the situation in the above program into an array through the np.array function, it is necessary to ensure that each sub-element in each f contains the same number of sub-elements.

The solution to the inconsistent number of child elements

import numpy as np
import random

f=[]
e=[random.randint(1,9) for x in range(5)]
f.append(np.array(e))
# 此处做了修改
g=[random.randint(1,9) for x in range(4)]
f.append(g)
print(f)
f1=np.array(f, dtype=object)
print("转为数组后:")
print(f1)
print(len(f))

print("重新转化")
f1=f1[0].tolist()+f1[1]
print(f1)
print(len(f1))

The result after running shows:


[array([5, 8, 6, 4, 5]), [1, 4, 3, 6]]
转为数组后:
[array([5, 8, 6, 4, 5]) list([1, 4, 3, 6])]
2
重新转化
[5, 8, 6, 4, 5, 1, 4, 3, 6]
9

It can be seen that the desired result was successfully achieved this time.

Since the number of elements in the sublists is different when converting multiple lists to arrays, errors may occur, so here is a summary.

Use the a.tolist() function to convert an array into a list

From the above example, we can see that the blogger converted the first sub-element (which is an array) in f1 into a list.

Here is another program to illustrate:

import numpy as np
import random

a=[random.randint(1,9) for x in range(5)]
a_array=np.array(a)
print(a_array)
print(type(a_array))
a_list=(a_array).tolist()
print(a_list)
print(type(a_list))

The result is displayed as:

[6 1 9 3 1]
<class 'numpy.ndarray'>
[6, 1, 9, 3, 1]
<class 'list'>

Guess you like

Origin blog.csdn.net/ximu__l/article/details/129351029