我认为你可以这样做:
import pandas as pd #To display the results
from itertools import combinations, product
# Create bread dictionary
bread = ['italian', 'wheat', 'honey oat']
bprice = [1, 2, 3]
breaddict = dict(zip(bread, bprice))
# Create meat dictionary
meat = ['roastbeef', 'ham', 'turkey', 'steak']
mprice = [3, 1, 2, 4]
meatdict= dict(zip(meat,mprice))
# Create veggie dictionary
vegetable = ['lettuce', 'onions', 'tomatoes', 'pickles']
vprice = [1, 4, 2, 3]
vegdict=dict(zip(vegetable, vprice))
# The real work is done here
# Create combinations of two veggies
# Then use that combination with bread and meat to calculate a product of sandwiches
sandwiches = product(bread,meat,combinations(vegetable,2))
# Create empty dataframe for storage and display
df=pd.DataFrame()
# Iterate through sandwiches
# and use unpacking tuple and map with dictionary to populate dataframe
for b, m, v in sandwiches:
df=df.append(pd.concat([pd.Series(', '.join([b, m, *v])).rename('Combinations'),
pd.Series(sum([breaddict[b],
meatdict[m],
*map(vegdict.get, v)])).rename('Total')],
axis=1))
print(df)
输出:
Combinations Total
0 italian, roastbeef, lettuce, onions 9
1 italian, roastbeef, lettuce, tomatoes 7
2 italian, roastbeef, lettuce, pickles 8
3 italian, roastbeef, onions, tomatoes 10
4 italian, roastbeef, onions, pickles 11
.. ... ...
67 honey oat, steak, lettuce, tomatoes 10
68 honey oat, steak, lettuce, pickles 11
69 honey oat, steak, onions, tomatoes 13
70 honey oat, steak, onions, pickles 14
71 honey oat, steak, tomatoes, pickles 12
[72 rows x 2 columns]
没有 pandas,但仍然使用 itertools.combinations 和 itertools.product
from itertools import combinations, product
bread = ['italian', 'wheat', 'honey oat']
bprice = [1, 2, 3]
breaddict = dict(zip(bread, bprice))
# Create meat dictionary
meat = ['roastbeef', 'ham', 'turkey', 'steak']
mprice = [3, 1, 2, 4]
meatdict= dict(zip(meat,mprice))
# Create veggie dictionary
vegetable = ['lettuce', 'onions', 'tomatoes', 'pickles']
vprice = [1, 4, 2, 3]
vegdict=dict(zip(vegetable, vprice))
# The real work is done here
# Create combinations of two veggies
# Then use that combination with bread and meat to calculate a product of sandwiches
sandwiches = product(bread,meat,combinations(vegetable,2))
sandwiches_with_price = [[b,m,*v],
sum([breaddict[b], meatdict[m], *map(vegdict.get, v)])) for b, m, v in sandwiches]
sandwiches_with_price
输出:
[(['italian', 'roastbeef', 'lettuce', 'onions'], 9),
(['italian', 'roastbeef', 'lettuce', 'tomatoes'], 7),
(['italian', 'roastbeef', 'lettuce', 'pickles'], 8),
(['italian', 'roastbeef', 'onions', 'tomatoes'], 10),
(['italian', 'roastbeef', 'onions', 'pickles'], 11),
(['italian', 'roastbeef', 'tomatoes', 'pickles'], 9),
(['italian', 'ham', 'lettuce', 'onions'], 7),
(['italian', 'ham', 'lettuce', 'tomatoes'], 5),
(['italian', 'ham', 'lettuce', 'pickles'], 6),
(['italian', 'ham', 'onions', 'tomatoes'], 8),
(['italian', 'ham', 'onions', 'pickles'], 9),
(['italian', 'ham', 'tomatoes', 'pickles'], 7),
(['italian', 'turkey', 'lettuce', 'onions'], 8),
(['italian', 'turkey', 'lettuce', 'tomatoes'], 6),
(['italian', 'turkey', 'lettuce', 'pickles'], 7),
(['italian', 'turkey', 'onions', 'tomatoes'], 9),
(['italian', 'turkey', 'onions', 'pickles'], 10),
(['italian', 'turkey', 'tomatoes', 'pickles'], 8),
(['italian', 'steak', 'lettuce', 'onions'], 10),
(['italian', 'steak', 'lettuce', 'tomatoes'], 8),
(['italian', 'steak', 'lettuce', 'pickles'], 9),
(['italian', 'steak', 'onions', 'tomatoes'], 11),
(['italian', 'steak', 'onions', 'pickles'], 12),
(['italian', 'steak', 'tomatoes', 'pickles'], 10),
(['wheat', 'roastbeef', 'lettuce', 'onions'], 10),
(['wheat', 'roastbeef', 'lettuce', 'tomatoes'], 8),
(['wheat', 'roastbeef', 'lettuce', 'pickles'], 9),
(['wheat', 'roastbeef', 'onions', 'tomatoes'], 11),
(['wheat', 'roastbeef', 'onions', 'pickles'], 12),
(['wheat', 'roastbeef', 'tomatoes', 'pickles'], 10),
(['wheat', 'ham', 'lettuce', 'onions'], 8),
(['wheat', 'ham', 'lettuce', 'tomatoes'], 6),
(['wheat', 'ham', 'lettuce', 'pickles'], 7),
(['wheat', 'ham', 'onions', 'tomatoes'], 9),
(['wheat', 'ham', 'onions', 'pickles'], 10),
(['wheat', 'ham', 'tomatoes', 'pickles'], 8),
(['wheat', 'turkey', 'lettuce', 'onions'], 9),
(['wheat', 'turkey', 'lettuce', 'tomatoes'], 7),
(['wheat', 'turkey', 'lettuce', 'pickles'], 8),
(['wheat', 'turkey', 'onions', 'tomatoes'], 10),
(['wheat', 'turkey', 'onions', 'pickles'], 11),
(['wheat', 'turkey', 'tomatoes', 'pickles'], 9),
(['wheat', 'steak', 'lettuce', 'onions'], 11),
(['wheat', 'steak', 'lettuce', 'tomatoes'], 9),
(['wheat', 'steak', 'lettuce', 'pickles'], 10),
(['wheat', 'steak', 'onions', 'tomatoes'], 12),
(['wheat', 'steak', 'onions', 'pickles'], 13),
(['wheat', 'steak', 'tomatoes', 'pickles'], 11),
(['honey oat', 'roastbeef', 'lettuce', 'onions'], 11),
(['honey oat', 'roastbeef', 'lettuce', 'tomatoes'], 9),
(['honey oat', 'roastbeef', 'lettuce', 'pickles'], 10),
(['honey oat', 'roastbeef', 'onions', 'tomatoes'], 12),
(['honey oat', 'roastbeef', 'onions', 'pickles'], 13),
(['honey oat', 'roastbeef', 'tomatoes', 'pickles'], 11),
(['honey oat', 'ham', 'lettuce', 'onions'], 9),
(['honey oat', 'ham', 'lettuce', 'tomatoes'], 7),
(['honey oat', 'ham', 'lettuce', 'pickles'], 8),
(['honey oat', 'ham', 'onions', 'tomatoes'], 10),
(['honey oat', 'ham', 'onions', 'pickles'], 11),
(['honey oat', 'ham', 'tomatoes', 'pickles'], 9),
(['honey oat', 'turkey', 'lettuce', 'onions'], 10),
(['honey oat', 'turkey', 'lettuce', 'tomatoes'], 8),
(['honey oat', 'turkey', 'lettuce', 'pickles'], 9),
(['honey oat', 'turkey', 'onions', 'tomatoes'], 11),
(['honey oat', 'turkey', 'onions', 'pickles'], 12),
(['honey oat', 'turkey', 'tomatoes', 'pickles'], 10),
(['honey oat', 'steak', 'lettuce', 'onions'], 12),
(['honey oat', 'steak', 'lettuce', 'tomatoes'], 10),
(['honey oat', 'steak', 'lettuce', 'pickles'], 11),
(['honey oat', 'steak', 'onions', 'tomatoes'], 13),
(['honey oat', 'steak', 'onions', 'pickles'], 14),
(['honey oat', 'steak', 'tomatoes', 'pickles'], 12)]