博客
关于我
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 For循环多次返回
查看>>
python frame_python3 selenium自动化 frame表单嵌套的切换方法
查看>>
Python ftplib - 指定端口
查看>>
Python furl库:一键搞定复杂URL操作
查看>>
Python GC 也会关闭文件吗?
查看>>
python gen_key.py 报错 提示找不到OpenSSL lib
查看>>
python getattrribute_Python学习——面向对象高级之反射
查看>>
Python进阶语法:字典推导式
查看>>
python gil_Python中GIL的使用详解
查看>>
python glob.glob使用
查看>>
python glob的安装和使用
查看>>
Python google drive API 下载,文件在哪里?
查看>>
Python GPS 模块:读取最新的 GPS 数据
查看>>
python grpc入门
查看>>
python gRPC测试helloworld
查看>>
python list,str的拼接与转换
查看>>
python matplotlib简单使用
查看>>
python os.system
查看>>
Python os.system执行多条语句,os.system的返回值以及与os.popen的区别
查看>>
Python os和sys模块
查看>>