python as用法_Python中asarray用法

通过在编译环境中输入

import numpy

help(numpy.asarray)

Help on function asarray in module numpy.core.numeric:

asarray(a, dtype=None, order=None)

Convert the input to an array.

Parameters

----------

a : array_like

Input data, in any form that can be converted to an array.  This

includes lists, lists of tuples, tuples, tuples of tuples, tuples

of lists and ndarrays.

dtype : data-type, optional

By default, the data-type is inferred from the input data.

order : {'C', 'F'}, optional

Whether to use row-major ('C') or column-major ('F' for FORTRAN)

memory representation.  Defaults to 'C'.

举例:

Convert a list into an array:将列表转换为数组

>>> a = [1,2]

>>> numpy.asarray(a)

array([1, 2])

将数据类型转换为float和int

>>> a= [1,2]

>>> numpy.asarray(a,'f')

array([ 1., 2.], dtype=float32)

>>> numpy.asarray(a,'i')

array([1, 2])

判断a中数是否大于0,如果大于0,则将该数置为1,

>>> a = [[1,2],[1,0]]

>>> a = numpy.asarray(a) #必须先转换为array,否则出现array(1.0, dtype=float32)

>>> numpy.asarray(a>0,'i') #'i'表示为dtype类型为int

array([[1, 1],

[1, 0]])

>>> numpy.asarray(a>0,'f')

array([[ 1., 1.],

[ 1., 0.]], dtype=float32)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值