2013-07-02 128 views
0

嗨得到變量i具有打印出以下變量:如何從列表字段

data=[('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b')] 

我使用python我想知道如何我從這個名單中提取LOCATION_NAME的例如內容,我曾嘗試執行以下操作:

data[0]// this prints out ('location_name', u'b') 

我要的是讓LOCATION_NAME的內容,所以在這種情況下,我得到b

非常感謝

回答

3

你可以像下面這樣做

data=[('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b')] 
d = dict(data) 

print d["location_name"] 
+0

然而,這是不值得創建的字典,在你只需要做一次查詢的情況下 –

+0

@gnibbler他已經在字典的形式,他只是不會使用它。 http://stackoverflow.com/questions/17436156/how-to-repopulate-wtforms – John

+0

@gnibbler,我也只是錯過了每日代表頂級,upvote, John

1

你有一個元組列表。因此,要打印該值,您只需執行以下操作:

print data[0][1] 
0

您可以使用索引訪問數據。 像 test=[(1, 2, 3), (4, 5, 6), (5, 6, 7)]

teste[0] # will access the first element that is (1, 2, 3) 
'(1, 2, 3)' 
teste[0][0] # will access the element (1, 2, 3) then the second index [0] will access 1 
'1' 

由於內部的元素,更多的索引你將需要。