# Python: Comparing 2D arrays and Returning independent values from those arrays [closed]

I'm sure this would be a common question, however yet to find an answer for.

Suppose I have two, 2D vectors in Python

RaceA =[[ Runner ID, Heart Rate, Kilometers Done,],
[1234, 67, 50],
[1256, 83,64],
[1356,92,82],
[845,80,18]]

RaceB =[[ Runner ID, Heart Rate, Kilometers Done,],
[845, 79, 74],
[5363, 86,34],
[1256,84,53],
[12233,93,74],
[4233,40,34]]


As you can see, both races had different racers. I'd like to compare the heart rates of racers who ran both of the races.

[[Racer ID, Heart Rate A, Heart Rate B],
[1256,83,84],
[845,80,79]]


I've used the '&' operator and that returns me the racers IDs for example. But I feel like I'm missing out on a function that will return me heart rate values for both racers in a swift fashion.

This obviously isn't my problem (I have vectors 6000 and 50000 rows long) but the premise is there.

• This question is more appropriate for StackOverflow. Also, if you are alright with using pandas, then the ix method along with the & should solve this for you. – gpavanb Jul 24 '17 at 19:13
• I'll look in to pandas and the ix function. Thanks for your help and I'll submit further questions like this over on StackOverflow :) – Jake Clark Jul 25 '17 at 0:28

[[ x[0], x[1], y[1] ] for x in RaceA for y in RaceB if x[0] == y[0]]

• Glad I've helped. A couple more comments. First, my code is just two for cycles condensed in one line, for cycles are explained everywhere. This particular way of writing them is called list comprehension, google it for more info. Second, in data analysis and scientific applications usually people use other data structures (numpy arrays, pandas dataframes etc), which have built-in tools to achieve similar things faster. Lists are basic Python, and are seldom used in these fields, but if you just started it's fine. – Anon Jul 27 '17 at 8:59