python - Django - 我如何错误地使用 python 映射?

标签 python django

我在 View 代码中收到来自 django 的类型错误。 尝试四舍五入到最接近的 5。即 7 变成 10 或 123 变成 125,等等

我有一个整数列表

valList = [ int(i) for i in str(int(math.ceil(result))) ]

然后我改变一个数字

valList[len(valList)-1] = 5

然后组合这些值再次创建一个整数。

result = ''.join(map(str, valList))

然后转换回一个整数

result = int(result)

当我使用 python 控制台(以及在 django shell 中)时,此方法有效,但是当我运行这是我的 django View 时,我得到一个 TypeError Exception 错误是 map() takes恰好 1 个参数(给定 2 个)

我不知道是什么原因造成的......或者我做错了什么。

谢谢。

附注如果有人知道四舍五入到最接近的 5 的优雅解决方案,我会洗耳恭听。

最佳答案

数学解决方案如何:

>>> def f(n):
...   q, r = divmod(n,5)
...   if r == 0:
...     return q * 5
...   else:
...     return (q * 5) + 5
...
>>> f(7)
10
>>> f(123)
125
>>> f(120)
120

关于python - Django - 我如何错误地使用 python 映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7023009/

相关文章:

python - 为什么 IPython 在它启动的目录中执行 code.py?

python - 每次执行代码后 python 列表大小持续增加

python - 从两个类别列 pandas 创建一个类别列

python - django - 使用不同的基本模板渲染任何网址

python - 在pygame中旋转列表

python - 雪豹上的 Django 和 PIL

python - 有使用 Pypy 的大项目吗?

python - Django Rest 框架中的最佳实现 follow 和 unfollow

python-3.x - 使用 Djangobulk_update() 时,“dict”对象没有属性 'pk'

python - 如何在由 uswgi 提供服务并由 systemd 服务启动的虚拟环境中配置 Django 应用程序?