【发布时间】:2021-06-08 06:00:51
【问题描述】:
records = [["chi", 20.0],["beta", 50.0],["alpha", 50.0]]
a = len(records)
i = 0
b = []
while i < a:
print(records[i][1])
b.append(records[i][1])
i = i + 1
print(b)
c = len(b)
#from previous question
unique_list = []
for el in b:
if el not in unique_list:
unique_list.append(el)
else:
print ("Element already in the list")
print(unique_list)
second_lowest_score = unique_list[1]
print(second_lowest_score)
names_list = []
g = 0
while g < a:
if records[g][1] == second_lowest_score:
names_list.append(records[g][0])
g = g + 1
print(names_list)
我想要做的是将具有第二低分数 (50) 的记录的名称附加到 names_list。但是,while 循环没有给我任何结果。没有语法错误,所以我不确定我的代码为什么错误。但是,当我使用前面的 while 循环附加数字时,它似乎工作正常。是不是不能在while循环中使用if语句?
【问题讨论】:
标签: python-3.x list if-statement while-loop append