我有一个包含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
python方法是:
具有以下输出:
如果需要将最终数据存储在列表中,请用理解列表替换
for
循环:提供:
我们可以
enumerate
通过列表和子列表来获得索引,并使用max
来找到最大值。你知道吗下面将复制代码的结果:
或者,作为一个班轮的附属品(灵感来自@PatrickHaugh;仅Python3):
相关问题 更多 >
编程相关推荐