无法通过引用变量删除列表元素
Cannot delete list element by referring to variable
我需要从列表 'to_do" 中删除字典中具有最小值的元素。变量 'letter' = 's' 并且编译器说列表 [= 中没有这样的元素15=]。我错过了什么?
to_do = ['s','a','b','c','d','e','f','g','h','z','w']
done = []
dict = {}
dict ['s'] = [0, ]
dict ['a'] = [999, []]
dict ['b'] = [999, []]
dict ['c'] = [999, []]
dict ['d'] = [999, []]
dict ['e'] = [999, []]
dict ['f'] = [999, []]
dict ['g'] = [999, []]
dict ['h'] = [999, []]
dict ['z'] = [999, []]
dict ['w'] = [999, []]
while to_do:
value = 999
count = 0
for element in to_do:
var = dict[element]
if var[0] < value:
value = var[0]
letter = element
#print(letter)
index = to_do.index(letter)
print(value)
to_do.pop(to_do.index(letter))
你的代码中的错误是字母在删除后没有被重置为不同的值。
to_do = ["s", "a", "b", "c", "d", "e", "f", "g", "h", "z", "w"]
done = []
dict_ = {}
dict_["s"] = [
0,
]
dict_["a"] = [999, []]
dict_["b"] = [999, []]
dict_["c"] = [999, []]
dict_["d"] = [999, []]
dict_["e"] = [999, []]
dict_["f"] = [999, []]
dict_["g"] = [999, []]
dict_["h"] = [999, []]
dict_["z"] = [999, []]
dict_["w"] = [999, []]
letter = ""
while to_do:
value = 999
count = 0
for element in to_do:
var = dict_[element]
if var[0] < value:
value = var[0]
letter = element
if letter:
index = to_do.index(letter)
to_do.pop(to_do.index(letter))
letter = ""
这将永远循环,因为你没有改变任何东西,但我认为你缺少一些你在问题中没有说明的东西。
我需要从列表 'to_do" 中删除字典中具有最小值的元素。变量 'letter' = 's' 并且编译器说列表 [= 中没有这样的元素15=]。我错过了什么?
to_do = ['s','a','b','c','d','e','f','g','h','z','w']
done = []
dict = {}
dict ['s'] = [0, ]
dict ['a'] = [999, []]
dict ['b'] = [999, []]
dict ['c'] = [999, []]
dict ['d'] = [999, []]
dict ['e'] = [999, []]
dict ['f'] = [999, []]
dict ['g'] = [999, []]
dict ['h'] = [999, []]
dict ['z'] = [999, []]
dict ['w'] = [999, []]
while to_do:
value = 999
count = 0
for element in to_do:
var = dict[element]
if var[0] < value:
value = var[0]
letter = element
#print(letter)
index = to_do.index(letter)
print(value)
to_do.pop(to_do.index(letter))
你的代码中的错误是字母在删除后没有被重置为不同的值。
to_do = ["s", "a", "b", "c", "d", "e", "f", "g", "h", "z", "w"]
done = []
dict_ = {}
dict_["s"] = [
0,
]
dict_["a"] = [999, []]
dict_["b"] = [999, []]
dict_["c"] = [999, []]
dict_["d"] = [999, []]
dict_["e"] = [999, []]
dict_["f"] = [999, []]
dict_["g"] = [999, []]
dict_["h"] = [999, []]
dict_["z"] = [999, []]
dict_["w"] = [999, []]
letter = ""
while to_do:
value = 999
count = 0
for element in to_do:
var = dict_[element]
if var[0] < value:
value = var[0]
letter = element
if letter:
index = to_do.index(letter)
to_do.pop(to_do.index(letter))
letter = ""
这将永远循环,因为你没有改变任何东西,但我认为你缺少一些你在问题中没有说明的东西。