[英]Convert tuple of dictionary to list
how to convert this 如何转换这个
({'content': 'home'}, {'content': 'home2'})
to 至
['home','home2']
sorry i am a beginner in python programming. 抱歉,我是python编程的初学者。 hope someone can help me thanks
希望有人可以帮我谢谢
Use list comprehension. 使用列表理解。 This assumes your tuple is stored in the variable
tuple_list
. 假设您的元组存储在变量
tuple_list
。
arr = [i['content'] for i in tuple_list]
Assuming every element is a dictionary with a single key that is always 'content'
: 假设每个元素都是一个字典,并且只有一个始终为
'content'
键:
t = ({'content': 'home'}, {'content': 'home2'})
values_list = [d['content"] for d in t]
Read up on python iterators, list comprehensions and tuple and dict datatypes. 阅读python迭代器,列表理解以及元组和字典数据类型。
The @Bianconi answer is the correct way. @Bianconi答案是正确的方法。 But another way for you(beginner) can be:
但是,您(初学者)的另一种方法可以是:
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
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.