Python中是否有元组数据结构 [英] Is there a tuple data structure in Python

查看:30
本文介绍了Python中是否有元组数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个包含标签、名称和值列表(数组)的 3 项组合,什么是存储此类内容的最佳数据结构.

目前我正在使用字典,但它只允许 2 个项目,但使用很容易遍历

for k, v in dict.iteritems():

我们可以有类似的东西吗:

for k, v, x in tuple.iteritems():

解决方案

你可以考虑collections.namedtuple 类型来创建类似元组的对象,这些对象具有可通过属性查找访问的字段.

<块引用>

collections.namedtuple(typename, field_names[,verbose])

返回一个名为 typename 的新元组子类.新的子类用于创建类似元组的对象,这些对象具有可通过属性查找访问的字段以及可索引和可​​迭代的.子类的实例也有一个有用的文档字符串(带有 typename 和 field_names)和一个有用的 __repr__() 方法,它以 name=value 格式列出元组内容.

<预><代码>>>>进口藏品>>>mytup = collections.namedtuple('mytup', ['tag','name', 'values'])>>>e1 = mytup('tag1','great',[1,'two',3])>>>e1mytup(tag='tag1', name='great', values=[1, 'two', 3])>>>e1.values[1, '二', 3]>>>

以其他答案为基础,过滤mytu​​p 对象列表的示例:

<预><代码>>>>tlist = [mytup("foo", "dog", [1,2,3,4]),mytup("bar","cat", [4,5,6,7,8,9]), mytup("moo","cow", [4,5,7,8,9,1,3,4,65])]>>>列表[mytup(tag='foo', name='dog', values=[1, 2, 3, 4]),mytup(tag='bar', name='cat', values=[4, 5, 6, 7, 8, 9]),mytup(tag='moo', name='cow', values=[4, 5, 7, 8, 9, 1, 3, 4, 65])]>>>[t for t in tlist if t.tag == 'bar'][mytup(tag='bar', name='cat', values=[4, 5, 6, 7, 8, 9])]>>>

Namedtuple 对象当然可以用于其他结构(例如 dict),如其他答案中所述.显然,优点是字段被命名,使用它们的代码更清晰.

I want to have an 3 item combination like tag, name, and list of values (array) what is the best possible data structure to store such things.

Current I am using dictionary, but it only allows 2 items, but easy traversal using

for k, v in dict.iteritems():

can we have something similar like:

for k, v, x in tuple.iteritems():

解决方案

You can consider the collections.namedtuple type to create tuple-like objects that have fields accessible by attribute lookup.

collections.namedtuple(typename, field_names[, verbose])

Returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable. Instances of the subclass also have a helpful docstring (with typename and field_names) and a helpful __repr__() method which lists the tuple contents in a name=value format.

>>> import collections
>>> mytup = collections.namedtuple('mytup', ['tag','name', 'values'])
>>> e1 = mytup('tag1','great',[1,'two',3])
>>> e1
mytup(tag='tag1', name='great', values=[1, 'two', 3])
>>> e1.values
[1, 'two', 3]
>>> 

Building on other answers, an example of filtering a list of mytup objects:

>>> tlist = [mytup("foo", "dog", [1,2,3,4]),
    mytup("bar","cat", [4,5,6,7,8,9]), mytup("moo","cow", [4,5,7,8,9,1,3,4,65])]
>>> tlist
[mytup(tag='foo', name='dog', values=[1, 2, 3, 4]),
mytup(tag='bar', name='cat', values=[4, 5, 6, 7, 8, 9]),
mytup(tag='moo', name='cow', values=[4, 5, 7, 8, 9, 1, 3, 4, 65])]
>>> [t for t in tlist if t.tag == 'bar']
[mytup(tag='bar', name='cat', values=[4, 5, 6, 7, 8, 9])]
>>> 

Namedtuple objects can, of course, be used in other structures (e.g a dict), as mentioned in other answers. The advantage is, obviously, that the fields are named, and code using them is clearer.

这篇关于Python中是否有元组数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