B
B
Busyaska2021-11-30 21:22:37
Python
Busyaska, 2021-11-30 21:22:37

How to compare the values ​​of two lists while iterating over the elements?

How to compare values ​​from two lists while iterating over elements and output the difference?

cost_one = [0.405, 0.405, 0.405, 0.072, 0.03, 0.03, 2.646, 2.691]
cost_two = [0.99, 0.99, 0.99, 0.44, 0.08, 0.03, 2.3, 2.2]
for i in cost_one:
    for k in cost_two:
        if i > k:
            print (i ,'больше', k ,'на' i - k)
        else:
            print (k ,'больше', i ,'на', k-i)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-11-30
@Busyaska

cost_one = [0.405, 0.405, 0.405, 0.072, 0.03, 0.03, 2.646, 2.691]
cost_two = [0.99, 0.99, 0.99, 0.44, 0.08, 0.03, 2.3, 2.2]

for x,y in zip(cost_one, cost_two):
    if x < y:
        x,y = y,x
    print(f'{x} больше {y} на {x-y:.3f}')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question