Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 更新的列表在Python中不使用 导入数学 数组=[16,5,3,4,11,9,13] 对于数组[0:len(数组)-1]中的x: 键=x 索引=数组。索引(x) posj=索引 对于数组中的y[索引+1:len(数组)]: 如果y_Python 3.x_Loops - Fatal编程技术网

Python 3.x 更新的列表在Python中不使用 导入数学 数组=[16,5,3,4,11,9,13] 对于数组[0:len(数组)-1]中的x: 键=x 索引=数组。索引(x) posj=索引 对于数组中的y[索引+1:len(数组)]: 如果y

Python 3.x 更新的列表在Python中不使用 导入数学 数组=[16,5,3,4,11,9,13] 对于数组[0:len(数组)-1]中的x: 键=x 索引=数组。索引(x) posj=索引 对于数组中的y[索引+1:len(数组)]: 如果y,python-3.x,loops,Python 3.x,Loops,而不是 import math array = [16,5,3,4,11,9,13] for x in array[0:len(array)-1]: key=x index=array.index(x) posj=index for y in array[index+1:len(array)]: if y<key: key=y posj=array.index(y) if inde

而不是

import math

array = [16,5,3,4,11,9,13]


for x in array[0:len(array)-1]:
    key=x
    index=array.index(x)
    posj=index
    for y in array[index+1:len(array)]:
        if y<key:
            key=y
            posj=array.index(y)
    if index!=posj:
        hold=array[index]
        array[index]=key
        array[posj]=hold


print(array)
试一试

输出 而不是

import math

array = [16,5,3,4,11,9,13]


for x in array[0:len(array)-1]:
    key=x
    index=array.index(x)
    posj=index
    for y in array[index+1:len(array)]:
        if y<key:
            key=y
            posj=array.index(y)
    if index!=posj:
        hold=array[index]
        array[index]=key
        array[posj]=hold


print(array)
试一试

输出
array[0:len(array)-1]
创建
array
的副本。那么,我如何保持此副本的更新?因为您尚未将副本分配给任何变量,所以不能。我也不确定为什么要创建一个副本,而不是通过indexGood point在数组上迭代,我想我对Python的无知已经显现出来了。我应该删除这个问题吗?
array[0:len(array)-1]
创建
array
的副本。那么,我如何保持此副本的更新?因为您尚未将副本分配给任何变量,所以不能。我也不确定为什么要创建一个副本,而不是通过indexGood point在数组上迭代,我想我对Python的无知已经显现出来了。我应该删除这个问题吗?这样做只会让我选择遍历每个元素。如何只遍历n-1个元素而不复制数组?在这个特定的实现中,这并不重要,但为了将来的参考,我想知道。使用计数器和中断条件。因此,您的解决方案与
array[0:len(array)]
之间的区别是什么。array[0:len(array)]创建了一个副本。这样做只会让我选择迭代每个元素。如何只遍历n-1个元素而不复制数组?在这个特定的实现中,这并不重要,但为了将来的参考,我想知道。使用计数器和中断条件,那么您的解决方案与
array[0:len(array)]
之间的区别是什么。array[0:len(array)]创建了一个副本
for x in array:
[3, 4, 5, 9, 11, 13, 16]