什么时候以及为什么我应该使用namedtuple而不是字典?(复制)

时间:2021-04-10 15:08:05

Possible Duplicate:
What are “named tuples” in Python?

可能的重复:在Python中什么是“命名元组”?

What is the situation where a namedtuple should be used?

应该使用名称元组的情况是什么?

When I looked into it, it looked like a way to make tuples more like dictionaries. How do they compare to dictionaries?

当我研究它时,它看起来像是一种使元组更像字典的方法。它们与字典相比如何?

Can only the same hashable types for dictionaries be used for namedtuples?

只能使用相同的可转换的字典类型来命名名称元组吗?

2 个解决方案

#1


171  

In dicts, only the keys have to be hashable, not the values. namedtuples don't have keys, so hashability isn't an issue.

在dicts中,只有键是可洗的,而不是值。命名元组没有键,所以hashaability不是问题。

However, they have a more stringent restriction -- their key-equivalents, "field names", have to be strings.

然而,它们有一个更严格的限制——它们的键等价物“字段名”必须是字符串。

Basically, if you were going to create a bunch of instances of a class like:

基本上,如果你要创建一个类的实例,比如:

class Container:
    def __init__(self, name, date, foo, bar):
        self.name = name
        self.date = date
        self.foo = foo
        self.bar = bar

mycontainer = Container(name, date, foo, bar)

and not change the attributes after you set them in __init__, you could instead use

在__init__中设置属性后不要更改属性,您可以使用

Container = namedtuple('Container', ['name', 'date', 'foo', 'bar'])

mycontainer = Container(name, date, foo, bar)

as a replacement.

作为替代。

Of course, you could create a bunch of dicts where you used the same keys in each one, but assuming you will have only valid Python identifiers as keys and don't need mutability,

当然,您可以创建一堆规则,在每个规则中使用相同的键,但是假设您只有有效的Python标识符作为键,并且不需要可变性,

mynamedtuple.fieldname

is prettier than

是漂亮的比

mydict['fieldname']

and

mynamedtuple = MyNamedTuple(firstvalue, secondvalue)

is prettier than

是漂亮的比

mydict = {'fieldname': firstvalue, 'secondfield': secondvalue}

Finally, namedtuples are ordered, unlike regular dicts, so you get the items in the order you defined the fields, unlike a dict.

最后,命名元组是有序的,不像常规的命令,所以您可以按照定义字段的顺序获得条目,不像命令。

#2


29  

Tuples are immutable, whether named or not. namedtuple only makes the access more convenient, by using names instead of indices. You can only use valid identifiers for namedtuple, it doesn't perform any hashing — it generates a new type instead.

元组是不可变的,无论命名与否。namedtuple只通过使用名称而不是索引来使访问更加方便。您只能对namedtuple使用有效的标识符,它不执行任何散列,而是生成一个新的类型。

#1


171  

In dicts, only the keys have to be hashable, not the values. namedtuples don't have keys, so hashability isn't an issue.

在dicts中,只有键是可洗的,而不是值。命名元组没有键,所以hashaability不是问题。

However, they have a more stringent restriction -- their key-equivalents, "field names", have to be strings.

然而,它们有一个更严格的限制——它们的键等价物“字段名”必须是字符串。

Basically, if you were going to create a bunch of instances of a class like:

基本上,如果你要创建一个类的实例,比如:

class Container:
    def __init__(self, name, date, foo, bar):
        self.name = name
        self.date = date
        self.foo = foo
        self.bar = bar

mycontainer = Container(name, date, foo, bar)

and not change the attributes after you set them in __init__, you could instead use

在__init__中设置属性后不要更改属性,您可以使用

Container = namedtuple('Container', ['name', 'date', 'foo', 'bar'])

mycontainer = Container(name, date, foo, bar)

as a replacement.

作为替代。

Of course, you could create a bunch of dicts where you used the same keys in each one, but assuming you will have only valid Python identifiers as keys and don't need mutability,

当然,您可以创建一堆规则,在每个规则中使用相同的键,但是假设您只有有效的Python标识符作为键,并且不需要可变性,

mynamedtuple.fieldname

is prettier than

是漂亮的比

mydict['fieldname']

and

mynamedtuple = MyNamedTuple(firstvalue, secondvalue)

is prettier than

是漂亮的比

mydict = {'fieldname': firstvalue, 'secondfield': secondvalue}

Finally, namedtuples are ordered, unlike regular dicts, so you get the items in the order you defined the fields, unlike a dict.

最后,命名元组是有序的,不像常规的命令,所以您可以按照定义字段的顺序获得条目,不像命令。

#2


29  

Tuples are immutable, whether named or not. namedtuple only makes the access more convenient, by using names instead of indices. You can only use valid identifiers for namedtuple, it doesn't perform any hashing — it generates a new type instead.

元组是不可变的,无论命名与否。namedtuple只通过使用名称而不是索引来使访问更加方便。您只能对namedtuple使用有效的标识符,它不执行任何散列,而是生成一个新的类型。