在 Python 中四舍五入到三位小数

时间:2021-05-29 03:44:57

标签: python text-mining

我的代码问题有一个答案,要求我四舍五入

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

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

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

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

我知道我总是可以对答案执行此操作:

np.round(0.2310364009813431, 3)

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

1 个答案:

答案 0 :(得分:1)

这里有两种方式:

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)