从没有替换的列表中获取最大值和x,y坐标

2024-10-04 05:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含6个列表的列表,每个列表包含6个浮动。我想从整个集合中找到最大值,记录它的x和y值(因为缺少更好的术语…就好像我在考虑图中的列表一样),然后从考虑中删除该列表,这样外部列表中的每个元素只能选择一次。你知道吗

我想出了以下方法来做这件事,这是有效的。但我担心这是非常不雅观,可能有一个更好的方法来处理这件事。你知道吗

原始列表

[[5.468295520651542, 37.0564281559046, 43.455497382198956, 3.781268179173938, 0.11634671320535195, 10.122164048865619],
 [7.3991031390134525, 48.87892376681614, 26.00896860986547, 4.708520179372197, 5.829596412556054, 7.174887892376682],
 [30.382775119617225, 2.6315789473684212, 0.7177033492822966, 64.5933014354067, 1.4354066985645932, 0.23923444976076555],
 [5.112474437627812, 2.8629856850715747, 6.952965235173824, 3.476482617586912, 76.27811860940696, 5.316973415132924],
 [85.98979013045944, 6.693136698808849, 3.5734543391945546, 1.5314804310833805, 1.1344299489506522, 1.0777084515031197],
 [5.565529622980251, 65.1705565529623, 2.5733093955715143, 5.326152004787552, 11.789347695990426, 9.57510472770796]]

我的代码

maxmaxlist=[]
maxxlist=[]
maxylist=[]
for i in range(0,6):
    maxlist=[]
    xlist=[]
    for j in range(0,len(templist)):
        temp = (max(templist[j]))
        maxlist.append(temp)
        xlist.append(templist[j].index(temp))

    temp = max(maxlist)
    maxmaxlist.append(temp)

    temp2 = maxlist.index(temp)
    maxylist.append((templist[temp2]).index(temp))
    maxxlist.append(temp2)
    templist[temp2] = [0]
print maxmaxlist, maxxlist, maxylist

Tags: 方法in列表forindexrangetempappend
3条回答

python方法是:

lst = [[5.468295520651542, 37.0564281559046, 43.455497382198956, 3.781268179173938, 0.11634671320535195, 10.122164048865619], [7.3991031390134525, 48.87892376681614, 26.00896860986547, 4.708520179372197, 5.829596412556054, 7.174887892376682], [30.382775119617225, 2.6315789473684212, 0.7177033492822966, 64.5933014354067, 1.4354066985645932, 0.23923444976076555], [5.112474437627812, 2.8629856850715747, 6.952965235173824, 3.476482617586912, 76.27811860940696, 5.316973415132924], [85.98979013045944, 6.693136698808849, 3.5734543391945546, 1.5314804310833805, 1.1344299489506522, 1.0777084515031197], [5.565529622980251, 65.1705565529623, 2.5733093955715143, 5.326152004787552, 11.789347695990426, 9.57510472770796]]

for m, x in sorted(((max(l), x) for x, l in enumerate(lst)), reverse=True):
  m, y = max((val, y) for y, val in enumerate(lst[x]))
  print(x, y, '-->', m)

具有以下输出:

4 0 --> 85.98979013045944
3 4 --> 76.27811860940696
5 1 --> 65.1705565529623
2 3 --> 64.5933014354067
1 1 --> 48.87892376681614
0 2 --> 43.455497382198956

如果需要将最终数据存储在列表中,请用理解列表替换for循环:

m_x_y = [(m, x, max((val, y) for y,val in enumerate(lst[x]))[1]) for m, x in
         sorted(((max(l), x) for x,l in enumerate(lst)), reverse=True)]
print(m_x_y)

提供:

[(85.98979013045944, 4, 0), (76.27811860940696, 3, 4),
 (65.1705565529623,  5, 1), (64.5933014354067,  2, 3),
 (48.87892376681614, 1, 1), (43.45549738219896, 0, 2)]

我们可以enumerate通过列表和子列表来获得索引,并使用max来找到最大值。你知道吗

l = [[5.468295520651542, 37.0564281559046, 43.455497382198956, 3.781268179173938, 0.11634671320535195, 10.122164048865619], [7.3991031390134525, 48.87892376681614, 26.00896860986547, 4.708520179372197, 5.829596412556054, 7.174887892376682], [30.382775119617225, 2.6315789473684212, 0.7177033492822966, 64.5933014354067, 1.4354066985645932, 0.23923444976076555], [5.112474437627812, 2.8629856850715747, 6.952965235173824, 3.476482617586912, 76.27811860940696, 5.316973415132924], [85.98979013045944, 6.693136698808849, 3.5734543391945546, 1.5314804310833805, 1.1344299489506522, 1.0777084515031197], [5.565529622980251, 65.1705565529623, 2.5733093955715143, 5.326152004787552, 11.789347695990426, 9.57510472770796]]

maximum, max_x, max_y = max((n, x, y) 
                            for x, subl in enumerate(l) 
                            for y, n in enumerate(subl))

# 85.98979013045944, 4, 0

下面将复制代码的结果:

LL = [[5.468295520651542, 37.0564281559046, 43.455497382198956, 3.781268179173938, 0.11634671320535195, 10.122164048865619],
 [7.3991031390134525, 48.87892376681614, 26.00896860986547, 4.708520179372197, 5.829596412556054, 7.174887892376682],
 [30.382775119617225, 2.6315789473684212, 0.7177033492822966, 64.5933014354067, 1.4354066985645932, 0.23923444976076555],
 [5.112474437627812, 2.8629856850715747, 6.952965235173824, 3.476482617586912, 76.27811860940696, 5.316973415132924],
 [85.98979013045944, 6.693136698808849, 3.5734543391945546, 1.5314804310833805, 1.1344299489506522, 1.0777084515031197],
 [5.565529622980251, 65.1705565529623, 2.5733093955715143, 5.326152004787552, 11.789347695990426, 9.57510472770796]]


# find max index of each sublist
mxy = [max(range(len(L)), key=L.__getitem__) for L in LL]
# and the corresponding maxima
mx = [L[y] for L, y in zip(LL, mxy)]
# indirectly (i.e. by index) sort maxima in descending order
mxx = sorted(range(len(LL)), key=mx.__getitem__, reverse=True)
# use index to rearrange mxy and mx
mxy = [mxy[x] for x in mxx]
mx = [mx[x] for x in mxx]

mx
# [85.98979013045944, 76.27811860940696, 65.1705565529623, 64.5933014354067, 48.87892376681614, 43.455497382198956]
mxy
# [0, 4, 1, 3, 1, 2]
mxx
# [4, 3, 5, 2, 1, 0]

或者,作为一个班轮的附属品(灵感来自@PatrickHaugh;仅Python3):

from itertools import count, repeat

mx, mxy, mxx = zip(*sorted(map(max, map(zip, LL, map(count, repeat(0)), map(repeat, count()))), reverse=True))

相关问题 更多 >