3 回答
TA贡献1775条经验 获得超11个赞
其他选择:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxs = [ max(s) for s in l ] #=> [7, 10, 3]
max_idxs = [ s.index(max(s)) for s in l ] #=> [2, 1, 1]
mins = [ min(s) for s in l ] #=> [5, 6, 1]
mins_idxs = [ s.index(min(s)) for s in l ] #=> [0, 0, 2]
或者您可以将结果存储到一个 dict 数组中,一个班轮:
mapp = map(lambda x: {'max': max(x), 'max_idxs': x.index(max(x)), 'min': min(x), 'min_idxs': x.index(min(x)) }, l)
for k in mapp:
print(k)
#=> {'max': 7, 'max_idxs': 2, 'min': 5, 'min_idxs': 0}
#=> {'max': 10, 'max_idxs': 1, 'min': 6, 'min_idxs': 0}
#=> {'max': 3, 'max_idxs': 1, 'min': 1, 'min_idxs': 2}
TA贡献1829条经验 获得超7个赞
您可以使用以下单行:
l=[[5,6,7],[6,10,9,6],[2,3,1]]
maxL, index = zip(*[(max(subList), subList.index(max(subList))) for subList in l])
print(maxL) # will output (7, 10, 3)
print(index) # will output (2, 1, 1)
添加回答
举报