Two random lists from one list

Discussion in 'Python' started by noydb, Mar 11, 2011.

  1. noydb

    noydb Guest

    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
    noydb, Mar 11, 2011
    #1
    1. Advertising

  2. noydb

    Chris Hulan Guest

    On Mar 11, 1:21 pm, 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 =
    > 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:]
    Chris Hulan, Mar 11, 2011
    #2
    1. Advertising

  3. noydb

    Peter Otten Guest

    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'])
    Peter Otten, Mar 11, 2011
    #3
  4. noydb

    Tim Chase Guest

    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
    Tim Chase, Mar 11, 2011
    #4
  5. noydb

    noydb Guest

    Thanks All for your responses, all a help!
    noydb, Mar 18, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.frontiernet.net
    Replies:
    6
    Views:
    1,092
    news.frontiernet.net
    Apr 16, 2004
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    377
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. globalrev
    Replies:
    4
    Views:
    734
    Gabriel Genellina
    Apr 20, 2008
  4. Alex Untitled
    Replies:
    11
    Views:
    640
    Giampiero Zanchi
    Nov 16, 2009
  5. VK
    Replies:
    15
    Views:
    1,093
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page