list = [1,2,3,4]
arr = np.array(list)
print(list)
print(arr)
[1, 2, 3, 4]
[1 2 3 4]
1、list转换为str
当list中存放的数据是字符串时,一般是通过str中的join函数进行转换:
list = ['a','b','c','d']
str1 = ''.join(list)
str2 = ' '.join(list)
str3 = '.'.join(list)
print(str1)
print(str2)
print(str3)
a b c d
a.b.c.d
但是当list中存放的数据是整型数据或者数字的话,需要先将数据转换为字符串再进行转换:
list = [1, 2, 3, 4]
str1 = ''.join([str(x) for x in list])
str2 = ' '.join([str(x) for x in list])
str3 = '.'.join([str(x) for x in list])
print(str1)
print(str2)
print(str3)
1 2 3 4
1.2.3.4
str1 = 'abcde'
str2 = 'a b c d e'
str3 = 'a, b, c, d, e'
result1 = list(str1)
result2 = str2.split()
result3 = str3.split(', ')
print(result1)
print(result2)
print(result3)