菜鸟学习python编程--杂记 (不定期更新)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/e01528/article/details/82924389

 

目录

1.bool类型的形参+return 的合理使用

   其实当i有明确含义时,如下for col in xrange(w)

3.for与np的联合使用,

4. import copy

5. np.clip(data,min,max),

6. np.where 两种用法

7. np.oneslike(x)

8. s_list = np.random.choice(self.schools_majors, batch_size)

9. np.expand_dims(im, axis=0) n h w c

10. .format

11. z.reshape(-1) 变成一行   z.reshape(-1,1)变成一列

12. continue遇到一种情形先跳过,这次循环

13.map 映射 map(表达式, 列表等) 不用for循环


1.bool类型的形参+return 的合理使用

  bool型的形参,代表的是一种情况,搭配return使用,一旦执行,return后面的语句不再执行。

  比如下面代表的是只去除左边的空白。    


2.总是习惯于for i in range这种,

   其实当i有明确含义时,如下for col in xrange(w)


3.for与np的联合使用

   这样就不用两个for嵌套使用

   例子:  

   如下:for col in xrange(w):

                   if(im[:,col,:]>240).all():如果全这一列全部都大于240,则左边索引+1


   备注:这里的all是np当中的,(im[:,col,:]>240)返回一个bool类型的ndarray,也可以all(im[:,col,:]>240)

    当你脑海中想到每一个,的时候,可以考虑any all。如:一列当中每一行的元素都要小于240,表明这一列是空白的。

#############多的地方代表要学习的地方
def remove_zero_padding(image, only_left=False):.
'''OCR分割出性别后去除两边空白的区域'''
    h, w = image.shape[:2]
    index_left = 0
    index_right = w - 1
    for col in range(w):
        if (image[:, col] >= 240).all():
            index_left = col + 1
        else:
            break  
    if only_left:#############
        return image[:, index_left:]#############
    
    for col in range(w):##########
        if (image[:, w - col - 1] >= 240).all():#############
            index_right = w - col - 1
        else:
            break
    
    return image[:, index_left:index_right]

4. import copy

    out = copy.deepcopy(im) 重新重建一个np,和原先的没关系

    out = im.copy() 也可以,操作的时候如下

    e.g. im = remove_zero_padding(im.copy()[:, 5:-5])

5. np.clip(data,min,max)

    小于min的都为min大于max都变为max

x=np.array([1,2,3,5,6,7,8,9])
np.clip(x,3,8)
array([3, 3, 3, 5, 6, 7, 8, 8])

6. np.where 两种用法

  • np.where(condition, x, y)

    满足条件(condition),输出x,不满足输出y。image = np.where(image >= 240, 255,image)

  • np.where(condition)

    a = np.array([2,4,6,8,10])

    a[np.where(a > 5)] # 等价于 a[a>5] array([ 6, 8, 10])


7. np.oneslike(x)

   返回一个用1填充的跟x 形状和类型 一致的数组。

8. s_list = np.random.choice(self.schools_majors, batch_size)

    #随机选取多少个学校或专业

9. np.expand_dims(im, axis=0) n h w c

10. .format

  索引: python3中  print("{0}/{1}".format(i,100000),end="\r"),{0}表示第0个,{1}表第一个。

for i in range(100000):
    print("{0}/{1}".format(i,100000),end="\r")
精度:print('{:.3f}'.format(123.45678)) 四舍五入
#类似字典
line = "{pic_id},{sex},{nation},{weight} kg,{blood_type},{native_place}".format(pic_id=pic_id,
                                                                                        sex=sex,
                                                                                        nation=nation,
                                                                                        weight=weight,
                                                                                        blood_type=blood_type,
                                                                                        native_place=native_place) + \
               "," + ",".join([school['gaozhong'], major['gaozhong'], xuewei['gaozhong'], time['gaozhong'], graduate['gaozhong']]) + \
               "," + ",".join([school['dazhuan'], major['dazhuan'], xuewei['dazhuan'], time['dazhuan'], graduate['dazhuan']]) + \
               "," + ",".join([school['benke'], major['benke'], xuewei['benke'], time['benke'], graduate['benke']]) + \
               "," + ",".join([school['yanjiusheng'], major['yanjiusheng'], xuewei['yanjiusheng'], time['yanjiusheng'], graduate['yanjiusheng']]) + \
               ",无" * 5        # 高中、专科、本科、研究生、其它(全是无)

11. z.reshape(-1) 变成一行   z.reshape(-1,1)变成一列

12. continue遇到一种情形先跳过,这次循环

    跳过太白的图片

if origin_im.mean() > 250:
    continue

13.map 映射 map(表达式, 列表等) 不用for循环

>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]

>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
[3, 7, 11, 15, 19]

猜你喜欢

转载自blog.csdn.net/e01528/article/details/82924389