J'ai une liste qui contient six listes qui contiennent chacune 6 flotteurs. Je veux trouver la valeur maximale de l'ensemble global, enregistrer ses valeurs x et y (faute d'un meilleur terme ... comme si je considérais la liste de la liste d'un graphique), puis retirer cette liste de la considération afin que chaque L'élément de la liste externe ne peut être choisi qu'une seule fois.

J'ai trouvé la méthode suivante pour le faire, qui fonctionne. Mais je crains que ce ne soit terriblement inélégant et qu'il existe probablement une bien meilleure façon de procéder.

Liste originale

[[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]]

Mon code

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
-1
kupe 13 avril 2018 à 01:30

4 réponses

Meilleure réponse

Une approche pythonique serait:

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)

Avec la sortie suivante:

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

Si vous avez besoin de stocker vos données finales dans une liste, remplacez la boucle for par une liste de compréhension:

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)

Donne:

[(85.98979013045944, 4, 0), (76.27811860940696, 3, 4),
 (65.1705565529623,  5, 1), (64.5933014354067,  2, 3),
 (48.87892376681614, 1, 1), (43.45549738219896, 0, 2)]
0
sciroccorics 12 avril 2018 à 23:44

Nous pouvons enumerate parcourir les listes et sous-listes pour obtenir les indices, et utiliser max pour trouver le maximum.

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
2
Patrick Haugh 12 avril 2018 à 22:41

Contribution donnée

In [1]: 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]]
    ...: 

Déclaration

In [2]: [ (x,*reversed(max([ (m, y) for (y,m) in enumerate(l)]))) for (x,l) in enumerate(L) ]

Produit

Out[2]: 
[(0, 2, 43.455497382198956),
 (1, 1, 48.87892376681614),
 (2, 3, 64.5933014354067),
 (3, 4, 76.27811860940696),
 (4, 0, 85.98979013045944),
 (5, 1, 65.1705565529623)]
0
Setop 13 avril 2018 à 09:22

Ce qui suit reproduit le résultat de votre code:

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]

Ou, pour les amateurs du one-liner (inspiré par @PatrickHaugh; Python3 uniquement):

from itertools import count, repeat

mx, mxy, mxx = zip(*sorted(map(max, map(zip, LL, map(count, repeat(0)), map(repeat, count()))), reverse=True))
1
Paul Panzer 13 avril 2018 à 00:05