Conversion between data types (array, str, list) in python

import numpy as np
from numpy import random

random.seed(5)
array = random.standard_normal(10)
print(array)

# array to list
list_ = list(array)  # 由float数字类型组成的list
print(list_) 

# list to string
str_ = ' '.join(str(x) for x in list_)
print(str_)

# string to array
arr =  str_.split(' ')
iter_obj = map(float,arr)
arr_ = np.array(list(iter_obj))
print(arr)

The final output results are as follows:

It should be noted:

  • ' 'The parameters in .join() can only be string type data, since list_ is a list composed of float64, so it must be converted to str type first;
  • arr = str_.split(' ') gets a list composed of str, which is not the same as the data type in list_, list_ is composed of numbers of type float;
  • str_ is just a string with a Size of 1.

Guess you like

Origin blog.csdn.net/Huang_Fj/article/details/96322280