mmdbcn 发表于 2018-8-16 06:58:10

【Python3】04、内置数据结构

#############  
In : mapping = {str(x):x for x in range(9)}
  

  
In : mapping
  
Out: {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8}
  

  
In : s.partition('.')
  
Out: ('123', '.', '456')
  

  
In : s = "123"
  

  
In : s.partition('.')
  
Out: ('123', '', '')
  

  
In : s = "12.3"
  

  
In : s.partition('.')
  
Out: ('12', '.', '3')
  

  
In : s = "123.456"
  

  
In : i, _, f = s.partition('.')
  

  
In : ret = 0
  

  
In : for idx, x in enumerate(i[::-1]):
  
    ...:   ret += mapping * 10 ** idx
  
    ...:
  

  
In : ret
  
Out: 123
  

  
In : for idx, x in enumerate(f):
  
   ...:   ret += mapping / 10 ** (idx+1)
  
   ...:
  

  
In : ret
  
Out: 123.456      #结果可能会有精度的问题
  

  

  
###先全部当成整数来运算###
  
In : ret = 0
  

  
In : for idx, x in enumerate((i+f)[::-1]):
  
   ...:   ret += mapping * 10 ** idx
  
   ...:
  

  
In : ret / 10 ** len(f)
  
Out: 123.456             #精度损失会少一点
页: [1]
查看完整版本: 【Python3】04、内置数据结构