Sorry for the pretty undescriptive title, this is a pretty weird problem. I'm using this code to find all possible combinations of my list:
from itertools import chain, combinations
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
stuff = ['pretzel', 'sesame', 'sourdough', 'donut', 'waffles', 'beef', 'turkey', 'chicken', 'tuna', 'vegan', 'pork', 'steak', 'bison', 'cheese', 'kethcup', 'mayo', 'pickles', 'mustard', 'lettuce', 'onions', 'tomato', 'bacon', 'bbq', 'hotsauce', 'pb', 'jelly', 'butter', 'jalapeno', 'frenchfry', 'apple', 'honeymustard', 'onionrings', 'ranch', 'macncheese', 'pulledpork', 'avacado', 'mushrooms']
for i, combo in enumerate(powerset(stuff), 1):
print('combo #{}: {}'.format(i, combo))
To be completely honest with you, this is the first hunk of code I found on Google that did pretty much what I wanted. It spits out all of the possible combinations, and even gives each one a number! However, what I need it to do has changed and I really can't figure out what to do (which isn't really saying much) and so I'm coming here to ask: How would I make it so only some of the items in that list can be used together? For example, I don't want the types of bun or meat to be able to be listed together, but the condiments can be used in any combination (or lack thereof). Thanks in advance.
Edit: What I'm trying to accomplish in the end is that I need to find and list all of the possible combinations of bun, meat, and condiments on a given sandwich. The types of bun and meat in a given option can only be used once, but any combination of any condiments is acceptable (so using them all is an option).
You could do:
def subsets(l):
ret = []
for n in range(len(l)+1):
ret += list(itertools.combinations(l, n))
return ret
buns = ["sesame", "sourdough"]
meat = ["ham" , "beef"]
condiments = ["ketchup","mustard", "onions"]
combo = []
for b in buns:
for m in meat:
for c in subsets(condiments):
x = [b,m]
for s in c:
x.append(s)
combo.append(x)
combo would contain all the possible answers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.