原始数据 [('firstid', '1'), ('language', 'g'), ('lastid', '199'), ('sale', '759'), ('saledate', '16. Mai 2018'), ('section', 'Moderne Kunst'), ('action', 'Browse')]
转换结果是:
{'language': '[1,g]', 'section': 'Moderne Kunst', 'sale': '[759,16. Mai 2018]', 'saledate': '16. Mai 2018', 'action': 'Browse', 'lastid': '199'}
解决 别人提问的问题,问题网页是: https://segmentfault.com/q/1010000000161056
代码:
requests_list = [('language', '1'), ('language', 'g'), ('lastid', '199'), ('sale', '759'), ('saledate', '16. Mai 2018'),
('section', 'Moderne Kunst'), ('action', 'Browse')] key_list = [] for ii in requests_list: key = ii[0] key_list.append(key) key_list = list(set(key_list)) print key_list req_dict = {} req_dict_list = [] for kk in key_list: kk_list = [] for mm in requests_list: if kk in str(mm): kk_list.append(mm[1]) print len(kk_list) print (kk_list) if len(kk_list)>=2: kk_list_str = ','.join(kk_list) kk_list_str = "[" + kk_list_str + "]" else: kk_list_str = kk_list[0] req_dict_list.append(kk_list_str) print req_dict_list print kk print dict(zip(key_list,req_dict_list))