고객의 요청이 재고 수량을 넘지 않고 배송에 필요한 최소 수량
stock = {
'못':125,
'나사못':35,
'나비너트':8,
'와서':24,
}
order = ['나사못', '나비너트', '클립']
def get_batches(count, size):
return count // size
result = {}
for name in order:
count = stock.get(name,0)
batches = get_batches(count, 8)
if batches:
result[name] = batches
print(result) # {'나사못':4, '나비너트':1}
컴프리헨션 사용하여 루프의 로직 간결화
found = {name: get_batches(stock.get(name,0),8)
for name in order
if get_batches(stock.get(name,0),8)}
print(found)
#{'나사못':4, '나비너트':1}
found = ((name, batches) for name in order
if (batches := get_batches(stock.get(name,0), 8)))
print(next(found))
print(next(found))
#결과
('나사못',4)
('나비너트',1)