I have a very large list alist
which has a dict and a nested list like this:
我有一个非常大的列表alist,它有一个dict和一个嵌套列表,如下所示:
a_list = [{'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}},
{'A': [{'dictionary': 'True',
'item': 'pineapples',
'id': '13',
'score': '9.7899',
'rollup': {'True': 'OK', 'Fiz': 'Yes'},
'variant_list': [{'endp': '8', 'form': 'pineapple', 'register': '0'}]}], 'status': {'codecheck': '0', 'cred': '90809890', 'msg': 'OK'}},
......
{'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}},
]
How can I extract the extract the item
parameter if and only if exist into a list like this:
当且仅当存在于如下列表中时,如何提取项目参数的提取:
['NaN', 'pineapples', 'NaN']
I do not understand how to parse it since it has a very nested structure, the main issue which I am struggling with is to accessing to each element of the list and then to the other list and leaving a NaN
string.
我不明白如何解析它,因为它有一个非常嵌套的结构,我正在努力的主要问题是访问列表的每个元素,然后到另一个列表并留下一个NaN字符串。
1 个解决方案
#1
1
Use the following approach(list comprehension):
使用以下方法(列表理解):
a_list = [{'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}},
{'A': [{'dictionary': 'True',
'item': 'pineapples',
'id': '13',
'score': '9.7899',
'rollup': {'True': 'OK', 'Fiz': 'Yes'},
'variant_list': [{'endp': '8', 'form': 'pineapple', 'register': '0'}]}],
'status': {'codecheck': '0', 'cred': '90809890', 'msg': 'OK'}},
{'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}},
]
result = ['NaN' if not len(o['A']) else o['A'][0]['item'] for o in a_list]
print(result)
The output:
['NaN', 'pineapples', 'NaN']
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
列表推导提供了创建列表的简明方法。常见的应用是创建新的列表,其中每个元素是应用于另一个序列的每个成员或可迭代的一些操作的结果,或者创建满足特定条件的那些元素的子序列。
a certain condition, in your case, is 'NaN' if not len(o['A']) else o['A'][0]['item']
在你的情况下,某个条件是'NaN',如果不是len(o ['A']),否则o ['A'] [0] ['item']
https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
#1
1
Use the following approach(list comprehension):
使用以下方法(列表理解):
a_list = [{'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}},
{'A': [{'dictionary': 'True',
'item': 'pineapples',
'id': '13',
'score': '9.7899',
'rollup': {'True': 'OK', 'Fiz': 'Yes'},
'variant_list': [{'endp': '8', 'form': 'pineapple', 'register': '0'}]}],
'status': {'codecheck': '0', 'cred': '90809890', 'msg': 'OK'}},
{'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}},
]
result = ['NaN' if not len(o['A']) else o['A'][0]['item'] for o in a_list]
print(result)
The output:
['NaN', 'pineapples', 'NaN']
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
列表推导提供了创建列表的简明方法。常见的应用是创建新的列表,其中每个元素是应用于另一个序列的每个成员或可迭代的一些操作的结果,或者创建满足特定条件的那些元素的子序列。
a certain condition, in your case, is 'NaN' if not len(o['A']) else o['A'][0]['item']
在你的情况下,某个条件是'NaN',如果不是len(o ['A']),否则o ['A'] [0] ['item']
https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions