簡體   English   中英

使用相同的鍵將一個字典的列表轉換為兩個字典的列表

[英]Turn a list of one dictionary into a list of two dictionaries using the same key

這是我的代碼:

new_weights = [[1, 2, 3], [4, 5, 6]]
myList = [{'weights': new_weights}]

這將返回: [{'weights': [[1, 2, 3], [4, 5, 6]]}]

怎么可能得到以下內容?

[{'weights': [1, 2, 3]}, {'weights': [4, 5, 6]}]

使用列表理解如下:

new_weights = [[1, 2, 3], [4, 5, 6]]
myList = [{'weights': new_weights}]

result = [
    {'weights': element}
    for element in new_weights
]
print(result)

輸出:

[{'weights': [1, 2, 3]}, {'weights': [4, 5, 6]}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM