Two random lists from one list

Dan Stromberg drsalists at gmail.com
Fri Mar 11 13:46:10 EST 2011


Catenate the lists into a new list.  Then randomize the order of the new
list by iterating over each element in turn, swapping it with a random
element elsewhere in the same list (optionally including swapping it with
itself - that's easier and still gives good randomization).  This gives
linear time (n+m).

On Fri, Mar 11, 2011 at 10:21 AM, noydb <jenn.duerr at gmail.com> wrote:

> Hello All,
>
> I am just looking to see if there is perhaps a more efficient way of
> doing this below (works -- creates two random teams from a list of
> players).  Just want to see what the experts come up with for means of
> learning how to do things better.
>
> Thanks for any responses!
>
> ###
> import random
> players = ["jake", "mike", "matt", "rich", "steve", "tom", "joe",
> "jay"]
> teamA = random.sample(players, 4)
> print teamA
> teamB = []
> for p in players:
>    if p not in teamA:
>        teamB.append(p)
> print teamB
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110311/150081ec/attachment.html>


More information about the Python-list mailing list