python怎么创建数组


本文摘自php中文网,作者藏色散人,侵删。

一 直接定义法:

1.直接定义

  matrix=[0,1,2,3]

2.间接定义

  matrix=[0 for i in range(4)]  print(matrix)

二 Numpy方法:

Numpy内置了从头开始创建数组的函数:

zeros(shape)将创建一个用指定形状用0填充的数组。默认的dtype是float64。

下面是几种常用的创建方法:

1

2

3

4

5

6

7

#coding=utf-8import numpy as np

a = np.array([1,2,3,4,5])print a

b = np.zeros((2,3))print b

c = np.arange(10)print c

d = np.arange(2,10,dtype=np.float)print d

e = np.linspace(1.0,4.0,6)print e

f = np.indices((3,3))print f

三 其他转换法:

数组还有比较常用的一种方法,就是从其他Python结构(例如,列表,元组)转换。

下面给出一些例子。

列表转数组:

1

2

3

4

5

a = []

a.append((1,2,4))

a.append((2,3,4))

a = np.array(a)

a.flatten()

元组转成数组:

1

2

3

import numpy as np

mylist = [1,2,3]print tuple(mylist)

iarray = np.array(tuple(mylist))print iarray

相关推荐:《Python教程》

以上就是python怎么创建数组的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

实例讲解Python基于回溯法子集树模板解决旅行商问题(tsp)

Python要在linux下编程吗

Python获取代理ip的实例分享

Python实现的计算器功能

Python适合在什么系统

Python中json模块和pickle模块的简单介绍(附示例)

Python调用c# com dll组件的过程详解

什么叫Python字符串的格式化

Python 批量合并多个txt文件的实例讲解

Python里upper什么意思

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...