【发布时间】:2020-08-01 20:49:25
【问题描述】:
我得到了一个列表,其中包含另一个列表形式的一些学生的姓名和分数。
例如:lis=[['barry',35],['larry',20],['cathy',10],['mathew',10]]
我需要找到分数第二低的学生的姓名。对于上述输入,答案将是:cathey 马修
我尝试通过以下方式解决它。但是没有输出。 谁能找出我的代码中的错误?
x=int(input()) #total number of element
lis=[] #list
pis=[]
for _ in range(x) :
name = input()
score = float(input())
lis.append([name,score]) #appending name and score to lis
pis.append([score]) #appending only scores to pis
pis.sort() #sorting pis
lis.sort() #sorting lis
x=pis[1] #finding the 2nd smallest score in pis
for i in lis:
if ((i[1])) == x: #comparing the second smallest number to every score in lis
print (i[0]) #printing only the name if the scores are same,(this step don't work)
【问题讨论】:
-
"pis" 也是一个列表列表(不必要)。所以“pis[1]”是一个列表。
标签: python python-3.x list logic out-of-memory