博客
关于我
python 实现字符串转整型
阅读量:796 次
发布时间:2023-03-07

本文共 583 字,大约阅读时间需要 1 分钟。

def str2Int(s):        l = list(s)    if len(l) <= 0:        return 0    flag = 0    sum = 0    dict_num = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}    dict_tag = {'+': 1, '-': -1} if not dict_tag.__contains__(l[0]) and not dict_num.__contains__(l[0]):    return 0elif dict_tag.__contains__(l[0]):    flag = dict_tag[l[0]]else:    sum += dict_num[l[0]]for i in range(1, len(l)):    if l[i] < '0' or l[i] > '9':        return 0    else:        sum = sum * 10 + dict_num[l[i]]if flag == -1:    return -sumelse:    return sum    

 

转载于: [原文链接]

你可能感兴趣的文章
python os文件/目录
查看>>
Python Package 之 Faker(随机姓名、电话)
查看>>
python pandas TimeStamps到夏令时的本地时间字符串
查看>>
Python Pandas 用顶行替换标题
查看>>
Python Pandas-从DataFrame按类别绘制多个条形图
查看>>
Python PyQt5 将不再显示此消息复选框添加到 QMessageBox
查看>>
Python PyQt5:如何使用 PyQt5 显示错误消息
查看>>
Python pytest 面试题!
查看>>
Python pytz 时区函数返回一个相差 9 分钟的时区
查看>>
python rabbitmq实现简单/持久/广播/组播/topic/rpc消息异步发送可配置Django
查看>>
Python random和json模块
查看>>
Python random模块seed理解
查看>>
python range()函数
查看>>
Python rdflib可传递查询
查看>>
python redis 集群_python 搭建redis集群
查看>>
python redis连接,在Python中使用Redis连接池的正确方法
查看>>
python regex_Python RegEx
查看>>
python requests post 中文结果请求得到unicode
查看>>
Python Requests接口自动化测试实战
查看>>
Python requests模块
查看>>