python把str转换为int

简介: 1 def str2int(s): 2 def fn(x,y): 3 return x+y 4 def char2num(s): 5 return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7'...
1 def str2int(s):
2     def fn(x,y):
3         return x+y
4     def char2num(s):
5         return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s]
6     return reduce(fn,map(char2num,s))
7 
8 print(str2int('2468'))
9     

 

目录
相关文章
|
25天前
|
算法 开发者 Python
【Python 基础扫盲 】self参数、__init__方法和.__str__方法的用处和区别?
【Python 基础扫盲 】self参数、__init__方法和.__str__方法的用处和区别?
20 0
|
1月前
|
Python
Python系列(15)—— int类型转string类型
Python系列(15)—— int类型转string类型
|
8月前
|
XML 编解码 数据格式
Python 字符串str详解(超详细)(二)
Python 字符串str详解(超详细)(二)
77 0
|
4月前
|
Python
python str = ‘2023/11/9 0:00:00’ 转变成‘2023-11-09’
python str = ‘2023/11/9 0:00:00’ 转变成‘2023-11-09’
|
5月前
|
JSON 数据格式 Python
Python将字符串(str/json)和字典(dict)互转
Python将字符串(str/json)和字典(dict)互转
|
8月前
|
Python
Python 字符串str详解(超详细)(四)
Python 字符串str详解(超详细)(四)
64 0
|
8月前
|
机器学习/深度学习 移动开发 索引
Python 字符串str详解(超详细)(三)
Python 字符串str详解(超详细)(三)
75 0
|
8月前
|
存储 SQL 程序员
Python 字符串str详解(超详细)(一)
Python 字符串str详解(超详细)(一)
274 0
|
8月前
|
Python
Python3 ‘str‘ object has no attribute ‘decode‘. Did you mean: ‘encode‘?
Python3 ‘str‘ object has no attribute ‘decode‘. Did you mean: ‘encode‘?
167 0