【问题标题】:How to shuffle a list in Python如何在 Python 中打乱列表
【发布时间】:2015-12-11 08:38:29
【问题描述】:
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() 创建的列表?

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

【问题讨论】:

标签: python list shuffle


【解决方案1】:

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

  • while x: 将循环,只要x 是真实的。非空列表是真实的。

  • 您可以通过x = random.randrange(n) (docs) 选择一个随机数x 其中0 <= x < n

  • 您可以使用l.pop(i) (docs) 从列表l(即l[i])中删除索引为i 的项目

【讨论】:

    【解决方案2】:

    另一个没有回答 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]: 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 2015-04-29
      相关资源
      最近更新 更多