0

I am trying to pull 2 separate variable from 2 arrays and have python solve a set of equations using both variables. I need to use the variables in pairs only, the arrays are already in the correct order so I feel like I just need to enumerate the variables so python only uses each variable once instead of what it is currently doing which is computing every possible combination of the variables.

pa is Phase Angle in radians, such as: 1.20729, 1.20616.

distance is in meters, such as: 34753800.126, 21632361.184.

A = Area and is set to 1

Ref is Reflectivity and is set to 0.175

The result should be the apparent magnitude of each pair of variables, such as:

  • 13.6148934 (uses 1.20729 and 34753800.126)
  • 12.584035 ( uses 1.20616 and 21632361.184)

I don't want a result that uses 1.20729 and 21632361.184

with open('ApparentMagnitude.txt', 'w') as output:
    for pa in PA_Rads_Array:
        for distance in Meters_Array:
            P = (1/pi) * (np.sin(pa) + ((pi - pa) * np.cos(pa)))
            RAP = Ref * A * P
            RAPd = RAP/(distance**2)
            M = -26.7 - (2.5 * np.log10(RAPd))
            output.write(str(M) + "\n")
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Ben
  • 3
  • 2
  • Before an overeager mod down-votes your post, format your code, include examples of what your input is like and what you expect as an output. :-) – navneethc Dec 03 '20 at 14:32
  • Does this answer your question? [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – Tomerikoo Dec 03 '20 at 14:56

1 Answers1

0

Try using the zip function:

Given two lists, say l1 = [1,2,3] and l2 = [4,5,6], zip(l1, l2) returns (1, 4), (2, 5) and (3, 6).

Then:

with open('ApparentMagnitude.txt', 'w') as output:
    for pa,distance in zip(PA_Rads_Array, Meters_Array):
        P = (1/pi) * (np.sin(pa) + ((pi - pa) * np.cos(pa)))
        RAP = Ref * A * P
        RAPd = RAP/(distance**2)
        M = -26.7 - (2.5 * np.log10(RAPd))
        output.write(str(M) + "\n")
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Keredu
  • 378
  • 1
  • 14