B
B
Busyaska2021-12-01 19:32:42
Python
Busyaska, 2021-12-01 19:32:42

How to compare list and dictionary values ​​in Python?

How to compare prices, otherwise I have some kind of problem with cycles, and the price is displayed for each product 8 times.

cost_now = [0.455, 0.455, 0.455, 0.078, 0.03, 0.03, 2.58, 2.545]
cost_before ={
'товар первый' : 0.99,
'товар второй' : 0.99,
'товар третий'  : 0.99,
'товар четвертый'  : 0.44,
'товар пятый'  : 0.08,
'товар шестой'  : 0.03,
'товар седьмой'  : 2.3,
'товар восьмой'  : 2.2
}
for name, cost in cost_before.items():
    for i in cost_now:
        if i > cost:
            print (f'{name} сейчас стоит {i} +{i-cost:.3f}')
        elif i == cost:
            print (f'{name} сейчас стоит {i} без изменений')
        else:
            print (f'{name} сейчас стоит {i} -{cost-i:.3f}')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-12-01
@Busyaska

Instead of

for name, cost in cost_before.items():
    for i in cost_now:

let it be
for i, (name, cost) in zip(cost_now, cost_before.items()):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question