I have following torque and speed arrays in python. My question is how can I get corresponding torque values for given speed (Each Torque index is correspond to each speed index). For ex, Speed 1 will have Torque 10, 30, 50 and Speed 7 will have Torque 20 so on.
Torque = [10, 20, 30, 40, 50]
Speed = [1, 7, 1, 8, 1]
I tried using Tuple with following code and output, but I cant able to separate Torques for given speed. For ex, at 1 speed Trq = [10, 30, 50]. Help is most appreciated.
Trq_Speed = list(zip(Speed, Torque))
Trq_Speed.sort(key=lambda x: x[0])
print(Trq_Speed)
[(10, 1), (30, 1), (50, 1), (20, 7), (40, 8)]
Use enumerate
and a defaultdict
.
from collections import defaultdict
d = defaultdict(list)
Torque = [10, 20, 30, 40, 50]
Speed = [1, 7, 1, 8, 1]
for i, e in enumerate(Speed):
d[e].append(Torque[i])
d
returns:
defaultdict(list, {1: [10, 30, 50], 7: [20], 8: [40]})
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments