Solved

Creating a string from an unknown amount of members of a list

Posted on 2006-07-11
5
207 Views
Last Modified: 2010-04-16
Hi there,

Here is my problem...  

vehiclesLeast2Extras is a list of the following format: ["Vehicle name", "Extra1", "Extra2", "Extra3" ....]
extrasOnlyList then creates a new list from this one which only has the extras: ["Extra1", "Extra2", "Extra3" ....]

This extrasOnlyList is then put through combinations() which returns all of the combinations of members of this extrasOnlyList.

I want then to be able to print out all of the combinations of extras for vehicles that have more than two extras available.
This is what I have so far:


def combinations(elements):
    """Find all combinations of vehicles that have at least 2 extras"""
    first = elements.pop()
    if len(elements)==0:
      return [[first], []]
      
    # recurse
    subel = combinations(elements)
    return [[first] + el for el in subel] + subel

for lstMem in vehiclesLeast2Extras:
    extrasOnlyList = []
    for i in range(1, len(lstMem)):
        extrasOnlyList.append(lstMem[i])
   
    for combi in combinations(extrasOnlyList):
        x = len(combi)
        combiString = []
        combiString.append("%s" % (combi[0:x]))
        for cbiStr in combiString:
            print 'Possible combination of extras in a vehicle:',lstMem[0],"_",cbiStr,'" />'


But I can't get it to make all of the combi's over the range of a combi list in one string.  It must be the string formatting part of the line:
combiString.append("%s" % (combi[0:x]))



Any ideas would be appreciated.

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
5 Comments
 
LVL 17

Expert Comment

by:ramrom
ID: 17087053
Please show us the output you want. I can't decipher it from the above.
0
 
LVL 29

Expert Comment

by:pepr
ID: 17088203
Just a notice. The code:

    extrasOnlyList = []
    for i in range(1, len(lstMem)):
        extrasOnlyList.append(lstMem[i])

can be replaced by one line:  extrasOnlyList = lstMem[1:]

However, the intention is unclear. How the elements in the combination() relate to vehicles? (I.e., what the """Find all combinations of vehicles that have at least 2 extras""" comment means?), what does it mean "extra" and what does it mean "combination of extras" in your case?
0
 
LVL 29

Expert Comment

by:pepr
ID: 17088225
If it relates to your previous questions -- is the "Vehicle name" the name of the physical piece, or rather the signature like (any) "Toyota Yaris". Are the extras the enhancements of the cars that the customer can get for extra money? Could you give a real example of how the list of extras may look-like?
0
 

Author Comment

by:JohnAutoSales
ID: 17088675
Hi,

Sorry for the lack of clarity.
An example of the "Vehicle Name" would be "ToyotaYaris"

Examples of extras are "Air-Conditioning","Sunroof", "ABS", "Metallic Paint","Immobiliser"


The output I want would look like this:

Possible combination of extras in a vehicle: ToyotaYaris_Sunroof*ABS*MetallicPaint
Possible combination of extras in a vehicle: ToyotaYaris_Sunroof*ABS*Immobiliser
Possible combination of extras in a vehicle: ToyotaYaris_ABS*MetallicPaint
Possible combination of extras in a vehicle: ToyotaYaris_Sunroof*MetallicPaint
Possible combination of extras in a vehicle: ToyotaYaris_ABS*MetallicPaint
Possible combination of extras in a vehicle: ToyotaYaris_Sunroof*ABS*MetallicPaint*Air-Conditioning

..........


For all of the different combinations of the extras (assuming that the vehicle in question has at least 2 extras).

Thanks,
John
0
 
LVL 14

Accepted Solution

by:
RichieHindle earned 500 total points
ID: 17088698
I don't understand your final paragraph, but to print only the combinations of extras for vehicles that have more than two extras available, you need to add this:

    if len(extrasOnlyList) > 2:

making the script look like this:

vehiclesLeast2Extras = [        
    ["Yaris", "Sunroof", "ABS", "Alloys"],
    ["Polo", "Aircon", "CD changer"],
]

def combinations(elements):
    """Find all combinations of vehicles that have at least 2 extras"""
    first = elements.pop()
    if len(elements)==0:
     return [[first], []]
     
    # recurse
    subel = combinations(elements)
    return [[first] + el for el in subel] + subel

for lstMem in vehiclesLeast2Extras:
    extrasOnlyList = []
    for i in range(1, len(lstMem)):
        extrasOnlyList.append(lstMem[i])
   
    if len(extrasOnlyList) > 2:
        for combi in combinations(extrasOnlyList):
            x = len(combi)
            combiString = []
            combiString.append("%s" % (combi[0:x]))
            for cbiStr in combiString:
                print 'Possible combination of extras in a vehicle:',lstMem[0],"_",cbiStr,'" />'


If you're worried about the output format and want it to look friendlier, try something like this:

vehiclesLeast2Extras = [        
    ["Yaris", "Sunroof", "ABS", "Alloys"],
    ["Polo", "Aircon", "CD changer"],
]

def combinations(elements):
    """Find all combinations of vehicles that have at least 2 extras"""
    first = elements.pop()
    if len(elements)==0:
     return [[first], []]
     
    # recurse
    subel = combinations(elements)
    return [[first] + el for el in subel] + subel

for lstMem in vehiclesLeast2Extras:
    extrasOnlyList = []
    for i in range(1, len(lstMem)):
        extrasOnlyList.append(lstMem[i])
   
    if len(extrasOnlyList) > 2:
        for combi in combinations(extrasOnlyList):
            x = len(combi)
            combiString = []
            combiString.append("%s" % (", ".join(combi[0:x])))
            for cbiStr in combiString:
                print 'Possible combination of extras in a vehicle:',lstMem[0],"_",cbiStr,'" />'


Expanding on what ramrom said above, it's always helpful if you can include in your question:

 o A script we can actually run, without having to guess at the inputs
 o The output of that script, and what's wrong with it
 o The desired output of the script.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
Python win32com to iterate though outlook mail folder 3 1,463
Perl Frameworks 1 107
Cassandra Select Query 1 106
Getting Variable not defined error in Python 1 39
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…

740 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