因此,当将随机数字列表输入到函数中时,我想将第一个元素与最大元素进行切换。然后返回新列表。
def jumpMaximum(numList):
max2=max(numList)
a,b = int(numList[-len(numList)]),max
numList[a],numList[b] = numList[b],numList[a]
return numList
我似乎无法弄清楚是什么导致此失败并且不返回列表。我需要它做的就是取出最大的数字并将其与第一个数字交换。
最佳答案
你可以试试这个:
index_max = a.index(max(a))
a[index_max], a[0] = a[0], a[index_max]
关于python - 将第一个元素与列表中的最大元素交换-Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19622174/