Solved
Creating a string from an unknown amount of members of a list
Posted on 2006-07-11
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