How to show decimal point only when it's not a whole number?

后端 未结 3 1087
半阙折子戏
半阙折子戏 2020-12-03 15:14

I have Googled, but couldn\'t find a proper answer to this.

Let\'s say we have floats and we get their averages. Their averages are like this:

3.5
2.         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 15:42

    Similar to the previous answer:

    [int(i) if int(i) == i else i for i in li]
    

    Or:

    [int(i) if not i % 1 else i for i in li]
    

提交回复
热议问题