就是如果一个数组有多个最大值,要求返回多个最大值位置怎么做。
x=(2。1;5,5:2)
print(which.max(x))
这样结果只能返回第一个的
class which():
def __init__(self, lst):
self.lst = lst
def max(self):
res = []
t = max(self)
for i,n in enumerate(self):
if n==t:
res.append(str(i))
res = ' '.join(res)
return f'[{t}] {res}'
def min(self):
res = []
t = min(self)
for i,n in enumerate(self):
if n==t:
res.append(str(i))
res = ' '.join(res)
return f'[{t}] {res}'
a = [1,2,5,2,1,5]
print(which.max(a))
#[5] 2 5
print(which.min(a))
#[1] 0 4
你用的是Python吧,好像并没有返回多个最大值的位置的函数。
建议这种情况自己写一个函数。
最简单的就是,不是已经求出最大值了嘛。
循环数组把和最大值相同的元素放到列表中,然后返回。