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 0 elif 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 -sum else: return sum
来源:https://www.cnblogs.com/gczr/p/8081392.html