好的,所以我有一点问题,对不起,我是 python 新手。
我正在尝试按分数对列表进行排序,这是一个数字,但如果出现平局,我需要按名称的长度对它们进行排序,名称越短排名越高。
所以我在这里有这个清单
11 Jenny
8 Adam
10 Mark
8 Ada
当我在这里使用此代码时,它会返回
11 Jenny
10 Mark
10 Mark
10 Mark
def sort(names, counts):
newArr = []
newNames = names
newCount = counts
for x in range(0, len(names)):
newArr.append(findBiggest(newNames, newCount))
for z in range(0, len(names)):
name = newArr[len(newArr) - 1].split(" ")[1]
print name
if names[z] == name:
tempArr1 = newNames
tempArr2 = newCount
newNames = []
newCount = []
for y in range(0, len(tempArr1)):
if y != z:
newNames.append(tempArr1[y])
newCount.append(tempArr2[y])
return newArr
def findBiggest(names, counts):
biggest = 0;
for x in range(0, len(counts)):
if int(counts[x]) > biggest:
biggest = int(counts[x])
biggestCountArr = [[], []]
for x in range(0, len(counts)):
if int(counts[x]) == biggest:
biggestCountArr[0].append(counts[x])
biggestCountArr[1].append(names[x])
if len(biggestCountArr[0]) == 1:
return str(biggestCountArr[0][0]) + " " + biggestCountArr[1][0]
else:
return smallestLength(biggestCountArr)
def smallestLength(twoDArr):
names = twoDArr[1]
shortestLen = 0
for x in range(0, len(names)):
if len(names[x]) > shortestLen:
shortestlen = len(names[x])
for x in range(0, len(names)):
if len(names[x]) == shortestLen:
return str(twoDArr[0][x]) + " " + twoDArr[1][x]
只是让你知道
11 Jenny
8 Adam
10 Mark
8 Ada
应该出来
11 Jenny
10 Mark
8 Ada
8 Adam