- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我尝试使用以下方法将 namedtuple 转换为字典 [python 2.7.12] 时,发现 namedtuple._as_dict()
比第一种方法慢 10 倍以上。谁能告诉我这背后的原因是什么?
In [1]: Container = namedtuple('Container', ['name', 'date', 'foo', 'bar'])
In [2]: c = Container('john','10-2-2017',20.78,'python')
In [3]: %timeit dict(name=c.name,date=c.date,foo=c.foo,bar=c.bar)
The slowest run took 7.57 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 536 ns per loop
In [4]: %timeit c._asdict()
The slowest run took 4.84 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.19 µs per loop
最佳答案
因为 ._asdict
返回一个 OrderedDictionary
:
>>> c._asdict()
OrderedDict([('name', 'john'), ('date', '10-2-2017'), ('foo', 20.78), ('bar', 'python')])
>>>
请注意,如果您不关心顺序,那么使用字典文字应该是最快的方法:
In [5]: %timeit dict(name=c.name,date=c.date,foo=c.foo,bar=c.bar)
...:
The slowest run took 6.13 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 795 ns per loop
In [6]: %timeit c._asdict()
The slowest run took 4.13 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 2.25 µs per loop
In [7]: %timeit {'name':c.name, 'date':c.date, 'foo':c.foo, 'bar':c.bar}
The slowest run took 7.08 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 424 ns per loop
关于python - 为什么 namedtuple._as_dict() 比使用 dict() 转换慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47432731/
typing module documentation表示下面的两个代码片段是等价的。 from typing import NamedTuple class Employee(NamedTuple)
是否可以在另一个 namedtuple 中包含一个 namedtuple? 例如: from collections import namedtuple Position = namedtuple('
为什么我不能 pickle typing.NamedTuple 而我可以 pickle collections.namedtuple?我如何设法 pickle NamedTuple? 这段代码展示了我
我正在尝试创建一个 NamedTuple,其中一个字段默认为空字典。这大部分都有效,但是默认值在 NamedTuple 的实例之间共享: from typing import NamedTuple,
我正在寻找一种类似结构的数据结构,我可以从中创建多个实例并具有某种类型提示而不是不可变的。 所以我有这样的东西: class ConnectionConfig(NamedTuple): nam
在下面的代码中: point=namedtuple('point','x y') 我很困惑,一个类被视为一个值。就像我做的那样: class point: ... 它在命名空间中创建一个名为
考虑以下扩展 list 并附加 orderedtuple 的类: from collections import namedtuple Order = namedtuple('Order', ['id
我一直在使用这个基本代码来构建一个简单的库存系统。我需要扩展它以跟踪离开库存的另一个零件列表。我想我可以通过为数据结构创建另一个命名元组来做到这一点。我遇到一些问题,因为两个命名元组需要存在并同时访问
Python namedtuple 工厂函数允许它创建的子类的名称被指定两次——第一次在声明的左侧,然后作为函数的第一个参数(IPython 1.0.0 , Python 3.3.1): In [1]
最近我有一个关于数据类型的问题。 从那时起,我一直在尝试使用 NamedTuples(或多或少取得了成功)。 我目前的问题: - 如何将行从文件导入到新元组, - 如何将以空格/制表符(/无论什么)分
我有一个类,它几乎没有静态字段,并且是从可迭代对象初始化的(比如 csvreader 的输出)。 __init__ 为其中一些执行从字符串到数字的类型转换: class PerformanceTest
我知道 collections.namedtuple 的存在和目的,但我注意到,至少在 IDLE (3.2.2) 中,这个工厂函数也在 functools: >>> import functools
我想存储一个维度 namedtuple (x, y)。在我的整个程序中我只需要一次。 我能做到: Dimension = namedtuple('Dimension', ['x', 'y']) dim
有没有办法为 python 中的 namedtuple 重载相等运算符 __eq__(self, other)? 我知道这在类和重新定义方法中是可能的,但这对于 namedtuple 是否也可能,您将
这个问题在这里已经有了答案: Why should I use CamelCase for namedtuple? (1 个回答) 关闭 3 年前。 我是 Python 的新手,我一直在阅读在线文档
我们像这样使用 namedtuple: >>> from collections import namedtuple >>> Point = namedtuple('Point', ['x', 'y'
我正在处理命名元组列表。我想在创建每个命名元组后为其添加一个字段。似乎我可以通过将它作为属性引用来做到这一点(如 namedtuple.attribute = 'foo'),但它不会添加到字段列表中。
我正在尝试记录 namedtuple。当我构建文档时,我收到警告 WARNING: duplicate object description 和在记录的函数之后相同的空函数。例如: 如何删除那些别名?
在 Python 3.6 之后,我们有 typing.NamedTuple ,它是 collections.namedtuple() 的类型化版本,我们可以像类一样继承它: class Employe
鉴于我想从类型模块中正确使用命名元组的类型注释: from typing import NamedTuple, List class Foo(NamedTuple): my_list: Lis
我是一名优秀的程序员,十分优秀!