[英]list comprehension with multiple if statements
I have 4 lists and i want to have a final list with the maximum of each index of all lists and also to which list it belongs If it is from first list i mark 'a' ,and so on... But i have a problem with list comprehension.There is a syntax which i cannot find:我有 4 个列表,我想要一个最终列表,其中包含所有列表的每个索引的最大值以及它所属的列表如果它来自第一个列表,我标记为 'a',依此类推...但我有一个列表理解问题。有一个我找不到的语法:
import numpy as np
a=np.array([[1,200],[4,5]])
b=np.array([[11,33],[65,666]])
c=np.array([[1,2040],[54,522]])
d=np.array([[1,3],[685,222]])
x,y=a.shape
m=[]
for i in np.arange(x):
for j in np.arange(x):
maximum=max ( a[i][j] ,b[i][j],c[i][j] ,d[i][j])
m.append ((maximum,['a' if maximum in a else 'b' if maximum in b else 'c' if maximum in c else 'd' if maximum in d ] ))
where is my error in list comprehension??我的列表理解错误在哪里?
Here is a general rule for creating a list of comprehension这是创建理解列表的一般规则
[expression for item in iterable if condition == True]
In your case, you are totally missing iterable.在您的情况下,您完全缺少可迭代的。 Also, your conditions are not correct.另外,您的条件不正确。
I assume you want to get a list of max values for each column and the name of the list where this value is.我假设您想获取每列的最大值列表以及该值所在列表的名称。 In this case you could use that list of comprehension:在这种情况下,您可以使用该理解列表:
m.append((maximum,[name for lis, name in ((a, 'a'), (b, 'b'), (c, 'c'), (d, 'd')) if maximum in lis]))
NumPy arrays are much more than just nested lists. NumPy 数组不仅仅是嵌套列表。 For example, you can easily collect your four arrays into one 3d-array:例如,您可以轻松地将四个数组收集到一个 3d 数组中:
import numpy as np
a = np.array([[1,200], [4,5]])
b = np.array([[11,33], [65,666]])
c = np.array([[1,2040], [54,522]])
d = np.array([[1,3], [685,222]])
abcd = np.stack([a, b, c, d])
abcd.shape
(4, 2, 2)
Now the maximum for each index pair over the original arrays can be obtained very conveniently:现在可以非常方便地获得原始数组上每个索引对的最大值:
np.max(abcd, axis=0)
array([[ 11, 2040],
[ 685, 666]])
To get the index number for which list each maximum number came from, you can use np.argmax
:要获取每个最大数字来自哪个列表的索引号,您可以使用np.argmax
:
np.argmax(abcd, axis=0)
array([[1, 2],
[3, 1]], dtype=int64)
Here, 1
means b
, 2
means c
, etc. because we stacked a, b, c, d
in that order.在这里, 1
表示b
, 2
表示c
,等等,因为我们按顺序堆叠a, b, c, d
。
I'd just stack he arrays first, and use numpy methods:我只是先堆叠他的数组,然后使用 numpy 方法:
import numpy as np
a = np.array([[1,200],[4,5]])
b = np.array([[11,33],[65,666]])
c = np.array([[1,2040],[54,522]])
d = np.array([[1,3],[685,222]])
s = np.stack((a,b,c,d))
Now you can easily find the index of the maximum (so 0 => a, 1 => b, etc):现在您可以轻松找到最大值的索引(因此 0 => a、1 => b 等):
max_value = s.max(axis=0)
argmax_value = s.argmax(axis=0)
And you can even stack them together:您甚至可以将它们堆叠在一起:
result = np.dstack((max_value, argmax_value)).reshape(-1, 2)
# array([[ 11, 1],
# [2040, 2],
# [ 685, 3],
# [ 666, 1]], dtype=int64)
If you really need the letters in your list, you can now map the second column to your letters:如果您确实需要列表中的字母,现在可以将第二列映射到您的字母:
# should not be too many arrays... ;-)
mapper = {i: chr(ord('a') + i) for i in range(s.shape[0])}
result_list = [(x, mapper[y]) for x, y in result]
But generally, if your logic needs to know the name of a variable, the design is wrong.但是一般来说,如果你的逻辑需要知道一个变量的名字,那么设计就是错误的。 You should probably use a sequence container then (list, array, etc), like the stacked array in the first code box.您可能应该使用序列容器(列表、数组等),例如第一个代码框中的堆叠数组。
Just stack them after one another:只需将它们一个一个地堆叠起来:
[i for i in range(100) if i > 10 if i < 50]
Produces the integers between 11 and 49, inclusive.生成 11 到 49(含)之间的整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.