tips for this exercise?
kent at kentsjohnson.com
Tue Mar 28 22:54:19 CEST 2006
John Salerno wrote:
> Brian Quinlan wrote:
>>I would just write the function like this:
>> shuffle_nums = numbers[:] # copy the list to preserve the orginal
>> # order (if it matters)
>> random.shuffle(shuffle_nums) # shuffle the entire list
>> return shuffle_nums[:5] # return the first 5 elements
> Thanks. Interesting idea. I did consider copying it, but does that hurt
> performance each time the function is called? I know it may not be
> noticeable, but I don't like the idea of doing unnecessary work like
> that, if it is in fact unnecessary.
Yes, it hurts performance. No, you won't notice it. It probably is
unnecessary for your application, since you are just going to shuffle
the list again for the next use.
BUT...worrying about performance at this level is generally a waste of
your time. For a program that picks lottery numbers, performance would
have to be truly awful before you even noticed. Don't optimize until you
have identified a problem.
Python is pretty fast for most of the things you will want to do with it.
More information about the Python-list