Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Two random lists from one list

Reply
Thread Tools

Two random lists from one list

 
 
noydb
Guest
Posts: n/a
 
      03-11-2011
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
 
Reply With Quote
 
 
 
 
Chris Hulan
Guest
Posts: n/a
 
      03-11-2011
On Mar 11, 1:21*pm, noydb <(E-Mail Removed)> 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


How about:

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

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-11-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'])

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      03-11-2011
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


 
Reply With Quote
 
noydb
Guest
Posts: n/a
 
      03-18-2011
Thanks All for your responses, all a help!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
How do I get a random number between two random numbers? Alex Untitled Ruby 11 11-16-2009 09:45 AM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
Weighted "random" selection from list of lists Jesse Noller Python 4 10-09-2005 10:11 AM



Advertisments