在陈述我的问题之前,我想让大家知道,我搜索了以前的堆栈溢出问题,比如这个问题 Python排列组合但他们没有解决我的具体问题。
比方说,我有一本水果字典和它们的成本,还有一个预算。
items = {'Apple': 1, 'Pear': 3, 'Orange': 2, 'Banana' : 4, 'Grape' : 3, 'Melon': 5, 'Lemon': 1}
budget = 10
我想创建一个能提供所有可能的物品组合的输出,我可以买到。然而,以下规则必须到位。
我已经按照基本组合的例子来做了,并且弄明白了,但我似乎无法弄明白如何用键值对和 "预算 "来做。任何帮助都是感激的。谢谢!在我说明我的问题之前,我希望它能成为我的问题。
items = {'Apple': 1, 'Pear': 3, 'Orange': 2, 'Banana' : 4, 'Grape' : 3, 'Melon': 5, 'Lemon': 1}
budget = 10
tuples = items.items()
combos = list(itertools.combinations(tuples, 4))
combos_under_budget = [ t for t in combos if sum(p[1] for p in t) <= budget ]
combos_under_budget
>>> [(('Apple', 1), ('Pear', 3), ('Orange', 2), ('Banana', 4)),
(('Apple', 1), ('Pear', 3), ('Orange', 2), ('Grape', 3)),
(('Apple', 1), ('Pear', 3), ('Orange', 2), ('Lemon', 1)),
(('Apple', 1), ('Pear', 3), ('Banana', 4), ('Lemon', 1)),
(('Apple', 1), ('Pear', 3), ('Grape', 3), ('Lemon', 1)),
(('Apple', 1), ('Pear', 3), ('Melon', 5), ('Lemon', 1)),
(('Apple', 1), ('Orange', 2), ('Banana', 4), ('Grape', 3)),
(('Apple', 1), ('Orange', 2), ('Banana', 4), ('Lemon', 1)),
(('Apple', 1), ('Orange', 2), ('Grape', 3), ('Lemon', 1)),
(('Apple', 1), ('Orange', 2), ('Melon', 5), ('Lemon', 1)),
(('Apple', 1), ('Banana', 4), ('Grape', 3), ('Lemon', 1)),
(('Apple', 1), ('Grape', 3), ('Melon', 5), ('Lemon', 1)),
(('Pear', 3), ('Orange', 2), ('Banana', 4), ('Lemon', 1)),
(('Pear', 3), ('Orange', 2), ('Grape', 3), ('Lemon', 1)),
(('Orange', 2), ('Banana', 4), ('Grape', 3), ('Lemon', 1))]
返回列表的第一项("lb")有剩余.IMO,程序范式(一般来说)更容易阅读和调试。(另外,这是我第一次尝试在SO上回答)
items = {'Apple': 1, 'Pear': 3, 'Orange': 2, 'Banana' : 4, 'Grape' : 3, 'Melon': 5, 'Lemon': 1}
budget = 10
import itertools
lk = list(items.keys()) # List of Keys
ltf = list(itertools.combinations(lk, 4)) # List of Tuples of Fruits
# Procedural
lb = [] # List under Budget
for tf in ltf:
s = 0
for f in tf:
s += items[f]
print(s, tf)
if s <= budget:
lb.append((budget - s, tf)) # Leftover, Tuple of Fruit
print('Under budget\n')
功能性较强的代码(有余)(@ilyankou的单行本回答比较优雅)。
ls_tf = [(sum([items[f] for f in tf]), tf) for tf in ltf]
lb = [(budget - s_tf[0], s_tf[1]) for s_tf in ls_tf if (s_tf[0] <= budget)]
出(程序性和功能性):
[(0, ('Apple', 'Pear', 'Orange', 'Banana')),
(1, ('Apple', 'Pear', 'Orange', 'Grape')),
(3, ('Apple', 'Pear', 'Orange', 'Lemon')),
(1, ('Apple', 'Pear', 'Banana', 'Lemon')),
(2, ('Apple', 'Pear', 'Grape', 'Lemon')),
(0, ('Apple', 'Pear', 'Melon', 'Lemon')),
(0, ('Apple', 'Orange', 'Banana', 'Grape')),
(2, ('Apple', 'Orange', 'Banana', 'Lemon')),
(3, ('Apple', 'Orange', 'Grape', 'Lemon')),
(1, ('Apple', 'Orange', 'Melon', 'Lemon')),
(1, ('Apple', 'Banana', 'Grape', 'Lemon')),
(0, ('Apple', 'Grape', 'Melon', 'Lemon')),
(0, ('Pear', 'Orange', 'Banana', 'Lemon')),
(1, ('Pear', 'Orange', 'Grape', 'Lemon')),
(0, ('Orange', 'Banana', 'Grape', 'Lemon'))]
Print of procedural(程序和功能):
10 ('Apple', 'Pear', 'Orange', 'Banana')
Under budget
9 ('Apple', 'Pear', 'Orange', 'Grape')
Under budget
11 ('Apple', 'Pear', 'Orange', 'Melon')
7 ('Apple', 'Pear', 'Orange', 'Lemon')
Under budget
11 ('Apple', 'Pear', 'Banana', 'Grape')
13 ('Apple', 'Pear', 'Banana', 'Melon')
9 ('Apple', 'Pear', 'Banana', 'Lemon')
Under budget
12 ('Apple', 'Pear', 'Grape', 'Melon')
8 ('Apple', 'Pear', 'Grape', 'Lemon')
Under budget
10 ('Apple', 'Pear', 'Melon', 'Lemon')
Under budget
10 ('Apple', 'Orange', 'Banana', 'Grape')
Under budget
12 ('Apple', 'Orange', 'Banana', 'Melon')
8 ('Apple', 'Orange', 'Banana', 'Lemon')
Under budget
11 ('Apple', 'Orange', 'Grape', 'Melon')
7 ('Apple', 'Orange', 'Grape', 'Lemon')
Under budget
9 ('Apple', 'Orange', 'Melon', 'Lemon')
Under budget
13 ('Apple', 'Banana', 'Grape', 'Melon')
9 ('Apple', 'Banana', 'Grape', 'Lemon')
Under budget
11 ('Apple', 'Banana', 'Melon', 'Lemon')
10 ('Apple', 'Grape', 'Melon', 'Lemon')
Under budget
12 ('Pear', 'Orange', 'Banana', 'Grape')
14 ('Pear', 'Orange', 'Banana', 'Melon')
10 ('Pear', 'Orange', 'Banana', 'Lemon')
Under budget
13 ('Pear', 'Orange', 'Grape', 'Melon')
9 ('Pear', 'Orange', 'Grape', 'Lemon')
Under budget
11 ('Pear', 'Orange', 'Melon', 'Lemon')
15 ('Pear', 'Banana', 'Grape', 'Melon')
11 ('Pear', 'Banana', 'Grape', 'Lemon')
13 ('Pear', 'Banana', 'Melon', 'Lemon')
12 ('Pear', 'Grape', 'Melon', 'Lemon')
14 ('Orange', 'Banana', 'Grape', 'Melon')
10 ('Orange', 'Banana', 'Grape', 'Lemon')
Under budget
12 ('Orange', 'Banana', 'Melon', 'Lemon')
11 ('Orange', 'Grape', 'Melon', 'Lemon')
13 ('Banana', 'Grape', 'Melon', 'Lemon')