1

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.

1
  • 2
    Your code randomly selects lines and removes them. Commented Feb 16, 2013 at 22:40

4 Answers 4

3
import random

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

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

print "\nFalling:\n"+'.\n'.join(picked)+'.'
Sign up to request clarification or add additional context in comments.

Comments

2

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]

1 Comment

randint is a <= b <= c.
1

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

Comments

1

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

3 Comments

random.shuffle is in-place (I'm not sure why). It returns None.
it's an in-place algorithm.. that's the most effective way of doing it. if you need the original list, just create a copy before shuffling.
Your solution makes no sense. the while True: loop will just keep setting s to the list. It only prints the random.shuffle(s) after the loop. However, the loop will never end, because there is no break clause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.