python学习(二)

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)

序号参数描述
1start起始值
2stop结束值
3num生成等间隔样例的数量,默认为50
4endpoint序列中是否包含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 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值