python 中list与array互相转换。

将array转成list比较简单,如下:

#array to list
import numpy as np  #array模块,但其只支持一维数组,不支持多维数组,也没有各种运算函数。
#matrix=[0 for i in range(4)]  #这样定义的数组时list object,可以拿list当数组用
matrix_array=np.random.randint(0,3,(2,3))
#其他定义
#array = np.empty((5, 4), dtype=int)    #可以用于预先申请内存
matrix_list = matrix_array.tolist()

将list转换成数组。

由于list中可以存放不同类型的元素,因此在转换成数组时,为了保证转换不出错,要检查类型是否一致,有数字且有字符的list转成array时会变成字符数组。

import numpy as np
# define list
array = np.asarray(list)
#the second method
array = np.array(list, dtype = int)

list对象的常用方法有:

list=[1,2,3,4,5]   #list的定义以[]方式,tuple的定义以()方式
list.insert(1, 'content')    #在指定位置插入元素
list.pop()    #将最后一位的元素删除
list.pop(i)    #删除指定位置的元素,i从0开始
list[-1]    #下标访问

下面介绍一个快速的将list转换成array的函数,代码来自 TSN_ECCV2016,如下:

def fast_list2arr(data, offset=None, dtype=None):
    """
    Convert a list of numpy arrays with the same size to a large numpy array.
    This is way more efficient than directly using numpy.array()
    See
        https://github.com/obspy/obspy/wiki/Known-Python-Issues
    :param data: [numpy.array]
    :param offset: array to be subtracted from the each array.
    :param dtype: data type
    :return: numpy.array
    """
    num = len(data)
    out_data = np.empty((num,)+data[0].shape, dtype=dtype if dtype else data[0].dtype)
    for i in xrange(num):
        out_data[i] = data[i] - offset if offset else data[i]
    return out_data

 该方法中每个元素都是array,但是整个对象是array list,要转换成array of array 才能送入caffe网络进行预测。原理是先申请空间再逐一复制array list中的每个元素。

使用的时候会比较方便,如下图片的处理:

frame = fast_list2arr([cv2.resize(x, frame_size) for x in frame])

 

### Python 中 `numpy.array` 的使用方法 #### 创建数组 可以利用多种方式来创建 NumPy 数组。最常见的方式是从列表或其他序列类型的对象转换而来。 ```python import numpy as np # 从列表创建一维数组 data_list = [1, 2, 3, 4] array_from_list = np.array(data_list) print(array_from_list) # 输出: [1 2 3 4] # 从嵌套列表创建多维数组 nested_lists = [[1, 2], [3, 4]] multi_dim_array = np.array(nested_lists) print(multi_dim_array) # 输出: # [[1 2] # [3 4]] ``` #### 数据类型指定 当调用 `np.array()` 函数时,可以通过设置 dtype 参数来自定义数据类型[^3]。 ```python custom_dtype_array = np.array([1, 2, 3, 4], dtype=float) print(custom_dtype_array) # 输出: [1. 2. 3. 4.] ``` #### 基本操作 NumPy 提供了一系列用于处理数组的方法和支持向量化运算的功能。 ##### 形状变换 改变现有数组形状而不修改其内容: ```python original_shape = np.arange(8).reshape((2, 4)) reshaped = original_shape.reshape(-1, 2) print(f"Original shape:\n{original_shape}") print(f"\nReshaped to (-1, 2):\n{reshaped}") ``` ##### 数学计算 执行逐元素加减乘除等算术运算以及广播机制下的矩阵运算。 ```python vector_a = np.array([1, 2]) vector_b = np.array([3, 4]) addition_result = vector_a + vector_b multiplication_result = vector_a * vector_b print(addition_result) # 输出: [4 6] print(multiplication_result) # 输出: [3 8] ``` #### 类型转换 可以在 NumPy 数组 Python 列表间互相转换。 ```python py_list = [9, 8, 7, 6] converted_to_np_array = np.array(py_list) back_to_py_list = converted_to_np_array.tolist() print(converted_to_np_array) # 输出: [9 8 7 6] print(back_to_py_list) # 输出: [9, 8, 7, 6] ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值