python-切换位置:列表[i],list [list.index(minimum)] = list [list.index(munimim)],列表[i]

发布于 2025-02-03 04:34:07 字数 799 浏览 6 评论 0原文

谁能解释我,为什么不可能在我的功能中交换列表位置以进行选择? 这是我一开始写的,但是该功能返回与输入相同的列表:

def selection_sort(list):
    for i in range(0, len(list)):
        minimum = list[i]
        for j in range(i + 1, len(list)):
            if list[j] < minimum:
                minimum = list[j]
        list[i], list[list.index(minimum)] = list[list.index(minimum)], list[i]
    return list

然后我尝试使用一个变量索引= list.index.index(最小),然后突然起作用。

def selection_sort(list):
    for i in range(0, len(list)):
        minimum = list[i]
        for j in range(i + 1, len(list)):
            if list[j] < minimum:
                minimum = list[j]
        index = list.index(minimum)
        list[i], list[index] = list[index], list[i]
    return list

谁能解释我的区别,为什么不融合第一个解决方案? 谢谢你!

could anyone explain me, why is it not possible to swap list positions in my function for selection sort?
This is what i wrote at first, but the function returns the same list as the input:

def selection_sort(list):
    for i in range(0, len(list)):
        minimum = list[i]
        for j in range(i + 1, len(list)):
            if list[j] < minimum:
                minimum = list[j]
        list[i], list[list.index(minimum)] = list[list.index(minimum)], list[i]
    return list

Then I tried to use a variable index = list.index(minimum) and suddenly it worked.

def selection_sort(list):
    for i in range(0, len(list)):
        minimum = list[i]
        for j in range(i + 1, len(list)):
            if list[j] < minimum:
                minimum = list[j]
        index = list.index(minimum)
        list[i], list[index] = list[index], list[i]
    return list

Could anyone explain me the difference and why is it not corect the first solution?
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街女流氓 2025-02-10 04:34:07

分配的右侧(list [list.index(mix umimum)],list [i])首先计算,然后分配给左list [i], list [list.index(mixumim)]

我猜下一部分,但是我假设list [i]分配给,然后list [list.index(munimum)]。但是,由于您已经分配给list [i]您已经修改了列表,然后再查找list.index(mixumime)第二次。由于您没有在相同的列表中搜索,因此索引可能有所不同。

您的第二次尝试一次计算索引,并在两个地方使用相同的值。

附带说明,最好不要将变量命名与Python内置名称相同。您的函数采用一个称为list的参数,以便“阴影”“内置” list函数。您将无法在功能内部调用该函数,因为该名称已被盗。最好将其称为my_listarraylist_to_sort等。

The right hand side of the assignment (list[list.index(minimum)], list[i]) is computed first, then assigned to the tuple on the left list[i], list[list.index(minimum)].

I'm guessing the next part, but I assume list[i] is assigned to, then list[list.index(minimum)]. However, since you've already assigned to list[i] you've already modified the list before finding list.index(minimum) a second time. Since you're not searching in identical lists, the indexes may be different.

Your second attempt calculates the index once and uses the same value in both places.

As a side note, it is good practice to not name variables the same as a python builtin name. Your function takes a parameter called list so that "shadows" the builtin list function. You wouldn't be able to call that builtin inside your function because the name has been stolen. Better to call it my_list, array, or list_to_sort etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文