学习Python过程中的一些记录,慢慢更新中,希望能坚持下去~

文件读取错误

Image data cannot be converted to float

这个出现的的原因一般是没有读取到所需图片,一般是路径问题,也有可能是图片不合规范,这时候将图片或者文件放到程序的文件夹内,或者改为绝对路径就可以。

不过我试了,改绝对路径可以,将图片放到文件夹内仍然不行(用cv2.imread())读取文件。

一些函数的使用

创建匿名函数

Lambda函数又称匿名函数,有些函数如果只是临时一用,而且它的业务逻辑也很简单时,就可以简单的写一下

>>> add = lambda x, y : x+y
>>> add
<function <lambda> at 0x102bc2140>
>>> add(1,2)
3

random()

import random

print( random.randint(1,10) )        # 产生 1 到 10 的一个整数型随机数  
print( random.random() )             # 产生 0 到 1 之间的随机浮点数
print( random.uniform(1.1,5.4) )     # 产生  1.1 到 5.4 之间的随机浮点数,区间可以不是整数
print( random.choice('tomorrow') )   # 从序列中随机选取一个元素
print( random.randrange(1,100,2) )   # 生成从1到100的间隔为2的随机整数

ceil()函数

ceil() 函数返回数字的上入整数。

import math   # This will import math module

print "math.ceil(-45.17) : ", math.ceil(-45.17)
print "math.ceil(100.12) : ", math.ceil(100.12)
print "math.ceil(100.72) : ", math.ceil(100.72)
print "math.ceil(119L) : ", math.ceil(119L)
print "math.ceil(math.pi) : ", math.ceil(math.pi)

math.ceil(-45.17) :  -45.0
math.ceil(100.12) :  101.0
math.ceil(100.72) :  101.0
math.ceil(119L) :  119.0
math.ceil(math.pi) : 4.0

map()函数

map() 函数会根据提供的函数对指定序列做映射。
map(function, iterable, …)
function – 函数
iterable – 一个或多个序列

>>> def square(x) :         # 计算平方数
...     return x ** 2
...
>>> map(square, [1,2,3,4,5])    # 计算列表各个元素的平方
<map object at 0x100d3d550>     # 返回迭代器
>>> list(map(square, [1,2,3,4,5]))   # 使用 list() 转换为列表
[1, 4, 9, 16, 25]
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))   # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]

注意map()在列表时的使用

a = [1,2]
b = [3,4]

def add(a, b):
    return a+b

print(list(map(add, a, b)))
print(add(a,b))

#输出结果
[4,6]
[1,2,3,4]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值