如何在此特定功能中解决范围问题?

发布于 2025-01-25 07:12:14 字数 652 浏览 2 评论 0原文

我正在实现一种洗牌算法,我需要检查我运行了多少次算法以获取原始数组。我已经尝试实施一段时间循环并启动计数器,但是范围有一些问题。数组ARR和ARR2具有相等的值。有人可以帮忙吗?

from random import randint, random
from math import floor


def shuffle(the_list):
    amnt_to_shuffle = len(the_list)
    while amnt_to_shuffle > 1:
        i = int(floor(random() * amnt_to_shuffle))
        amnt_to_shuffle -= 1
        the_list[i], the_list[amnt_to_shuffle] = the_list[amnt_to_shuffle], the_list[i]
    return the_list

arr = [i for i in range(1,11)]
original_arr = arr
k = 0

while 1:
    arr2 = shuffle(arr)
    if arr2 == original_arr:
        break
    else:
        shuffle(arr)
        k += 1

print(k)

I am implementing a shuffling algorithm and I need to check how many times I have ti run the algorithm to get the original array. I have tried implementing a while loop and initiating a counter but there is some problem with the scope. The arrays arr and arr2 have equal values. Can someone help?

from random import randint, random
from math import floor


def shuffle(the_list):
    amnt_to_shuffle = len(the_list)
    while amnt_to_shuffle > 1:
        i = int(floor(random() * amnt_to_shuffle))
        amnt_to_shuffle -= 1
        the_list[i], the_list[amnt_to_shuffle] = the_list[amnt_to_shuffle], the_list[i]
    return the_list

arr = [i for i in range(1,11)]
original_arr = arr
k = 0

while 1:
    arr2 = shuffle(arr)
    if arr2 == original_arr:
        break
    else:
        shuffle(arr)
        k += 1

print(k)

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

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

发布评论

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

评论(2

新雨望断虹 2025-02-01 07:12:14

您的代码仅具有一个列表。 arrointer_arrarr2,...它们都是相同列表。您的shuffle函数突变列表它作为参数。即使该函数返回列表,此它作为参数传递的呼叫者列表,因此 列表。

不是 返回此shuffle函数中的列表,这将是更多的Pythonic。这也意味着您不需要arr2

无关,但是您的循环应该只有一个呼叫shuffle,而不是两个。

关键是使ointer_arr新列表是arr的副本。您可以通过几种方式来执行此操作,例如Slice语法:

original_arr = arr[:]
k = 0

while 1:
    shuffle(arr)
    if arr == original_arr:
        break
    k += 1

Your code has only one list. arr, original_arr, arr2, ... they are all the same list. Your shuffle function mutates the list it gets as argument. Even though that function returns the list, this is the caller's list that it passed as argument, so it is that list that is shuffled.

It would be more pythonic to not return the list in this shuffle function, so to make absolutely clear to the caller that their list is mutated (shuffled) by it. This also means you don't need arr2.

Unrelated, but your loop should just have one call to shuffle, not two.

The key is to make original_arr a new list that is a copy of arr. You can do this in several ways, e.g. by slice syntax:

original_arr = arr[:]
k = 0

while 1:
    shuffle(arr)
    if arr == original_arr:
        break
    k += 1
笑饮青盏花 2025-02-01 07:12:14

通过编写Original_arr = ARR您将arr的指针复制到oinder_arr因此,其中任何一个的任何更改都会影响另一个。

您可以导入 copy.deepcopy.deepcopy 深层复制列表或使用Original_arr = arr [:]

By writing original_arr = arr you copy the pointer of arr to original_arr hence any change in any one of them will effect the other.

You can import copy.deepcopy to deep copy the list or to use original_arr = arr[:]

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