+ -
当前位置:首页 → 问答吧 → 解析编码规则code='3-2-3-4'

解析编码规则code='3-2-3-4'

时间:2011-02-04

来源:互联网

  1. code='3-2-3-4'
  2. s=code.split('-')
  3. r=[]
  4. r.append(0)
  5. for i in range(len(s)):
  6.     r.append(int(s[i])+r[i])
  7. del r[0]
  8. print r
复制代码
输出[3, 5, 8, 12]
有没有更简单的方法?python嘛就是要简单

作者: zhaoyh603   发布时间: 2011-02-04

本帖最后由 a515200 于 2011-02-05 02:35 编辑
  1. def root(x,h='',y=0):
  2.     x1=[int(i) for i in x if i not in h]
  3.     while y<len(x1)-1:
  4.         r=x1[y+1]+x1[y]
  5.         x1[y+1]=r
  6.         y+=1
  7.     return x1
复制代码
参数h为要消去字符的集合,如果不需要消去则默认为空字符,因为我忘记什么内建方法可以消去字符x里的非数字字符了,用正则和循环会增加时间复杂度,就不用了

作者: a515200   发布时间: 2011-02-05

热门下载

更多