Не работает механизм подбора сочетаний одежды
У меня есть вот такой код, который подбирает сочетания верхней и нижней одежды, но печатает он только те, в которых есть одежда из списка favourites
bottom_clothes = ('jeans', 'shorts', 'skirt')
top_clothes = ('t-shirt', 'bra', 'top', 'jacket')
favourites = ('jeans')
for cloth in bottom_clothes:
for top_cloth in top_clothes:
for fav in favourites:
if fav == cloth or top_cloth:
print(f'Try {cloth} with {top_cloth}')
break
Но по итогу код выдает вот это
Try jeans with t-shirt
Try jeans with bra
Try jeans with top
Try jeans with jacket
Try shorts with t-shirt
Try shorts with bra
Try shorts with top
Try shorts with jacket
Try skirt with t-shirt
Try skirt with bra
Try skirt with top
Try skirt with jacket
Как мне исправить код, чтобы он заработал как надо???
Ответы (3 шт):
Автор решения: Andy Pavlov
→ Ссылка
Насколько я понял вопрос, то надо поверять вхождение подбираемой верхней и нижней одежды в список избранных. Код можно привести к подобному виду:
bottom_clothes = ('jeans', 'shorts', 'skirt')
top_clothes = ('t-shirt', 'bra', 'top', 'jacket')
favourites = ('jeans')
for cloth in bottom_clothes:
for top_cloth in top_clothes:
if any([cloth in favourites, top_cloth in favourites]):
print(f'Try {cloth} with {top_cloth}')
Или, если нужно почти в одну строку:
for b, t in [[b,t] for b in bottom_clothes for t in top_clothes
if any([b in favourites, t in favourites])]:
print(f'Try {b} with {t}')
Вывод
Try jeans with t-shirt
Try jeans with bra
Try jeans with top
Try jeans with jacket
Автор решения: Dmitry
→ Ссылка
Решение в лоб
bottom_clothes = ('jeans', 'shorts', 'skirt')
top_clothes = ('t-shirt', 'bra', 'top', 'jacket')
favourites = ('t-shirt','jeans')
for item_from_favourites in favourites:
if item_from_favourites in bottom_clothes:
for item in top_clothes:
print(f"Try {item_from_favourites} with {item}")
elif item_from_favourites in top_clothes:
for item in bottom_clothes:
print(f"Try {item_from_favourites} with {item}")
вывод
Try t-shirt with jeans
Try t-shirt with shorts
Try t-shirt with skirt
Try jeans with t-shirt
Try jeans with bra
Try jeans with top
Try jeans with jacket
Автор решения: TigerTV.ru
→ Ссылка
Можно так:
bottom_clothes = ('jeans', 'shorts', 'skirt')
top_clothes = ('t-shirt', 'bra', 'top', 'jacket')
favourites = ('jeans', 'bra')
clothes = (bottom_clothes, top_clothes)
for fav in favourites:
for cloth in clothes[fav in bottom_clothes]:
print(f'Try {fav} with {cloth}')
Вывод:
Try jeans with t-shirt
Try jeans with bra
Try jeans with top
Try jeans with jacket
Try bra with jeans
Try bra with shorts
Try bra with skirt