Value error: list.remove(x) x not in list
Value error: list.remove(x) x not in list
我必须找出最低分数,然后将其从列表中删除。这是我的代码:
n = int(input())
list = []
for i in range(n+1):
name = input()
score = float(input())
list.append([name,score])
print(list)
min_scores = list[0][1]
for x in list:
if (x[1]<float(min_scores)):
min_scores = x[1]
list.remove(min_scores)
print(list)
读取错误消息
ValueError Traceback (most recent call last)
<ipython-input-135-44e6d871c75e> in <module>
11 if (x[1]<float(min_scores)):
12 min_scores = x[1]
---> 13 list.remove(min_scores)
14 print(list)
ValueError: list.remove(x): x not in list
您犯了严重错误,切勿将关键字用作变量名。
希望这段代码对您有所帮助。
n = int(input())
lst = [] # never use a keyword as a variable name
for i in range(n+1):
name = input()
score = float(input())
lst.append([name,score])
print(lst)
min_score = []
for x, y in lst: # use two variables because lst is a 2 dimential array
min_score.append(y) # adding y into min_score list
min_score.sort() # sorting min_score
del(min_score[0]) # after sorting first value of array will be minimum, so remove it.
print(min_score)
n = int(input())
listOfScores = []
for i in range(n + 1):
name = input()
score = float(input())
listOfScores.append([name, score])
print(listOfScores)
minScoreList = listOfScores[0]
for x in listOfScores:
if x[1] < float(minScoreList[1]):
minScoreList = x
listOfScores.remove(minScoreList)
print(listOfScores)
老实说,这在程序员社区中被称为垃圾代码。如果你需要算法
找出最低分数,然后将其从列表中删除。然后这样做
scores_list = []
def get_scores():
print('Type "stop" to stop the program')
x = input('Enter score:')
if x.isdigit():
scores_list.append(x)
if x == 'stop':
return print('Minimum score is:', min(scores_list))
get_scores()
get_scores()
并注意:切勿在变量名中进一步使用关键字,这可能会导致您的程序无法正常工作
只需将内置 min
函数与适当的键函数一起使用,并将其作为参数传递给 remove
。
n = int(input())
lst = []
for i in range(n+1):
name = input()
score = float(input())
lst.append([name, score])
lst.remove(min(lst, key=lambda t: t[1]))
也不要使用内置类型作为变量名(列表不是其他答案所说的关键字)。
我必须找出最低分数,然后将其从列表中删除。这是我的代码:
n = int(input())
list = []
for i in range(n+1):
name = input()
score = float(input())
list.append([name,score])
print(list)
min_scores = list[0][1]
for x in list:
if (x[1]<float(min_scores)):
min_scores = x[1]
list.remove(min_scores)
print(list)
读取错误消息
ValueError Traceback (most recent call last)
<ipython-input-135-44e6d871c75e> in <module>
11 if (x[1]<float(min_scores)):
12 min_scores = x[1]
---> 13 list.remove(min_scores)
14 print(list)
ValueError: list.remove(x): x not in list
您犯了严重错误,切勿将关键字用作变量名。 希望这段代码对您有所帮助。
n = int(input())
lst = [] # never use a keyword as a variable name
for i in range(n+1):
name = input()
score = float(input())
lst.append([name,score])
print(lst)
min_score = []
for x, y in lst: # use two variables because lst is a 2 dimential array
min_score.append(y) # adding y into min_score list
min_score.sort() # sorting min_score
del(min_score[0]) # after sorting first value of array will be minimum, so remove it.
print(min_score)
n = int(input())
listOfScores = []
for i in range(n + 1):
name = input()
score = float(input())
listOfScores.append([name, score])
print(listOfScores)
minScoreList = listOfScores[0]
for x in listOfScores:
if x[1] < float(minScoreList[1]):
minScoreList = x
listOfScores.remove(minScoreList)
print(listOfScores)
老实说,这在程序员社区中被称为垃圾代码。如果你需要算法
找出最低分数,然后将其从列表中删除。然后这样做
scores_list = []
def get_scores():
print('Type "stop" to stop the program')
x = input('Enter score:')
if x.isdigit():
scores_list.append(x)
if x == 'stop':
return print('Minimum score is:', min(scores_list))
get_scores()
get_scores()
并注意:切勿在变量名中进一步使用关键字,这可能会导致您的程序无法正常工作
只需将内置 min
函数与适当的键函数一起使用,并将其作为参数传递给 remove
。
n = int(input())
lst = []
for i in range(n+1):
name = input()
score = float(input())
lst.append([name, score])
lst.remove(min(lst, key=lambda t: t[1]))
也不要使用内置类型作为变量名(列表不是其他答案所说的关键字)。