Python中四舍五入到三位小数



我有一个代码问题的答案,要求我绕过

第1部分:在下面的单元格中写一行代码,显示以下句子基于词典的情感极性得分:"在山上徒步旅行很有趣,也很放松。">

第2部分:"在山上徒步旅行很有趣,也很放松"这句话基于词典的情感极性得分是多少?使用精度的三位小数(例如0.321(报告您的答案。**

如何在代码片段中添加舍入函数?

get_lexicon_polarity('Hiking in the mountains is fun and very relaxing')

我知道我总是可以这样做的答案:

np.round(0.2310364009813431, 3)

但是-我想知道如何在原始代码中调用它。

这里有两种方式:

1:将get_lexicon_polarity的结果分配给一个变量,然后将该变量传递给np.round

lexicon_polarity = get_lexicon_polarity('Hiking in the mountains is fun and very relaxing')
np.round(lexicon_polarity, 3)

2:将带有参数的函数作为输入参数传递给np.round

np.round(get_lexicon_polarity('Hiking in the mountains is fun and very relaxing'), 3)

最新更新