简体   繁体   中英

How to iterate through a dictionary and calculate the values based on the values from a list?

There are 8 motors and I am monitoring their speeds. I have a dictionary through which I am iterating and then I am calculating the difference in the speed with the individual retrieved speeds.

  • Then I have to compare if the change is less than the maximum values for each motor (which is there in the speeds list for each motor).
  • Currently what is happening is it is calculating the difference only for the last speed
  • Retrieved speeds dictionary looks like {1: 8490, 2: 7920, 3: 8460, 4: 7890, 5: 8460, 6: 7950, 7: 8430, 8: 6720}
speeds = {1: 8800, 2: 8800, 3: 8800, 4: 8800, 5: 8800, 6: 8800, 7: 8800, 8: 7300}
for index , speed in speeds.items():
    print("Please print these for me to understand", speed, retrieved_speeds_dict[index])
    absolute_change = abs(speed - retrieved_speeds_dict[index] )

rpms = [500, 600, 500, 700, 800, 100, 200, 500]
for each_rpm in rpms:
    if absolute_change > each_rpm:
        err_msg = err_msg + f"Out of range by {absolute_change}"
        print(err_msg)
    else:
        self.log.info(f"Speed is correct with {absolute_change}")

Can someone please help to fix the loops so that it calculates the difference and compare it with each rpm value.

Is this the kind of thing you want?

retrieved_speeds = { 1: 8490, 2: 7920, 3: 8460, 4: 7890, 5: 8460, 6: 7950, 7: 8430, 8: 6720 }
speeds = {1: 8800, 2: 8800, 3: 8800, 4: 8800, 5: 8800, 6: 8800, 7: 8800, 8: 7300}
rpms = [500, 600, 500, 700, 800, 100, 200, 500]

for  desired,actual,tolerance in zip( speeds.items(), retrieved_speeds.values(), rpms):
    index, desired = desired
    variance = abs(actual-desired)
    if variance > tolerance:
        delta = abs(variance-tolerance)
        err_msg = f"{index} Out of range by {delta}"
        print(err_msg)
    else:
        print(f"{index} Speed is correct within {variance}")

Output:

1 Speed is correct within 310
2 Out of range by 280
3 Speed is correct within 340
4 Out of range by 210
5 Speed is correct within 340
6 Out of range by 750
7 Out of range by 170
8 Out of range by 80

retrieved_speeds_dict = {1: 8490, 2: 7920, 3: 8460, 4: 7890, 5: 8460, 6: 7950, 
7: 8430, 8: 6720}
speeds = {1: 8800, 2: 8800, 3: 8800, 4: 8800, 5: 8800, 6: 8800, 7: 8800, 8: 
7300}
for a, each_rpm in zip(speeds.items(), rpms):
    index, speed = a
    print("Please print these for me to understand", speed, 
    retrieved_speeds_dict[index])
    absolute_change = abs(speed - retrieved_speeds_dict[index] )
    if absolute_change > each_rpm:
        err_msg = f"Out of range by {absolute_change}"
        print(err_msg)
    else:
        print(f"Speed is correct with {absolute_change}") 

Using the zip function, you can loop through the dictionary and list at the same time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM