【发布时间】:2013-07-05 12:56:49
【问题描述】:
我正在编写两个不同的随机播放函数。
第一个 shuffle 函数必须接受一个列表并返回一个新列表,其中的元素随机排列。
这是我迄今为止的第一个随机播放功能-
def shuf(List):
import random
newList=[]
for i in List:
i=random.randrange(len(List))
newList+=i
return newList
第二个 shuffle 函数将一个列表作为参数,并在适当的位置对列表进行 shuffle。
我知道如何使用内置函数,但我不允许使用它。
【问题讨论】:
-
您愿意分享您所知道的、不允许的以及您在编写这两个不同的 shuffle 函数方面的工作进展吗?
-
你已经知道你必须实现的算法了吗?您面临什么问题?
-
请注意,Python 的
random.shuffle()的实现是用Python 编写的,所以您可以直接阅读the source code。 -
这是我到目前为止的第一个 shuffle 函数 - def shuf(List): import random newList=[] for i in List: i=random.randrange(len(List)) newList+ =我返回新列表
标签: python python-3.3