2009-12-02 64 views
4

元组数据结构,我想有一个3项组合像标签,名称,和值(阵列)的名单什么是最好的可能的数据结构来存储这样的事情。有没有在Python

目前我使用的字典,但只允许2项,但容易穿越使用

for k, v in dict.iteritems(): 

我们可以有类似的喜欢的东西:

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

这是为什么被downvoted? – blank 2009-12-02 08:19:36

+5

问题标题有误导性;标题中问题的答案只是“是”。问题的实质实际上是要求存储一些数据的最佳结构,这是一个不同的问题。 – 2009-12-02 09:58:33

+0

-1:“最好可能”是没有意义的。请问一个可以回答的问题。最适合什么目的?速度?存储?代码复杂性?什么? – 2009-12-02 11:31:21

回答

3

可以考虑collections.namedtuple类型创建具有按属性查找可用的字段元组状物体。

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] 
>>> 

建筑上其他的答案,过滤mytup对象的列表的一个示例:

>>> 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对象可以,当然,可以在其它结构中使用(例如,dict),正如其他答案中所提到的。其优点是,很明显,该领域是命名为,并利用他们的代码更加清晰。

+0

而你如何创建一个这些namedtuple的数组来轻松遍历。我很抱歉,昨天刚刚启动了Python,仅用于编写Python脚本。 – 2009-12-02 09:05:25

+0

已添加列表过滤示例。 – gimel 2009-12-02 11:21:26

+1

'tlist = map(mytup,[(“foo”,“dog”,[1,2]),(“bar”,“cat”,[3,4]),])'J.F. – jfs 2009-12-02 11:28:41

4

为什么不只是使用一个元组列表(是的,这是Python中的数据类型,像列表,但不可变):

mylistoftuples = [(1, 2, 3), (2, "three", 4), (3, 4, [1, 2, 3, 4, 5])] 
for k, v, x in mylistoftuples: 
    print k, v, x 
+2

只是为了保持完整性,让我们注意,这严重失败的项目,查找部门。 – 2009-12-02 09:56:44

+1

我同意。请参阅下面的Kimvais以获得解决方案。 – 2009-12-02 13:17:54

0

你可以有一个3-i数组tem元组。

arr = [ (1,2,3), (4,5,6), (7,8,9)] 
for (k, v, x) in arr: 
    # do stuff 
8

Python tutorial on data structutres参见第5.3节“元组和序列”

但是,如果你想用“名”来索引的数据,你可能需要使用具有字符串名称作为关键字的词典和值是(tag,[list,of,values])的元组,例如

d = 
    { "foo" : ("dog", [1,2,3,4]), 
     "bar" : ("cat", [4,5,6,7,8,9]), 
     "moo" : ("cow", [4,5,7,8,9,1,3,4,65]) 
    } 

    for name,(tag,values) in d.items(): 
    do_something() 

这样也d["foo"]将工作,就像任何其他字典。

+0

这是一个比选择的更好的答案 - 它直接显示了提出的问题的解决方案。 – PaulMcG 2009-12-02 14:04:11

3

也许你应该看看这里:Python data structures

+2

+1 Python教程规则。对于我所见过的编程语言来说,这是最好的介绍之一,如果不是最好的。在阅读之前提出有关Python的任何问题是...我不知道... – 2009-12-02 07:55:13

1

下面是@gimel's answer评论:

>>> import collections 
>>> T = collections.namedtuple("T", 'tag name values') 
>>> from itertools import starmap 
>>> list(starmap(T, [("a", "b", [1,2]), ("c", "d",[3,4])])) 
[T(tag='a', name='b', values=[1, 2]), T(tag='c', name='d', values=[3, 4])] 
+0

工作良好和写短,但不知何故,我更喜欢gimel方法,它的外观更干净的阅读,但更多的打字。 – 2009-12-09 15:12:35