简体   繁体   中英

Randomize/shuffle a list/array in Python?

I'm completely new to Python and don't have programming experience. I have this(which I'm not sure if it's a list or array):

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('')

Which randomly selects 5-10 lines and prints them, but they print in the same order; ie "I lie" will always be at the bottom if it is selected. I was wondering how I can shuffle the selected lines, so that they appear in a more random order?

Edit: So when I try to run this:

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)+'.'

It seems to run, but doesn't print anything. Did I type this correctly from Amber's answer? I really have no clue what I'm doing.

import random

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

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

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

You could also use random.sample , which doesn't modify the original list:

>>> 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]

Here's a solution:

    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

You can use random.sample to pick a random number of item from your list.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM