Python的列表推导式(list comprehension)

huangapple go评论60阅读模式
英文:

Python list of lists comprehension

问题

我有一个包含列表的列表V,如果将其转换为一个numpy数组,其形状为(9945,1024)。每个内部列表都包含一些值,我想创建一个包含值低于某个阈值的列表的列表。例如,如果我有:

a = [[1,3,4],[1,0,1],[0,5,1]]

并且我只想要小于2的值,我应该得到:

a = [[1],[1,0,1],[0,5,1]]

我该如何实现这个?

我首先尝试使用布尔值:

boolean_V = ([V[i] >= V_max for i in range(0,len(V))])

然后我做了:

V_new = V[boolean_V]

但我发现(一旦转换)得到的numpy数组形状为(3090669)。我该怎么办?

英文:

I have a list of lists V which, if converted in a numpy array, has a shape (9945,1024). Each list inside contain some value, and I want to create a list of lists which contains lists with values under a certain treshold. For instance, if I have:

a = [[1,3,4],[1,0,1],[0,5,1]]

and I want only values less than 2, I should have:

a = [[1],[1,0,1],[0,5,1]]

How can I get this?
I tried first with a boolean:

boolean_V = ([V[i] >= V_max for i in range (0,len(V))])

And then I did:

V_new = V[boolean_V]

But I find (once converted) a numpy array of shape (3090669). What can I do?

答案1

得分: 1

以下是翻译好的部分:

问题之一是我们有一个大小为 AxB 的数组。鉴于我们使用的是 numpy 数组,在筛选之后,我们仍然需要一个 AxB 数组。但是,如果将其转换为列表,我们可以得到不同大小的行。可以使用以下方式实现:

filter_list = [[val for val in row if val < 2] for row in a]

如果您仍然需要它是一个 numpy 数组,您可以将此列表转换为一个 numpy 数组,并指定 dtype=object 以适应不同的行大小。

filter_array = np.array(filter_list, dtype=object)

您还可以使用以下方式创建一个掩码,指示满足条件的位置:

mask = np.abs(test) < 2
print(mask)

这将返回一个类似尺寸的数组,指示每个索引处的值是否满足条件。

英文:

One of the problems is we have an AxB sized array. Given we are using a numpy array, after we filter, we still need to have an AxB array. However, if we turn this into a list, we can have different sized rows. This can be achieved with something like this:

filter_list = [[val for val in row if val &lt; 2] for row in a]

If you do still need it to be a numpy array, you can convert this list to a numpy array, and specify dtype=object for varied row sizes.

filter_array = np.array(filter_list, dtype=object)

You could also make a mask with something like this to indicate what positions meet the condition

mask = np.abs(test) &lt; 2

print(mask)

array([[ True, False, False],
   [ True,  True,  True],
   [ True, False,  True]])

Which will given back a similar dimensioned array telling if that val at each index met the condition

答案2

得分: 0

这段代码的中文翻译如下:

threshold = 2
b = [[j for j in i if j <= threshold] for i in a]
英文:

Does this work:

threshold = 2
b = [[j for j in i if j &lt;= threshold] for i in a]

答案3

得分: 0

如果您希望最终结果是一个可能长度不相等的列表,可以像这样做:

for row in range(V):
    V[row] = list(filter(lambda x : x <= V_max, V[row]))

这将遍历大列表内的每个列表,并仅保留低于设置的阈值的值。

英文:

If you want the final result to be a list of not necessarily equal lengths you can do something like this:

for row in range(V):
   V[row] = list(filter(lambda x : x &lt;= V_max, V[row]))

This would iterate through each list inside the big list and keep only the values below the set threshold.

huangapple
  • 本文由 发表于 2023年4月17日 04:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定