[英]How to do this specific list comprehension in Python?
points = [
[[x,y], [x,y], [x,y]],
[[x,y], [x,y], [x,y], [x,y], [x,y]],
[[x,y]]
]
weights = [1, 2, 3]
output = [
[[x,y,1], [x,y,1], [x,y,1]],
[[x,y,2], [x,y,2], [x,y,2], [x,y,2], [x,y,2]],
[[x,y,3]]
]
I want to combine Points and Weights to ultimately look like the output column.我想结合点和权重最终看起来像 output 列。 The length of points and weights will always be the same.
点和权重的长度将始终相同。 However, the amount of [x,y] pairs will differ for each list inside the list.
但是,列表中每个列表的 [x,y] 对的数量会有所不同。 Exactly how I have shown the example.
正是我展示了这个例子的方式。 Python is the language I am using.
Python 是我使用的语言。
Any help would be greatly appreciated任何帮助将不胜感激
Using zip
it's relatively easy:使用
zip
相对容易:
[[p+[w] for p in pl] for pl, w in zip(points, weights)]
Work from the inside out.从内到外工作。
On the inside, we have [[x, y], [x, y], [x, y]]
and 1
, and we want to produce [[x, y, 1], [x, y, 1], [x, y, 1]]
.在里面,我们有
[[x, y], [x, y], [x, y]]
和1
,我们要产生[[x, y, 1], [x, y, 1], [x, y, 1]]
。
So, we are appending 1
to each of the [x, y]
values.因此,我们将
1
附加到每个[x, y]
值。 But .append
modifies a value in-place and returns None
;但是
.append
修改了一个值并返回None
; to do work with list-comprehensions, we want to return new values.要使用列表理解,我们希望返回新值。 So we can instead create
[1]
from the input 1
, and concatenate lists with +
.所以我们可以改为从输入
1
创建[1]
,并用+
连接列表。
Therefore, we want the given inner item
, +
an appended list [1]
, for
each of the item
s that is found in
our original data ( [[x, y], [x, y], [x, y]]
).因此,我们想要给定的内部
item
+
一个附加列表[1]
, for
in
我们的原始数据中找到的每个item
s ( [[x, y], [x, y], [x, y]]
)。 Since we are going to apply this to nested lists, let's say that we refer to that list as a row
.由于我们要将它应用于嵌套列表,假设我们将该列表称为
row
。 Then, we write the list comprehension, describing that task - exactly as I put it, reading left to right: [item + [1] for item in row]
.然后,我们编写列表理解,描述该任务 - 正如我所说的那样,从左到右阅读:
[item + [1] for item in row]
。
(Edit: as noted in the other answer, it also works to take advantage of unpacking: instead of item + [1]
, we can do [*item, 1]
.) (编辑:如另一个答案中所述,它也可以利用解包:而不是
item + [1]
,我们可以做[*item, 1]
。)
Now: that is the sort of processing that we want to do to each row
.现在:这就是我们想要对每一
row
进行的处理。 But we want to use a different value instead of the 1
each time.但是我们希望每次都使用不同的值而不是
1
。 Those values come from weights
, which we want to iterate in parallel with the row
s of the overall points
.这些值来自
weights
,我们希望与整体points
的row
s 并行迭代。
So, we need to use zip
in order to pair up each weight
of the weights
with a corresponding row
from points
.因此,我们需要使用
zip
来将每个weight
weights
与points
中的相应row
配对。 That looks like zip(points, weights)
, and when we iterate over that, we get (row, weight)
pairs.这看起来像
zip(points, weights)
,当我们迭代它时,我们得到(row, weight)
对。
Our processing for a given row
now looks like [item + [weight] for item in row]
, after modifying it to account for the varying weight
.我们对给定
row
的处理现在看起来像[item + [weight] for item in row]
,经过修改以考虑不同的weight
。
So we apply the technique again: we want to do [item + [weight] for item in row]
, for
each of the (row, weight)
pairs (parentheses are not strictly necessary here, but make things a lot easier to understand) that are found in
our zip
ped lists of points
and weights
.所以我们再次应用该技术:我们想要
for
每个(row, weight)
对执行[item + [weight] for item in row]
(括号在这里不是绝对必要的,但让事情更容易理解)可以in
我们的zip
ped points
和weights
列表中找到。 And again, we read that left to right:再一次,我们从左到右阅读:
[[item + [weight] for item in row] for (row, weight) in zip(points, weights)]
Try this one-liner list comprehension without using a zip -在不使用 zip 的情况下尝试这种单行列表理解 -
[[k+[weights[i]] for k in j] for i,j in enumerate(points)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.