如何从列表字段中获取变量

时间:2013-07-02 23:46:36

标签: python flask-wtforms

嗨我有一个打印出以下内容的变量:

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 个答案:

答案 0 :(得分:3)

你可以这样做

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

print d["location_name"]

答案 1 :(得分:1)

你有一个元组列表。因此,要打印该值,您只需要这样做:

print data[0][1]

答案 2 :(得分: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'

与元素一样内在,你需要更多的索引。