1

I am trying to do perform a simple tolerance stack circuit analysis using python instead of excel. Basically, say I have the resistor values below where it is separated by -> Minimum | Nominal | Maximum, hence the value below:

R1 -> 5 | 10 | 15 R2 -> 5 | 10 | 15

Total_R = R1 + R2

In theory, this would generate 9 combinations of 'Total_R' going from (minimum of R1 + minimum of R2) until (maximum of R1 + maximum of R2)

How can I perform this in python effectively to accommodate maybe up to 10 Resistor values?

1 Answers1

0

What you want is called Cartesian product. Python has a function for them: itertools.product:

from itertools import product

R1 = (5, 10, 15)
R2 = (13, 1313, 131313)

list(product(R1, R2))

will return you:

[(5, 13),
 (5, 1313),
 (5, 131313),
 (10, 13),
 (10, 1313),
 (10, 131313),
 (15, 13),
 (15, 1313),
 (15, 131313)]
vurmux
  • 9,420
  • 3
  • 25
  • 45
  • Thanks! But one question, how can I perform an operation say multiplication, for example the result (5, 13) that yields 65 instead of (5, 13) and so on so forth? – Kyle Nathan May 10 '19 at 12:02
  • `[pair[0] * pair[1] for pair in list(product(R1, R2))]` If my answer helped you, I will be appreciated if you will like/accept it :) – vurmux May 10 '19 at 12:04
  • Yes, exactly. You can do it by list generators or by manual looping. – vurmux May 10 '19 at 12:05