所以我试图在列表中找到第三大数字/字符串。这是我到目前为止的代码
def big(inputString):
big1 = inputString[0]
big2 = inputString[0]
big3 = inputString[0]
for char in inputString:
if char > big1:
big1, big2,big3 = char,big1,big2
elif big1 > char > big2 > big3:
big3 = char
print('largest',big1,'second largest',big2,third largest,big3)
当用户输入字符串列表时,输出应该如下所示:
big('abxztu')
largest z second largest x third largest u
但是我得到的输出是
largest z second largest x third largest b
谁能告诉我我的代码哪里出了问题
最佳答案
你可以简单地这样做:
def big(inputString):
l = sorted(list(inputString)) #Turning the input into a list and sorting it
l = l[::-1] #Reversing the list
print(l[0],l[1],l[2]) #Printing out the first three element of the list
这是一个测试运行的例子:
>>> big("abc")
c b a
编辑 1
另一个测试运行的例子:
>>> big("helloworld")
w r o
关于python - 如何在列表中找到最大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42202788/