我有以下内容:
[('first_name', u'Renier'), ('second_name', u''), ('surname', u'de Bruyn'), ('own_transport', u'True'), ('id_number', u'910920254081'), ('id_type', u'IDNumber'), ('id_nationality', u'ZA'), ('disabilities_list', u'Intellectual Disability'), ('disabilities_list', u'Memory Loss'), ('disabilities_list', u'Mental Illness'), ('disabilities_list', u'Physical Disability'), ('disabilities_list', u'Speech and Language Disorders'),...]
如何获取这些元组的值并使用值disabilities_list
来创建新列表?
我试过了:
for k in arr:
if k == "disabilities_list":
print k # this only returns "disabilities_list" 5 times
更新
arr
是从表单返回的内容,这是我print arr
时得到的:
NestedMultiDict([('first_name', u'Renier'), ('second_name', u''), ('surname', u'de Bruyn'), ('race', u'Caucation'), ('own_transport', u'True'), ('id_number', u'9109205021082'), ('id_type', u'IDNumber'), ('id_nationality', u'ZA'), ('disabitity', u'True'), ('disabilities_list[]', u'Memory Loss'), ('disabilities_list[]', u'Mental Illness'), ('disabilities_list[]', u'Physical Disability'), ('disabilities_list[]', u'Speech and Language Disorders'), ('disabilities_list[]', u'Vision Loss and Blindness'), ('mobile_number', u'0828245092'), ('home_number', u'0317016554'), ('email', u'renierdbruyn@hotmail.com'), ('other_contact', u''), ('line1', u'2 Hamilton Road'), ('line2', u''), ('line3', u''), ('city_town', u'Durban'), ('province', u'GP'), ('postal_code', u'3610'), ('suburb', u'Ashley'), ('country', u'ZA'), ('notice_period', u'4 weeks'), ('for_position', u'Developer'), ('curr_renumeration', u'12.0'), ('expect_renumeration', u'123.0'), ('relocate', u'True')])
答案 0 :(得分:4)
>>>arr = [('first_name', u'Renier'), ('second_name', u''), ('surname', u'de Bruyn'), ('own_transport', u'True'), ('id_number', u'910920254081'), ('id_type', u'IDNumber'), ('id_nationality', u'ZA'), ('disabilities_list', u'Intellectual Disability'), ('disabilities_list', u'Memory Loss'), ('disabilities_list', u'Mental Illness'), ('disabilities_list', u'Physical Disability'), ('disabilities_list', u'Speech and Language Disorders'),...]
>>>
>>>[k[1] for k in arr if k[0] == "disabilities_list[]"]
[u'Intellectual Disability', u'Memory Loss', ...]
修改:根据更新后的问题更新:
>>>[item[1] for item in arr.items() if item[0] == "disabilities_list"] # will do, what you want
答案 1 :(得分:1)
if k[0] == "disabilities_list":
答案 2 :(得分:1)
也许这就是你想要的:
>>> for k,v in arr.iteritems():
... if k == 'disabilities_list':
... print v