python - 我在 Python v 3.3.2 中遇到集合和元组问题

标签 python tuples

我在集合中使用 namedtuple。我收到这个错误

NameError: name collections is not defined.  

我使用的是 Python v 3.3.2。任何帮助将不胜感激

[例如]我在这条线上遇到错误

Sale = collection.namedtuple("Sale","productid custerid date price")

最佳答案

您是否导入了collections 模块?模块名称是 collections,而不是 collection

>>> Sale = collection.namedtuple("Sale","productid custerid date price")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'collection' is not defined
>>>
>>> import collection
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named collection
>>>
>>> import collections
>>> Sale = collections.namedtuple("Sale","productid custerid date price")

关于python - 我在 Python v 3.3.2 中遇到集合和元组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20438063/

相关文章:

python - Django REST Framework 如何将上下文添加到 ViewSet

python - 如何解压嵌套列表中的元组?

python - 列中元组列表中的小写字符串

Python:进行重复计数时删除重复元素

python - Pandas 是如何计算指数移动平均线的?

python - 如何在python中安装png模块

python - wxPython:刷新和更新组合框

python - 针对 Access 数据库的 pyodbc 查询出现 "Optional feature not implemented"错误

swift - Swift 中元组的多级排序数组

Python ascii 元组