Solved

Getting all possible combinations of members of a list

Posted on 2006-07-06
2
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.properties file to call function/method 9 70
merging 2 mysql databases with java 7 75
How to get all the API from website? 11 161
python error - set 2 59
Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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…
Suggested Courses

752 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