[英]Creating a dictionary of lists from nested list
如何使用鍵列表從不均勻的嵌套列表中創建列表字典? 我嘗試了字典理解,但結果不正確。
假設這是列表:
value_list = [['this is news head', 'abstract of the news', 'link of the news', ['title1', 'title2', 'title3', ... 'title10' ]],['this is news head2', 'abstract of the news2', 'link of the news2', ['title1', 'title2', 'title3', ... 'title10' ]]]
key_list = ['head', 'abstract', 'link', 'titles']
我厭倦了這個:
d = {k: value_list[i::3][i::10] for i, k in enumerate(key_list)}
但它沒有給我這個 output:
output: [{'head':'this is news head', 'abstract':'abstract of the news', 'link': 'link of the news', 'titles': ['title1', 'title2', 'title3'...'title10']},{'head':'this is news head2', 'abstract':'abstract of the news2', 'link': 'link of the news2', 'titles': ['title1', 'title2', 'title3'...'title10']}]
我不確定您為什么要進行精美的索引。 這就是全部。
d = [dict( zip(key_list, v)) for v in value_list]
因此,對於每一行,我創建一個將值與鍵匹配的對的壓縮列表,然后將其轉換為字典。
Output:
[{'head': 'this is news head', 'abstract': 'abstract of the news', 'link': 'link of the news', 'titles': ['title1', 'title2', 'title3', 'title10']}, {'head': 'this is news head2', 'abstract': 'abstract of the news2', 'link': 'link of the news2', 'titles': ['title1', 'title2', 'title3', 'title10']}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.