[英]Convert tuple of dictionary to list
如何轉換這個
({'content': 'home'}, {'content': 'home2'})
至
['home','home2']
抱歉,我是python編程的初學者。 希望有人可以幫我謝謝
使用列表理解。 假設您的元組存儲在變量tuple_list
。
arr = [i['content'] for i in tuple_list]
假設每個元素都是一個字典,並且只有一個始終為'content'
鍵:
t = ({'content': 'home'}, {'content': 'home2'})
values_list = [d['content"] for d in t]
閱讀python迭代器,列表理解以及元組和字典數據類型。
@Bianconi答案是正確的方法。 但是,您(初學者)的另一種方法可以是:
new_list=[] #new variable list
tuple_list=({'content': 'home'}, {'content': 'home2'}) #your data
# a for loop that read the data from tuple_list and put it into new_list.
for item in tuple_list:
new_list.append(item['content'])
print new_list
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.