Solved

Getting all possible combinations of members of a list

Posted on 2006-07-06
2
411 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

635 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