博客
关于我
python 实现字符串转整型
阅读量:795 次
发布时间: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课程的中期报告范文_课题研究中期总结报告范文
查看>>
python | lxml,一个超酷的 关于XML/HTML 文档 Python 库!
查看>>
python | mplfinance,一个有趣的金融数据可视化 Python 库!
查看>>
python | nipy,一个强大的关于 神经影像数据分析 的Python 库!
查看>>
python | NLTK,一个强大的 自然语言处理 Python 库!
查看>>
python | nupic,一个强大的 处理时间序列的Python 库!
查看>>
python | orange3,一个神奇的 Python 库!
查看>>
python | pdfminer,一个神奇的 关于PDF 文件的 Python 库!
查看>>
python | pendulum,一个有趣的 日期和时间 Python 库!
查看>>
python | pluginbase,一个神奇的 关于插件框架 的Python 库!
查看>>
python | ply,一个无敌的 词法和语法分析工具 的Python 库!
查看>>
python | py2exe,一个超酷的 Python 库!
查看>>
python | pyautogui,一个超酷的 Python 库!
查看>>
python | pybaobabdt,一个超强的 决策树可视化 Python 库!
查看>>
python | pycco,一个神奇的 Python 库!
查看>>
python | pyg2plot,一个有趣的 数据可视化 Python 库!
查看>>
python | pymc,一个超强的 Python 库!
查看>>
python | pynsist,一个强大的 Python 库!
查看>>
python | pyparsing,一个强大的 Python 库!
查看>>
python | pyqtgraph,一个神奇的 Python 库!
查看>>