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     

 

目录
相关文章
|
4月前
|
索引 Python
python字符串(str)
【5月更文挑战第8天】
31 3
|
26天前
|
存储 索引 Python
Python中的str类型
Python中的str类型
|
26天前
|
存储 C语言 Python
Python中的int语句:深入探索与应用
Python中的int语句:深入探索与应用
|
2月前
|
存储 数据处理 索引
数据类型转换:int()、str()、float()
在Python中,数据类型转换是一项基础且重要的操作
|
2月前
|
存储 JSON JavaScript
【Python】已完美解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
【Python】已完美解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
60 1
|
2月前
|
机器学习/深度学习 缓存 安全
Python标准库中的`str`类型有一个`translate()`方法,它用于替换字符串中的字符或字符子集。这通常与`str.maketrans()`方法一起使用,后者创建一个映射表,用于定义哪些字符应该被替换。
Python标准库中的`str`类型有一个`translate()`方法,它用于替换字符串中的字符或字符子集。这通常与`str.maketrans()`方法一起使用,后者创建一个映射表,用于定义哪些字符应该被替换。
|
2月前
|
开发者 Python
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
56 0
|
2月前
|
开发者 Python
【Python】已解决:TypeError: a bytes-like object is required, not ‘int’
【Python】已解决:TypeError: a bytes-like object is required, not ‘int’
61 0
|
2月前
|
存储 Python
语音输入,python数据类型,type()用来查看数据类型,数据类型转换,int(x)转整数,float(x)转换为浮点数,str(x),将对象转为字符串,标识符,标识符不允许使用关键字,关键字参考
语音输入,python数据类型,type()用来查看数据类型,数据类型转换,int(x)转整数,float(x)转换为浮点数,str(x),将对象转为字符串,标识符,标识符不允许使用关键字,关键字参考