python,从列表中获取值
我有一个复杂的列表结构。像python,从列表中获取值,python,python-3.x,python-2.7,list,Python,Python 3.x,Python 2.7,List,我有一个复杂的列表结构。像 List-->字典-->列表-->字典-->列表-->元组 如何正确地从该结构中获取值: [{'row.0': [{'item': {'props': [('col', 'CTO ID'), ('val', '9141')]}}]}, {'row.1': [{'item': {'props': [('col', 'CTO ID'), ('val', '9142')]}}]}, {'row.2': [{'item': {'props': [('col', 'CTO
List-->字典-->列表-->字典-->列表-->元组
如何正确地从该结构中获取值:
[{'row.0': [{'item': {'props': [('col', 'CTO ID'), ('val', '9141')]}}]},
{'row.1': [{'item': {'props': [('col', 'CTO ID'), ('val', '9142')]}}]},
{'row.2': [{'item': {'props': [('col', 'CTO ID'), ('val', '9143')]}}]},
{'row.3': [{'item': {'props': [('col', 'CTO ID'), ('val', '9144')]}}]},
{'row.4': [{'item': {'props': [('col', 'CTO ID'), ('val', '9146')]}}]}]
我希望输出具有类似-['9142'、'9142'、'9143'…]的列表。
你能帮我吗?这应该会有帮助
s = [{'row.0': [{'item': {'props': [('col', 'CTO ID'), ('val', '9141')]}}]},
{'row.1': [{'item': {'props': [('col', 'CTO ID'), ('val', '9142')]}}]},
{'row.2': [{'item': {'props': [('col', 'CTO ID'), ('val', '9143')]}}]},
{'row.3': [{'item': {'props': [('col', 'CTO ID'), ('val', '9144')]}}]},
{'row.4': [{'item': {'props': [('col', 'CTO ID'), ('val', '9146')]}}]}]
res = []
for i in s: #Iterate your list
key, val = i.items()[0] #get key, value
res.append(val[0]['item']['props'][1][1]) #append your required value.
print res
输出:
['9141', '9142', '9143', '9144', '9146']
在python2.7中测试什么是“获取”值?你的预期产出是多少?到目前为止你试过什么?你问的是最基本的问题。拿本书打开怎么样?谢谢,我是编程新手,所以我的问题对你来说可能很简单@Suren Simonyan:你们列表的结构很好,也很复杂:)要得到答案会很困难如果这是一本教科书中的脑筋急转弯,你应该先急转弯。如果这是一个真实的数据结构,那么无论是谁提出了这个,都应该被带到后面去枪毙。谢谢Rakesh!