Division in Python 3 gives different result than in Python 2

后端 未结 3 647
北恋
北恋 2020-12-04 03:09

In the following code, I want to calculate the percent of G and C characters in a sequence. In Python 3 I correctly get 0.5, but on Python 2 I get 0

3条回答
  •  误落风尘
    2020-12-04 03:39

    For Python2 / is integer division when the numerator and denominator are both int, you need to make sure to force floating point division

    eg.

    return (seq.count('G') + seq.count('C')) / float(len(seq))
    

    alternatively, you can put

    from __future__ import division
    

    at the top of the file

提交回复
热议问题