没有反转功能的反转数组,但循环不起作用

因此,我尝试在不使用反向函数的情况下从 .txt 文件反转数组。这就是我所拥有的。


numbers = read() #creates numbers array out of the txt file

numbersrev = numbers #blank array for reverse

numLength = len(numbers) #measures the length of the array


print(numbers)

print("Array length of numbers: ", numLength)


i = numLength

i = i-1 #since the array starts at 0 instead of 1

k = 0

for k in range(8):

    numbersrev[k] = numbers[i]

   print ("The ", i," element of numbers is the ", k," element of numbersrev")

   i -= 1

   k += 1


print(numbersrev)

这是我在vscode上调试后得到的结果:


[2, 4, 9, 11, 8, 3, 2, 5, 10]

Array length of numbers:  9

The  8  element of numbers is the  0  element of numbersrev

The  7  element of numbers is the  1  element of numbersrev

The  6  element of numbers is the  2  element of numbersrev

The  5  element of numbers is the  3  element of numbersrev

The  4  element of numbers is the  4  element of numbersrev

The  3  element of numbers is the  5  element of numbersrev

The  2  element of numbers is the  6  element of numbersrev

The  1  element of numbers is the  7  element of numbersrev

[10, 5, 2, 3, 8, 3, 2, 5, 10]

顶部数组是原始数组,底部数组是假定的反转数组


我一生都无法找出为什么它在中途停止改变numbersrev数组。有人知道可能是什么原因吗?


慕桂英3389331
浏览 152回答 4
4回答

守候你守候我

好吧,有几件事...首先,for 循环在 python 中自动增加它们的变量。所以:for k in range(8):    ...    i -= 1    k += 1应该:for k in range(8):    ...    i -= 1无需手动增加 k。接下来的列表不是数组。python 中的列表与 C 等语言中的数组有很大不同。列表是可变的,并且默认情况下通过引用传递。所以当你尝试创建一个空数组时:numbersrev = numbers #blank array for reverse您实际上numbers是从AND引用相同的“列表”numbersrev你应该做的是numbersrev = []然后在 for 循环中,只需附加到 numberrev 而不是分配。for k in range(numLength):    numbersrev.append(numbers[i])    print ("The ", i," element of numbers is the ", k," element of numbersrev")    i -= 1最后你可以/应该在 for 循环中引用数字的长度而不是硬编码的值,但是你如何拥有它仍然有效(假设你只得到8个数字)for k in range(numLength):    ...全部一起numbers = read() #creates numbers array out of the txt filenumbersrev = [] #blank array for reversenumLength = len(numbers) #measures the length of the array print(numbers)print("Array length of numbers: ", numLength)i = numLengthi = i-1 #since the array starts at 0 instead of 1for k in range(numLength):    numbersrev.append(numbers[i])    print ("The ", i," element of numbers is the ", k," element of numbersrev")    i -= 1print(numbersrev)

慕标琳琳

您在这里所做的就是使用以下行将反转数组指定为普通数组:numbersrev = numbers #blank array for reverse您实际上对该循环所做的事情是这样的:numbers[0] = numbers[9] # [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]numbers[1] = numbers[8] # [10, 9, 3, 4, 5, 6, 7, 8, 9, 10]numbers[2] = numbers[7] # [10, 9, 8, 4, 5, 6, 7, 8, 9, 10]numbers[3] = numbers[6] # [10, 9, 8, 7, 5, 6, 7, 8, 9, 10]numbers[4] = numbers[5] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]numbers[5] = numbers[4] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]numbers[6] = numbers[3] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]numbers[7] = numbers[2] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]numbers[8] = numbers[1] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]numbers[9] = numbers[0] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]...如果您像这样分配变量:numLength = len(numbers) #measures the length of the array numbersrev = [0]*numLength #blank array for reverse您将得到正确的答案,因为反向列表不再指向正常列表。

慕斯709654

你面临的问题是因为这条线numbersrev = numbers #blank array for reverse在这一行中,您不是创建空数组,而是将numbers数组的引用保存在名为 的新变量中numbersrev。这意味着当您在数组中进行操作时,numbersrev您也会更改 的值numbers。为了避免此类问题,您有两种选择:使用切片制作数组的副本通过这种方式,您可以复制数组的值,而不是数组的引用。这意味着您对新数组所做的更改不会更改原始数组。numbersrev = numbers[:]创建一个空数组并使用追加而不是赋值此更改与您所做的略有不同,但基本上您不是创建数组的副本,而是创建一个新数组,该数组将填充到 for 循环中,如下所示:numbers.rev = []...for k in range(8):    numbersrev.append(numbers[i])因此,使用第一个选项并更改 k 索引中的一些内容,我们有这样的代码:numbers = [2, 4, 9, 11, 8, 3, 2, 5, 10] # you can change for readnumbersrev = numbers[:] #copy array elementsnumLength = len(numbers) #array lengthprint(numbers)print("Array length of numbers: ", numLength)i = numLength - 1# you don't need to initialize k because it's initialized and incremented with range in the loopfor k in range(numLength):    numbersrev[k] = numbers[i]    print ("The ", i," element of numbers is the ", k," element of numbersrev")    i -= 1print(numbersrev)希望这能帮助你解决问题,就像注释一样,你可以通过多种方式解决这个问题:切片(numbers[::-1])、列表理解和其他一些。所有方法都是有效的,以防万一您想探索更多。

摇曳的蔷薇

numbersrev = numbers设置numbersrev为指向与 相同的列表numbers,这意味着当您修改numbers或numbersrev同时修改另一个列表时。要制作对象的实际副本,您需要调用numbersrev = numbers.copy(). 另外,@sahasrara62的评论是正确的,你需要打电话for k in range(numLength)而不是for k in range(8)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python