我有一个包含六个列表的列表,每个列表包含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
答案 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
答案 1 :(得分:1)
以下内容将再现您的代码结果:
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]
或者,对于单行的afficionados(灵感来自@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))
答案 2 :(得分:0)
pythonic方法将是:
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)]
答案 3 :(得分:0)
给定输入
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]]
...:
声明
In [2]: [ (x,*reversed(max([ (m, y) for (y,m) in enumerate(l)]))) for (x,l) in enumerate(L) ]
产生
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)]