[英]List of list of dicts values to list in python
The order of the resulted list is irrelevant, I need to add the elements afterwards.结果列表的顺序无关紧要,我需要在之后添加元素。
Input1: [[{'a': 57}], 87]
Expect Output1: [57,87]
Input2: [[{'a': 57, 'b':12}], 87]
Expect Output2: [57,12,87]
Input3: [[{'a': 57}], 12, [{'a':34}], 87]
Expect Output3: [57,12,34,87]
That's Pretty Simple all you Need is to just check the Type of the object这很简单,您只需要检查 object 的类型
Then If it's a list get the first index of it which is a dict然后,如果它是一个列表,则获取它的第一个索引,它是一个字典
So for example if i have [{1,2,3}]
It will convert it to Just {1,2,3}
因此,例如,如果我有[{1,2,3}]
它会将其转换为 Just {1,2,3}
Then Looping Through The values (not the keys)
in the Dict and just adds it to a list然后循环遍历 Dict 中的值(not the keys)
并将其添加到列表中
Whereas If it's not a list then just add it to the list而如果它不是列表,则只需将其添加到列表中
Input3 = [[{'a': 57}], 12, [{'a':34}], 87]
li = []
for i in Input3:
if type(i) == list:
for item in i[0].values():
li.append(item)
else:
li.append(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.