1.二维数组
(a)初始化数组
>>> static_list = [[0] * 10 for _ in range(3)]
>>> print(static_list)
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> list_three = [[0 for i in range(10)] for j in range(3)]
>>> print(list_three)
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
2.tuple,list,str相互转换
>>> s = 'abcdefg1234'
>>> print(type(s))
<class 'str'>
>>>
>>> l = list(s)
>>> print(type(l))
<class 'list'>
>>> s1 = ''.join(l)
>>> print('s1:',s1, type(s1))
s1: abcdefg1234 <class 'str'>
>>> t = tuple(s1)
>>> print(t)
('a', 'b', 'c', 'd', 'e', 'f', 'g', '1', '2', '3', '4')
>>> l=list(t)
>>> l=list(s1)
>>> print(l)
['a', 'b', 'c', 'd', 'e', 'f', 'g', '1', '2', '3', '4']
>>> s2 = ''.join(t)
>>> print(s2)
abcdefg1234
https://www.cnblogs.com/zrmw/p/10637114.html
3.读取txt文本内容
for line in open(txt,"r"): #设置文件对象并读取每一行文件
size = img.shape
imgh = size[0]
imgw = size[1]
line = line.strip()
listFromLine = line.split('\t')
#data.append(line)
print("%s" % listFromLine)
data = line.split(' ')
label_idx = int(float(data[0]))
label[label_idx].append(data)
4.保存文本内容
if not os.path.exists(new_dir):
os.mkdir(new_dir)
f1 = open(new_txt_path, 'w')
for txt in save_list:
line = list(txt)
str_line = " ".join(str(i) for i in line) + '\n'
f1.writelines(str_line)
f1.close()
5.plt绘图
参考官网https://matplotlib.org/tutorials/index.html
常用函数:
enumerate,arange,linespace, random
>>>seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
print i, element
arange按指定宽度生成序列:
>>> arr = np.arange(0,10,1) #默认不含end
>>> print(arr)
[0 1 2 3 4 5 6 7 8 9]
linspace()函数
通过指定开始值、终值和元素个数来创建一个一维数组,数组的数据元素符合等差数列,可以通过endpoint关键字指定是否包含终值,默认包含终值。
等差数列numpy.linspace(start,stop,num,endpoint,retstep,dtype)
序号 | 参数 | 描述 |
---|---|---|
1 | start | 起始值 |
2 | stop | 结束值 |
3 | num | 生成等间隔样例的数量,默认为50 |
4 | endpoint | 序列中是否包含stop 值 默认为 True |
>>> import numpy as np
>>> arr = np.linspace(10,20,9)
>>> print(arr)
[10. 11.25 12.5 13.75 15. 16.25 17.5 18.75 20. ]
>>> arr = np.linspace(10,20,9, endpoint=False)
>>> print(arr)
[10. 11.11111111 12.22222222 13.33333333 14.44444444 15.55555556
16.66666667 17.77777778 18.88888889]
>>> arr = np.linspace(10,20,5,retstep=True)
>>> print(arr)
(array([10. , 12.5, 15. , 17.5, 20. ]), 2.5)
>>>
random
>>> arr = np.random.rand(9).reshape(3,3)
>>> print(arr)
[[0.65364063 0.75736318 0.2242586 ]
[0.82258882 0.14179703 0.06452715]
[0.33913215 0.93847282 0.65274731]]
>>> arr = np.random.rand(3,2,3)
>>> print(arr)
[[[0.65370391 0.84374592 0.82312136]
[0.82027961 0.0158432 0.17479008]]
[[0.919728 0.81376633 0.0423475 ]
[0.75165144 0.29549194 0.54831757]]
[[0.83097579 0.76248915 0.16985863]
[0.47245166 0.3010762 0.04345083]]]
>>>
https://blog.csdn.net/sxau_zhangtao/article/details/98035060