Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Two random lists from one list (http://www.velocityreviews.com/forums/t744955-two-random-lists-from-one-list.html)

 noydb 03-11-2011 06:21 PM

Two random lists from one list

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

 Chris Hulan 03-11-2011 06:39 PM

Re: Two random lists from one list

On Mar 11, 1:21*pm, noydb <jenn.du...@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 =
> teamA = random.sample(players, 4)
> print teamA
> teamB = []
> for p in players:
> * * if p not in teamA:
> * * * * teamB.append(p)
> print teamB

players = ["jake", "mike", "matt", "rich", "steve", "tom", "joe",
"jay"]
random.shuffle(players)
teamA, TeamB = players[:4],players[4:]

 Peter Otten 03-11-2011 06:43 PM

Re: Two random lists from one list

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

>>> random.shuffle(players)
>>> teamA = players[:4]
>>> teamB = players[4:]
>>> teamA, teamB

(['tom', 'mike', 'jay', 'rich'], ['jake', 'matt', 'joe', 'steve'])

 Tim Chase 03-11-2011 06:55 PM

Re: Two random lists from one list

On 03/11/2011 12:21 PM, noydb wrote:
> 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.
>
> ###
> 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

I'd be tempted to do

temp = players[:] # copy players
random.shuffle(temp) # you could directly shuffle players
# if you don't care about mangling it
team_a = temp[:4]
team_b = temp[4:]
del temp # optional

This assumes you want balanced-ish teams.

-tkc

 noydb 03-18-2011 06:16 PM

Re: Two random lists from one list

Thanks All for your responses, all a help!

 All times are GMT. The time now is 07:52 AM.