[Tutor] Random order program
Charles Becker
charleshbecker at gmail.com
Mon Nov 28 08:57:10 CET 2011
Dave, Myles, et al,
On Nov 27, 2011, at 4:25 PM, Dave Angel <d at davea.name> wrote:
> On 11/27/2011 05:17 PM, myles broomes wrote:
>>
>>
>>
>>
>>
>>
>>
>> #random order list
>> while len(random_word_list) != len(word_list):
>> word = random.choice(word_list)
>> if word not in random_word_list:
>> random_word_list += word
>>
> If you use += operator with list on the left side, it assumes something compatible with list on the right. So either use
> random_word_list += [word]
> Or else use random_word_list.append(word)
>>
>>
>>
>>
>>
Everyone has offered some good feedback, I just wanted to throw in this, and hopefully everyone can say if I'm correct or not:
A way to make the code more 'pythonic' and easier to read might be to replace the conditional
while len(random_word_list) != len(word_list)
With the following :
For x in range(len(word_list))
This will prevent infinite loops, easier to read, and allows for a lot of other uses (even if x is never used). Any thoughts people? And would this method (on a small or large scale) be 'cheaper' than the original conditional? Or more 'pythonic'?
Charles
Sent from my iPhone
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111128/a1076e0e/attachment.html>
More information about the Tutor
mailing list