Reputation: 317
If the nth element of pair_length
passes a condition, then I want to write to an output file the nth elements of x_cords
, y_cords
, and pair_num
. For example, if the 3rd element of the pair_length
list passes the condition length <= average_pair_length():
, then I want to select the 3rd elements of x_cords
, y_cords
, and pair_num
lists so I can write them to an output file. I tried using the filter() function but it did not return what I wanted.
Here is my code:
pair_length = []
pair_nums = []
x_cords = []
y_cords = []
for line in islice(data, 1, None):
fields = line.split(" ")
pair_num = float(fields[0])
pair_nums.append(pair_num)
x_cord = float(fields[1])
x_cords.append(x_cord)
y_cord = float(fields[2])
y_cords.append(y_cord)
vector_length = sqrt(x_cord**2 + y_cord**2)
pair_length.append(vector_length)
def average_pair_length():
average = mean(pair_length)
return average
index = 0
for index, length in enumerate(pair_length):
if length <= average_pair_length():
#this is where I want to find the nth element of pair_length so I can find the nth elements in the other lists
Upvotes: 0
Views: 319
Reputation: 2919
Use index
to determine which element it is in the array
for index, length in enumerate(pair_length): # index tells you which element it is
if length <= average_pair_length():
x, y, pair_num = x_cords[index], y_cords[index], pair_nums[index]
print(x, y, pair_num)
# Do what you want to do with the matching x, y, and pair_num here
Upvotes: 1