根据 python 中的另一个列表查找最大值的索引
Finding the index of max value based on another list in python
有人可以帮我处理这部分代码吗?我不知道应该如何编写以下代码的注释部分。
p = [3, 1, 2, 4, 5]
ed = [(1,3), (1,4), (2,5), (3,2)]
for (i,j) in ed:
if i=1:
# the j for i= 1 would be 3 and 4 since I have (1,3) and (1,4) in "ed".
# I want to chose maximum between j, so in this case the maximum is 4.
# then considering list "p" I want to know the index of the j (here is 4) in the list "p"
#so the expected outcome must be 3 since the index of 4 in the list p is 3.
这里是你如何做到的:
p = [3, 1, 2, 4, 5]
ed = [(1,3), (1,4), (2,5), (3,2)]
i = 1
print(p.index(max([num[1] for num in ed if num[0] == i])))
输出:
>>>
3
请注意,如果在 ed 中找不到元组或在 p 中找不到数字,这将导致错误。所以你可能想处理这些情况
有人可以帮我处理这部分代码吗?我不知道应该如何编写以下代码的注释部分。
p = [3, 1, 2, 4, 5]
ed = [(1,3), (1,4), (2,5), (3,2)]
for (i,j) in ed:
if i=1:
# the j for i= 1 would be 3 and 4 since I have (1,3) and (1,4) in "ed".
# I want to chose maximum between j, so in this case the maximum is 4.
# then considering list "p" I want to know the index of the j (here is 4) in the list "p"
#so the expected outcome must be 3 since the index of 4 in the list p is 3.
这里是你如何做到的:
p = [3, 1, 2, 4, 5]
ed = [(1,3), (1,4), (2,5), (3,2)]
i = 1
print(p.index(max([num[1] for num in ed if num[0] == i])))
输出:
>>>
3
请注意,如果在 ed 中找不到元组或在 p 中找不到数字,这将导致错误。所以你可能想处理这些情况