Python值错误和索引器错误

Python值错误和索引器错误,python,Python,我有一个项目要为学校工作,我要在两个不同的城市列表中确定基本步骤的数量 当我试图运行项目来回答这个问题时,根据我试图运行的列表,我会得到两个不同的错误 下面是代码 """ This program sorts the words " London " , " Canterbury " , " York " and " Leicester " by their length (in asc

我有一个项目要为学校工作,我要在两个不同的城市列表中确定基本步骤的数量

当我试图运行项目来回答这个问题时,根据我试图运行的列表,我会得到两个不同的错误

下面是代码

"""
This program sorts the words " London " , " Canterbury " , " York " and " Leicester "
by their length (in ascending order) and prints the result out.
"""

# First , we define our input as an array of strings
cities = ["London", "Canterbury", "York", "Leicester"]

# You can  also  try it with a very  long  list. Uncomment  this  line to do so.
#cities = ['Lancaster', 'Sunderland', 'Wolverhampton', 'Nottingham', 'Oxford', 'Plymouth', 'Salisbury', 'Salford', 'Wakefield', 'Lichfield', 'Wells', 'Preston', 'Brighton  and  Hove', 'St  Albans', 'Kingston  upon  Hull', 'Chichester', 'Durham', 'Liverpool', 'Bath', 'Bradford', 'Cambridge', 'Ely', 'York', 'Exeter', 'Birmingham', 'Carlisle', 'Portsmouth', 'Chester', 'Ripon', 'Coventry', 'Gloucester', 'Sheffield', 'Winchester', 'Lincoln', 'Canterbury', 'Westminster', 'Newcastle  upon  Tyne', 'Peterborough', 'Worcester', 'Leeds', 'Norwich', 'Stoke -on-Trent', 'Southampton', 'Bristol', 'Derby', 'Truro', 'Manchester', 'Hereford', 'City of  London', 'Leicester']

# Initialize our result , which will be empty for now
result = []

# Initialize our loop variables
i = 0
j = 0
number_of_cities = len(cities)
# len () of a list gives you the number of elements in it
# len () of a string will give you the length of it

n = number_of_cities
number_of_steps = 0

# Sort using selection sort
for i in range(0, number_of_cities):
    minimum_length = len(cities[0])
    minimum_element = cities[0]

    for j in range(0, n):
        if (len(cities[j]) < minimum_length):
            minimum_length = len(cities[j])
            minimum_element = cities[j]
            number_of_steps = number_of_steps + 1

        # At the end of the second loop , we will have the shorter element in minimum_element
        # We just need to add it to our results and remove it from our working list

        result.append(minimum_element)
        cities.remove(minimum_element)
        n = n - 1

print("")
print("The ordered list is:")
print(str(result))
print("The list had " + str(number_of_cities) + " cities and I ordered it in " + str(number_of_steps) + " steps .")
“”“
这个程序将单词“伦敦”、“坎特伯雷”、“约克”和“莱斯特”排序
按长度(升序)并打印结果。
"""
#首先,我们将输入定义为字符串数组
城市=[“伦敦”、“坎特伯雷”、“约克”、“莱斯特”]
#你也可以用一个很长的列表来尝试。为此,请取消对此行的注释。
#城市=[兰开斯特、桑德兰、伍尔弗汉普顿、诺丁汉、牛津、普利茅斯、索尔兹伯里、萨尔福德、威克菲尔德、利希菲尔德、威尔斯、普雷斯顿、布莱顿和霍夫、圣奥尔本斯、赫尔上的金斯敦、奇切斯特、达勒姆、利物浦、巴斯、布拉德福德、剑桥、伊利、约克、埃克塞特、伯明翰、卡莱尔、朴茨茅斯、C海丝特、里彭、考文垂、格洛斯特、谢菲尔德、温彻斯特、林肯、坎特伯雷、威斯敏斯特、泰恩河畔纽卡斯尔、彼得伯勒、伍斯特、利兹、诺维奇、特伦特斯托克、南安普敦、布里斯托尔、德比、特鲁罗、曼彻斯特、赫里福德、伦敦城、莱斯特]
#初始化我们的结果,它现在将为空
结果=[]
#初始化循环变量
i=0
j=0
城市数量=len(城市)
#列表的len()给出其中的元素数
#字符串的len()将给出它的长度
n=城市数量
步数=0
#使用选择排序进行排序
对于范围内的i(0,城市数量):
最小长度=长度(城市[0])
最小元素=城市[0]
对于范围(0,n)内的j:
如果(len(cities[j])<最小长度):
最小长度=len(城市[j])
最小元素=城市[j]
步数=步数+1
#在第二个循环的末尾,我们将在最小_元素中使用较短的元素
#我们只需要将其添加到结果中,并将其从工作列表中删除
结果.追加(最小\u元素)
城市。删除(最小元素)
n=n-1
打印(“”)
打印(“已排序的列表为:”)
打印(str(结果))
打印(“列表中有”+str(城市的数量)+“城市”,我在”+str(步骤的数量)+“步骤”中排序。)
当我尝试使用list
cities=[“伦敦”、“坎特伯雷”、“约克”、“莱斯特”]
运行程序时,我得到以下错误:
索引器错误:列表索引超出范围

