Solved

Getting all possible combinations of members of a list

Posted on 2006-07-06
2
405 Views
Last Modified: 2010-08-05
Hi there,

I have got a list containing the make of a car and all of the possible extras that are possible with that particular make.  The list is of the following type:

[[Toyota Yaris, Sun-Roof, Air-Con, ABS, Immobiliser, Metallic Paint, Passenger Airbag, Leather Seats, Sports Suspension] , [Volkswagon Polo, Sun-Roof, Air-Con, ABS, Immobiliser, Metallic Paint, Passenger Airbag, Leather Seats, Sports Suspension, Chrome Grill, Body Coloured Mirrors]

Another list is then needed that will have the car make along with the possible combinations of extras available to the customer.  A list like this:

[[Toyota Yaris, Sun-Roof, Air-Con] , [Toyota Yaris, Metallic Paint] , [Volkswagon Polo, ABS, Immobiliser, Metallic Paint, Passenger Airbag]] and all of the other combinations.

For this I have been using the following function, but it's not working right:

def combi(listmember):
      
    first = listmember.pop()
    if len(listmember)==0:
        return [[first], []]
      
    submember = combi(listmember)
    return [[first] + lstmem for lstmem in submember] + submember


I need for all of the resulting combinations to be stored in another list and have the following format:

[[Make%Extra1*Extra2*Extra3.......] , [Make%Extra1*Extra2....] , ................]


Hope you can help,

John
0
Comment
Question by:JohnAutoSales
  • 2
2 Comments
 
LVL 14

Expert Comment

by:RichieHindle
ID: 17050391
I don't immediately have code to do this, but do you realise how big these lists are going to be?  That Polo has 1,024 different combinations of extras!
0
 
LVL 14

Accepted Solution

by:
RichieHindle earned 500 total points
ID: 17050508
Here you go:

import pprint

OPTIONS = [
    ["Toyota Yaris", "Sun-Roof", "Air-Con", "ABS", "Immobiliser"],
    ["Volkswagon Polo", "Immobiliser", "Metallic Paint", "Passenger Airbag"],
]

def get_combinations(options):
    """Find all combinations by counting in binary."""
    combinations = []
    for i in range(1, 2**len(options)):
        combinations.append([options[j] for j in range(len(options)) if (1 << j) & i])
    return combinations

result = []
for row in OPTIONS:
    car, combinations = row[0], get_combinations(row[1:])
    for combination in combinations:
        result.append("%s%%%s" % (car, "*".join(combination)))

pprint.pprint(result)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now