possible pairings in a set

MRAB google at mrabarnett.plus.com
Sat Apr 4 21:40:30 EDT 2009


Dave Angel wrote:
> 
> 
> Ross wrote:
>> I'm new to python and I'm trying to come up with a function that takes
>> a given number of players in a game and returns all possible unique
>> pairings. Here's the code I've come up with so far, but I'm not
>> getting the output I'd like to:
>>
>> def all_pairings(players):
>>     cleanlist = []
>>     for i in range(players):
>>         cleanlist.append(i)
>>     return cleanlist
>>     start = 0
>>     follow = start +1
>>     finallist = []
>>     while follow <= len(cleanlist)-1:
>>         for player in cleanlist:
>>             mini = cleanlist[start],cleanlist[follow]
>>             finallist.append(mini)
>>             follow +=1
>>         start+=1
>>     return finallist
>>
>> If I were to execute the function with all_pairings(4), I want to get
>> the output [[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]. Instead, I get
>> [0,1,2,3] with the code I currently have. Can you guys help me out?
>> Also, if my code is considered ugly or redundant by this community,
>> can you make suggestions to clean it up?
>>
>>   
> First problem is the return you have in the middle of the function, 
> which returns cleanlist.  That's why you're getting [0, 1, 2, 3]
> 
> Once you fix that, you'll find you have exceptions in the code where 
> cleanlist[follow] doesn't work if follow is equal to 4.
> 
> I'm afraid the code is ugly, and therefore hard to understand or fix.  
> Let me suggest an approach, rather than just giving you the code.  Why 
> not create a nested for which generates two subscripts between 0 and 4, 
> then append only those pairs in which the second is strictly greater 
> than the first.  It won't be the quickest, but it will be easy to follow.
> 
> No while loop, no explicit incrementing of variables, and in fact no 
> subscripting of the lists.  Just let them supply their own values.
>      cleanlist = range(players)             # no need for any loop, it's 
> already a list (except in Python 3.0, in which case it's a sequence that 
> still works just as well)
>      finallist = []
>      for i .....:
>              for j...
>                       if i<j:
>                             append ...
>      return finallist
> 
Actually, cleanlist[i] == i, so it's pointless. You can do just:

     finallist = []
     for i in range(players):
         for j in range(i + 1, players):
             finallist.append((i, j))

or, using list comprehension:

     finallist = [(i, j) for i in range(players) for j in range(i + 1, 
players)]



More information about the Python-list mailing list