2009-12-02 61 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