编程语言
首页 > 编程语言> > Python中创建二维数组

Python中创建二维数组

作者:互联网

 

在Python中创建二维数组应该这样写:

>>> C = [[0]*3 for i in range(4)]
>>> C
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> C[0][1] = 2
>>> C
[[0, 2, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

所以下次在Python中创建二维数组时候记住了:

aList = [[0] * cols for i in range(rows)]

原文链接:https://blog.csdn.net/u012505432/article/details/52218392

 

其他: numpy

import numpy as np
a= np.zeros((50,10))
b=np.arange(0,50,1)
X = np.empty(shape=[2, 2])
print(X)

a[1,1]= 1
a[0,0]= 100
a[49,9]= 100
print(a)
print(b)

结果:

[[4.05e-322 0.00e+000]
 [0.00e+000 0.00e+000]]
[[100.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   1.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0. 100.]]

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49]

 numpy 和 数组区别:

contentArr =  np.empty((50,10),dtype = np.str_)

numpy矩阵赋值结果:只有文本第一个字母/文本显示。

[['' '' '' '' '' '' '' '' '' '']
 ['' '创' '' '股' '2' '5' '1' '2' '3' '']
 ['' '创' '' '股' '2' '5' '1' '2' '3' '']
 ['' '中' '' '股' '1' '3' '1' '2' '2' '']
 ['' '中' '' '股' '1' '3' '1' '2' '2' '']
 ['' '工' '' '股' '0' '1' '8' '2' '3' '']
 ['' '工' '' '股' '0' '1' '9' '2' '4' '']
 ['' '中' '' '股' '1' '3' '1' '2' '2' '']
 ['' '中' '' '股' '1' '3' '1' '2' '2' '']
 ['' '嘉' '' '股' '1' '6' '1' '2' '1' '']
 ['' '银' '' '股' '1' '4' '1' '2' '2' '']
 ['' '安' '' '混' '0' '2' '8' '2' '3' '']
 ['' '安' '' '混' '0' '2' '8' '2' '3' '']
 ['' '融' '' '混' '1' '3' '1' '2' '3' '']
 ['' '华' '' '混' '2' '5' '1' '2' '2' '']
 ['' '上' '' 'Q' '1' '0' '1' '2' '4' '']
 ['' '前' '' '混' '0' '2' '9' '2' '3' '']
 ['' '博' '' '混' '0' '2' '1' '2' '3' '']
 ['' '前' '' '混' '0' '2' '9' '2' '3' '']
 ['' '广' '' '股' '1' '5' '1' '2' '3' '']
 ['' '华' '' '混' '2' '5' '1' '2' '2' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']
 ['' '' '' '' '' '' '' '' '' '']]

 

数组赋值:

 

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, '创金合信医疗保健股票A', 0, '股票型', '2.25%', '5.23%', '15.04%', '27.84%', '39.53%', 0], [0, '创金合信医疗保健股票C', 0, '股票型', '2.25%', '5.21%', '14.96%', '27.60%', '39.00%', 0], [0, '中欧品质消费股票A', 0, '股票型', '1.44%', '3.54%', '11.37%', '26.81%', '27.09%', 0], [0, '中欧品质消费股票C', 0, '股票型', '1.44%', '3.53%', '11.30%', '26.54%', '26.65%', 0], [0, '工银前沿医疗股票', 0, '股票型', '0.27%', '1.15%', '8.54%', '26.40%', '39.92%', 0], [0, '工银养老产业股票', 0, '股票型', '0.15%', '1.31%', '9.47%', '25.88%', '40.81%', 0], [0, '中欧消费主题股票A', 0, '股票型', '1.44%', '3.43%', '10.74%', '25.85%', '28.43%', 0], [0, '中欧消费主题股票C', 0, '股票型', '1.44%', '3.43%', '10.67%', '25.61%', '28.03%', 0], [0, '嘉实农业产业股票', 0, '股票型', '1.03%', '6.49%', '14.55%', '25.52%', '16.76%', 0], [0, '银华农业产业股票发起式', 0, '股票型', '1.25%', '4.45%', '10.53%', '25.37%', '25.69%', 0], [0, '安信新回报混合A', 0, '混合型', '0.93%', '2.88%', '8.47%', '24.34%', '35.39%', 0], [0, '安信新回报混合C', 0, '混合型', '0.92%', '2.88%', '8.46%', '24.28%', '35.26%', 0], [0, '融通医疗保健行业混合A/B', 0, '混合型', '1.17%', '3.93%', '13.86%', '24.17%', '31.00%', 0], [0, '华泰柏瑞健康生活混合', 0, '混合型', '2.65%', '5.00%', '13.24%', '24.12%', '26.40%', 0], [0, '上投摩根中国生物医药(QDII)', 0, 'QDII', '1.61%', '0.90%', '11.74%', '24.10%', '40.87%', 0], [0, '前海联合国民健康混合A', 0, '混合型', '0.82%', '2.79%', '9.35%', '24.07%', '31.61%', 0], [0, '博时医疗保健行业混合A', 0, '混合型', '0.61%', '2.81%', '13.11%', '23.90%', '30.05%', 0], [0, '前海联合国民健康混合C', 0, '混合型', '0.76%', '2.74%', '9.27%', '23.90%', '30.42%', 0], [0, '广发医疗保健股票A', 0, '股票型', '1.77%', '5.25%', '14.61%', '23.85%', '34.89%', 0], [0, '华泰柏瑞行业领先混合', 0, '混合型', '2.67%', '5.22%', '13.65%', '23.67%', '26.07%', 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

 

标签:Python,股票,医疗保健,混合型,二维,数组,np,混合,numpy
来源: https://www.cnblogs.com/watermarks/p/12803631.html