将列表内容转换为可理解的命名元素
Convert List Contents To Named Elements Within Comprehension
我什至难以描述这里发生了什么,但这段代码有效:
list_of_lists = [
[1.1, 1.2],
[2.1, 2.2]
]
for (first, second) in list_of_lists:
print("%s %s" % (first, second))
# output:
# 1.1 1.2
# 2.1 2.2
其中 list_of_lists 的每个内部列表都将元素转换为变量名称“first”和“second”。
这个命名列表内容的过程叫什么?
此外,如果我想将结果转换为等效于以下内容的对象:
[
{
"first": 1.1,
"second": 1.2
},
{
"first": 2.1,
"second": 2.2
}
]
我怎样才能在列表理解中做到这一点?我正在尝试这样的事情,但我正在努力寻找语法来表达我正在尝试做的事情,特别是关于 ???:
results = [??? for (first, second) in list_of_lists]
我知道我可以做更详细的事情:
results = [{"first": l[0], "second": l[1]} for l in list_of_lists]
...但我想以一种更简洁的形式来完成,即单独使用名称而不是列表项索引。
在迭代时从 list_of_lists 解压元组。
results = [{"first": first, "second": second} for first, second in list_of_lists]
这个过程叫做Unpacking argument list。
例如:
a, b = [1, 2]
则a
等于1
,b
等于2
在你的代码行
for (first, second) in list_of_lists
主列表 list_of_lists
中的每个 sub-list
解包 为两个值。
例如在循环的第一次迭代中,first
将等于 1.1
而 second
将等于 2.2
对第二个列表应用相同的原则:
li = [
{
"first": 1.1,
"second": 1.2
},
{
"first": 2.1,
"second": 2.2
}
]
result = [(item['first'], item['second']) for item in li]
print(result) # [(1.1, 1.2), (2.1, 2.2)]
# OR, TO EXTRACT DICTIONARY (as pointed by Sarol)
results = [{"first": first, "second": second} for first, second in list_of_lists]
print(results) # [{'first': 1.1, 'second': 1.2}, {'first': 2.1, 'second': 2.2}]
dict()
函数通过获取由 key:value
对组成的可迭代对象来创建一个字典,并且 x.items()
解包 一个字典 x
到它的 key
和 value
.
您的具体问题需要首先 packing
列表的每个项目,然后 unpacking
它。因此,使用 list-comprehension
,遍历列表,使用 items()
函数获取每个项目的 key-value
对,然后使用 dict()
方法从中创建字典。所以:
#OR, EVEN EASILY
result = [dict(item.items()) for item in li]
print(result) # [{'first': 1.1, 'second': 1.2}, {'first': 2.1, 'second': 2.2}]
我什至难以描述这里发生了什么,但这段代码有效:
list_of_lists = [
[1.1, 1.2],
[2.1, 2.2]
]
for (first, second) in list_of_lists:
print("%s %s" % (first, second))
# output:
# 1.1 1.2
# 2.1 2.2
其中 list_of_lists 的每个内部列表都将元素转换为变量名称“first”和“second”。
这个命名列表内容的过程叫什么?
此外,如果我想将结果转换为等效于以下内容的对象:
[
{
"first": 1.1,
"second": 1.2
},
{
"first": 2.1,
"second": 2.2
}
]
我怎样才能在列表理解中做到这一点?我正在尝试这样的事情,但我正在努力寻找语法来表达我正在尝试做的事情,特别是关于 ???:
results = [??? for (first, second) in list_of_lists]
我知道我可以做更详细的事情:
results = [{"first": l[0], "second": l[1]} for l in list_of_lists]
...但我想以一种更简洁的形式来完成,即单独使用名称而不是列表项索引。
在迭代时从 list_of_lists 解压元组。
results = [{"first": first, "second": second} for first, second in list_of_lists]
这个过程叫做Unpacking argument list。
例如:
a, b = [1, 2]
则a
等于1
,b
等于2
在你的代码行
for (first, second) in list_of_lists
主列表 list_of_lists
中的每个 sub-list
解包 为两个值。
例如在循环的第一次迭代中,first
将等于 1.1
而 second
将等于 2.2
对第二个列表应用相同的原则:
li = [
{
"first": 1.1,
"second": 1.2
},
{
"first": 2.1,
"second": 2.2
}
]
result = [(item['first'], item['second']) for item in li]
print(result) # [(1.1, 1.2), (2.1, 2.2)]
# OR, TO EXTRACT DICTIONARY (as pointed by Sarol)
results = [{"first": first, "second": second} for first, second in list_of_lists]
print(results) # [{'first': 1.1, 'second': 1.2}, {'first': 2.1, 'second': 2.2}]
dict()
函数通过获取由 key:value
对组成的可迭代对象来创建一个字典,并且 x.items()
解包 一个字典 x
到它的 key
和 value
.
您的具体问题需要首先 packing
列表的每个项目,然后 unpacking
它。因此,使用 list-comprehension
,遍历列表,使用 items()
函数获取每个项目的 key-value
对,然后使用 dict()
方法从中创建字典。所以:
#OR, EVEN EASILY
result = [dict(item.items()) for item in li]
print(result) # [{'first': 1.1, 'second': 1.2}, {'first': 2.1, 'second': 2.2}]