user2079363
user2079363

Reputation: 19

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.

Upvotes: 1

Views: 4864

Answers (4)

imalvinz
imalvinz

Reputation: 89

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

Upvotes: 1

Sylvain Defresne
Sylvain Defresne

Reputation: 44623

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

Upvotes: 1

Blender
Blender

Reputation: 298562

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]

Upvotes: 2

Amber
Amber

Reputation: 527488

import random

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

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

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

Upvotes: 3

Related Questions