【问题标题】:Randomize/shuffle a list/array in Python?在 Python 中随机化/打乱列表/数组?
【发布时间】:2013-02-16 22:37:28
【问题描述】:

我是 Python 的新手,没有编程经验。我有这个(我不确定它是列表还是数组):

from random import choice
while True:
s=['The smell of flowers',
'I remember our first house',
'Will you ever forgive me?',
'I\'ve done things I\'m not proud of',
'I turn my head towards the clouds',
'This is the end',
'The sensation of falling',
'Old friends that have said good bye',
'I\'m alone',
'Dreams unrealized',
'We used to be happy',
'Nothing is the same',
'I find someone new',
'I\'m happy',
'I lie',
]
l=choice(range(5,10))
while len(s)>l:
s.remove(choice(s))
print "\nFalling:\n"+'.\n'.join(s)+'.'
raw_input('')

其中随机选择5-10行打印,但打印顺序相同;即“我说谎”如果被选中将始终位于底部。我想知道如何打乱选定的行,使它们以更随机的顺序出现?

编辑: 所以当我尝试运行这个时:

import random
s=['The smell of flowers',
'I remember our first house',
'Will you ever forgive me?',
'I\'ve done things I\'m not proud of',
'I turn my head towards the clouds',
'This is the end',
'The sensation of falling',
'Old friends that have said good bye',
'I\'m alone',
'Dreams unrealized',
'We used to be happy',
'Nothing is the same',
'I find someone new',
'I\'m happy',
'I lie',
]

picked=random.sample(s,random.randint(5,10))
print "\nFalling:\n"+'.\n'.join(picked)+'.'

它似乎在运行,但没有打印任何东西。我从 Amber 的回答中正确输入了吗?我真的不知道我在做什么。

【问题讨论】:

  • 您的代码随机选择行并删除它们。

标签: python random shuffle


【解决方案1】:
import random

s = [ ...your lines ...]

picked = random.sample(s, random.randint(5,10))

print "\nFalling:\n"+'.\n'.join(picked)+'.'

【讨论】:

    【解决方案2】:

    您也可以使用random.sample,它不会修改原始列表:

    >>> import random
    >>> a = range(100)
    >>> random.sample(a, random.randint(5, 10))
        [18, 87, 41, 4, 27]
    >>> random.sample(a, random.randint(5, 10))
        [76, 4, 97, 68, 26]
    >>> random.sample(a, random.randint(5, 10))
        [23, 67, 30, 82, 83, 94, 97, 45]
    >>> random.sample(a, random.randint(5, 10))
        [39, 48, 69, 79, 47, 82]
    

    【讨论】:

    • randinta <= b <= c
    【解决方案3】:

    您可以使用random.sample 从您的列表中随机选择一个项目。

    import random
    r = random.sample(s, random.randint(5, 10))
    

    【讨论】:

      【解决方案4】:

      这里有一个解决方案:

          import random
          s=['The smell of flowers',
          'I remember our first house',
          'Will you ever forgive me?',
          'I\'ve done things I\'m not proud of',
          'I turn my head towards the clouds',
          'This is the end',
          'The sensation of falling',
          'Old friends that have said good bye',
          'I\'m alone',
          'Dreams unrealized',
          'We used to be happy',
          'Nothing is the same',
          'I find someone new',
          'I\'m happy',
          'I lie',
          ]
          random.shuffle(s)
          for i in s[:random.randint(5,10)]:
              print i
      

      【讨论】:

      • random.shuffle 就位(我不知道为什么)。它返回None
      • 这是一种就地算法。这是最有效的方法。如果您需要原始列表,只需在洗牌前创建一个副本。
      • 您的解决方案毫无意义。 while True: 循环将继续将 s 设置为列表。它只在循环后打印random.shuffle(s)。但是,循环永远不会结束,因为没有break 子句。
      猜你喜欢
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多