gpt4 book ai didi

python - 试图生成一个不重复的单个数字整数的随机列表。只得到 5 而不是 9

转载 作者:行者123 更新时间:2023-11-28 20:04:01 25 4
gpt4 key购买 nike

<分区>

这是我的代码

from random import randint
SudList = []
numbers = [1,2,3,4,5,6,7,8,9]
for i in numbers:
SudList.append(numbers[randint(0,len(numbers)-1)])
print("Sudlist-" + str(SudList))
numbers.remove(SudList[-1])
print("numbers-" + str(numbers))
print(SudList)

我希望这段代码返回这样的东西——(最终的 SudList)

[9,7,2,8,6,4,5,3,1]

但是它返回的是这样的东西-

[9,4,6,7,2]

这是整个输出-

Sudlist-[5]
numbers-[1, 2, 3, 4, 6, 7, 8, 9]
Sudlist-[5, 1]
numbers-[2, 3, 4, 6, 7, 8, 9]
Sudlist-[5, 1, 9]
numbers-[2, 3, 4, 6, 7, 8]
Sudlist-[5, 1, 9, 3]
numbers-[2, 4, 6, 7, 8]
Sudlist-[5, 1, 9, 3, 4]
numbers-[2, 6, 7, 8]
[5, 1, 9, 3, 4]
Press any key to quit

如何更正我的代码?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com