如何在Python中混洗列表

时间:2015-12-11 08:38:29

标签: python list shuffle

def make_sorted_deck():
     ''' Return a sorted deck of cards. Each card is
         represented by a string as follows:
         "queen of hearts". The cards are ordered by rank and then suit within rank.
     :return: The sorted deck of cards, as a list of strings
     EXAMPLE: make_sorted_deck() == ['2 of spades', '2 of hearts', '2 of    clubs', ..., 'ace of clubs', 'ace of diamonds'] '''
     #Hint: Use the previous functions and two nested for loops.
     sorted_deck = []
     for i in get_ranks():
         for j in get_suits():
             sorted_deck.append("{0} of {1}".format(i,j))
         return sorted_deck
     print(make_sorted_deck())

def shuffle(deck):
    ''' Randomly shuffle the cards in deck into a newly created deck of cards (list).
    :param: deck: A deck of cards
    :return: A new list, consisting of a random shuffle of deck.
    EXAMPLE: shuffle(['2 of hearts', '3 of diamonds', 'jack of spades', '2 of   clubs']) could return ['jack of spades', '3 of diamonds', '2 of hearts', '2 of clubs'] 
     #REQUIREMENTS: Please implement the following algorithm: Use a while loop to repeatedly pick a random card from deck, remove it, and add it to a newly created list. '''

我如何随机播放由make_sorted_deck()创建的列表?

我知道有一个我可以导入的功能来洗牌,但我需要这样做的方法就是取出一张随机卡并将其附加到一个新的列表中以获得一个洗牌列表。

2 个答案:

答案 0 :(得分:3)

我不打算解决你的作业,但是让我给你一些提示:

    只要while x:为true-ish,
  • x就会循环播放。非空列表是真实的。

  • 您可以通过xdocs

  • 选择0 <= x < n其中x = random.randrange(n)的随机数
  • 您可以使用idocs)从列表l(即l[i])中删除索引为l.pop(i)的项目

答案 1 :(得分:0)

另一个没有回答OP问题的答案......

要调整长度为n的列表,您需要一个索引列表,从0n-1,按随机顺序...

我们开始从random模块

导入randrange函数
from random import randrange
像这样randrange(n)调用的

会返回一个随机整数i0 <= i <= n-1

当我们在0n-1之间选择第一个随机索引时,我们认为下一个索引将在更窄的时间间隔内选择,依此类推......

l = [randrange(n-i) for i in range(n)]

当然l中的最后一个数字为0,因为i==n-1randrange(1)必须返回0

l中的数字不能直接用于处理要随机播放的列表, 因为它们在混洗过程的某一点引用 available 元素列表中的位置,所以对于n中的每个数字,我们必须看到已经洗牌了多少元素,并且它们的位置是相对的对于当前元素,让我们说我们想将_real_indices存储在列表中,最初是空的

indices = []

我们必须小心......

for i in l:                       # the randomized, partial indices
    j = 0                         # aux variable
    while j <= i:                 # we will increment j later
        if j in indices:          # if this number j, smaller than i, is in the
             i += 1               # list of used indices, i must be incremented
        j += 1
    indices.append(i)             # the (possibly) incremented i is stored

这一切都是为了改组。

这里我报告一个简短的IPython会话,演示这种方法的正确性:

In [1]: from random import randrange

In [2]: def shuffle(n):
    l = [randrange(n-i) for i in range(n)]
    indices = []
    for i in l:
        j = 0
        while j <= i:
            if j in indices: i = i+1
            j = j+1
        indices.append(i)
    return indices
   ...: 

In [3]: sh = shuffle(10) ; print sh ; print sorted(sh)
[7, 6, 4, 9, 1, 5, 0, 2, 8, 3]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [4]: sh = shuffle(10) ; print sh ; print sorted(sh)
[6, 9, 5, 1, 4, 3, 0, 2, 8, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [5]: sh = shuffle(10) ; print sh ; print sorted(sh)
[3, 6, 4, 9, 0, 7, 8, 1, 2, 5]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [6]: