Two random lists from one list
Peter Otten
__peter__ at web.de
Fri Mar 11 13:43:15 EST 2011
noydb 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
How about
>>> random.shuffle(players)
>>> teamA = players[:4]
>>> teamB = players[4:]
>>> teamA, teamB
(['tom', 'mike', 'jay', 'rich'], ['jake', 'matt', 'joe', 'steve'])
More information about the Python-list
mailing list