直接上代码:
"""
list 是python的内置函数
array 是在numpy包中定义的
在应用中array比list具有更多的属性函数,使用更灵活,但是要求内部元素数据类型相同
list包容性更好,可以同时包含各种类型数据
-->如果是处理数字数据,建议将list转为np.array
"""
import numpy as np
a = [[1,2,3],[4,5,6]]
# a = [[1,2,3],[4,5,'t']]
b = np.array(a)
print('\n#####--data--#######')
print(a) ## 列表不同元素间是“,”,输出 [[1, 2, 3], [4, 5, 6]]
print(b) ## 列表不同元素间是“ ”空格,输出 [[1 2 3]
## [4 5 6]]
print('\n#####--type--#######')
print(type(a)) ##<class 'list'>
print(type(b)) ##<class 'numpy.ndarray'>
print('\n#####--ndim--#######')
# print(a.ndim)
print(b.ndim)
print('\n#####--shape--#######')
# print(a.shape) ## AttributeError: 'list' object has no attribute 'shape'
print(b.shape) ## (2, 3)
print('\n#####--reshape--#######')
# print(a.reshape) ## AttributeError: 'list' object has no attribute 'reshape'
print(b.reshape(1,3,2))
print('\n#####--dtype--#######')
# print(a.dtype) ## AttributeError: 'list' object has no attribute 'dtype'
print(b.dtype) ## int64
print('\n#####--len--#######')
print(len(a)) ## 2
print(len(b)) ## 2
print('\n#####--size--#######')
# print(a.size) ## AttributeError: 'list' object has no attribute 'size'
print(b.size) ## 6
print('\n#####--调用1--#######')
print(a[0]) ## [1, 2, 3]
print(b[0]) ## [1 2 3]
print('\n#####--调用2--#######')
print(a[0][:]) ## [1, 2, 3]
print(b[0][:]) ## [1 2 3]
print('\n#####--调用3--#######')
# print(a[0,:]) #TypeError: list indices must be integers or slices, not tuple
# print(a[0,1]) #TypeError: list indices must be integers or slices, not tuple
print(b[0,:]) ## [1 2 3]
print('\n#####--互相转换--#######')
b = np.array(a)
a_ = b.tolist()
print(a)
print(b)
print(a_)


被折叠的 条评论
为什么被折叠?



