Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting all possible combinations of members of a list

Posted on 2006-07-06
2
Medium Priority
?
413 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

722 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