当我尝试使用list
cities=[兰开斯特、桑德兰、伍尔弗汉普顿、诺丁汉、牛津、普利茅斯、索尔兹伯里、萨尔福德、威克菲尔德、利希菲尔德、威尔斯、普雷斯顿、布莱顿和霍夫、圣奥尔本斯、赫尔上的金斯敦、奇切斯特、达勒姆、利物浦、巴斯、布拉德福德、剑桥、伊利、约克、埃克塞特、伯明翰、卡莱尔、朴茨茅斯、C海丝特、里彭、考文垂、格洛斯特、谢菲尔德、温彻斯特、林肯、坎特伯雷、威斯敏斯特、泰恩河畔纽卡斯尔、彼得伯勒、伍斯特、利兹、诺维奇、特伦特斯托克、南安普敦、布里斯托尔、德比、特鲁罗、曼彻斯特、赫里福德、伦敦城、莱斯特]
我收到以下错误
城市。删除(最小元素)值错误:列表。删除(x):x不在列表中


关于如何解决这个问题,有什么建议吗?我不确定我是否能对代码做很多修改,因为这是我们要做的。现在是假期,我所有的讲师都不在办公室,所以我无法得到任何帮助。谢谢。首先,让我解释一下,为什么没有
结果
变量。

选择排序作为任何类型的排序算法都旨在修改初始数组,而不是创建另一个。因为我没有将结果存储在单独的变量中,所以我对初始数组进行了排序并将其打印出来

“”“
这个程序将单词“伦敦”、“坎特伯雷”、“约克”和“莱斯特”排序
按长度(升序)并打印结果。
"""
#首先,我们将输入定义为字符串数组
城市=[兰开斯特、桑德兰、伍尔弗汉普顿、诺丁汉、牛津、普利茅斯、索尔兹伯里、萨尔福德、威克菲尔德、利希菲尔德、威尔斯、普雷斯顿、布莱顿和霍夫、圣奥尔本斯、赫尔上的金斯敦、奇切斯特、达勒姆、利物浦、巴斯、布拉德福德、剑桥、伊利、约克、埃克塞特、伯明翰、卡莱尔、朴茨茅斯、C海丝特、里彭、考文垂、格洛斯特、谢菲尔德、温彻斯特、林肯、坎特伯雷、威斯敏斯特、泰恩河畔纽卡斯尔、彼得伯勒、伍斯特、利兹、诺维奇、特伦特斯托克、南安普敦、布里斯托尔、德比、特鲁罗、曼彻斯特、赫里福德、伦敦城、莱斯特]
城市数量=len(城市)
#列表的len()给出其中的元素数
#字符串的len()将给出它的长度
n=城市数量
步数=0
#使用选择排序进行排序
对于范围(n-1)内的i:
最小值=i
对于范围(i+1,n)内的j:
如果len(城市[j])
问题出在城市。删除(最小元素)

例1

cities = ["London", "Canterbury", "York", "Leicester"]


for 0 
      if len(cities[0]) < minimum_length: 
           ...
cities = ['Canterbury', 'York', 'Leicester']

for 1 
      if len(cities[1]) < minimum_length:
           ...
cities = ['Canterbury', 'Leicester']

for 2 
      if len(cities[2]) < minimum_length:
           ...
cities[2] = Has been removed.

城市。删除(最小元素)值错误:列表。删除(x):x不在列表中

我已经告诉你错误的原因了


然后自己修改它。

您正试图从内部更改for循环的状态,但这并不像您认为的那样有效。特别是,这一行不会更改迭代的范围:

n = n - 1
例如:

s = ['a', 'b', 'c', 'd']
l = len(s)

for i in range(l):
    print('Current value of l:', l)
    print('Current value of i:', i)
    print('Current list element:', s[i])
    l -= 1
    print('New value of l:', l)
    print('\n')
s = ['a', 'b', 'c', 'd']
l = len(s)

for i in range(l):
    print('Current value of l:', l)
    print('Current value of i:', i)
    print('Current list element:', s[i])
    l -= 1
    print('New value of l:', l)
    print('\n')
Current value of l: 4
Current value of i: 0
Current list element: a
New value of l: 3


Current value of l: 3
Current value of i: 1
Current list element: b
New value of l: 2


Current value of l: 2
Current value of i: 2
Current list element: c
New value of l: 1


Current value of l: 1
Current value of i: 3
Current list element: d
New value of l: 0

result.append(minimum_element)