1

So I have two lists called A and C that each have the same amount of data points stored in them. I have created a new list called E that contains values in C that are greater than 400.

I need to make a plot of E vs. A My question is how do I get the corresponding values that I gained from creating E from the list A so that I can make a plot?

Is there a way to grab the data from A that corresponds with E which might make a new list I could plot? Thanks.

The data I am using is quite a large list, but here is an example:

    xpos,ypos,measurement,error
    96.54, 92.10, 236.69, 23.67
    26.26, 17.36, 457.55, 45.76
    96.15, 52.22, 369.31, 36.93
    53.23, 56.85, 630.77, 63.08
    82.48, 97.64, 198.24, 19.82

I have removed the headers and created four lists for each data column: A for the xpos, B for the ypos, C for measurement, and D for error.

C, or measurement is the data set that I edited to be E which has values over 400. I want to get the values from A (xpos) that match up across the row from the values over 400 in C (or measurement).

I am trying to plot in iPython notebook the list I got from E vs. the corresponding values in A.

3 Answers3

3

I would do this by ziping A and C together before calculating E:

E_pairs = [pair for pair in zip(A, C) if pair[1] > 400]

That will give you a list of tuples of A and C values where C is over 400.

Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
  • This is what I needed thank you! So then how do I split the tuple into two separate lists? I went about the way I knew how with .split() but I got an error saying the tuple was not able to be split – New Python User Jul 23 '14 at 22:27
  • 1
    The most concise and idiomatic way to split the list of tuples into two separate lists is `a_vals, e_vals = zip(*E_pairs)`. – Peter DeGlopper Jul 23 '14 at 22:35
  • `a_vals, e_vals = zip(*E_pairs)` appears to return tuples rather than lists. This may or may not be significant depending on your application. – Matti Wens Aug 13 '18 at 20:21
  • True - if you need mutability I think the `list` constructor is the best way to convert the tuple. – Peter DeGlopper Aug 20 '18 at 00:01
0
A=[100,300,400,200,500]

B=['a','b','c','d','e']

E=[[a,b] for a,b in zip(A,B)]

my_list=filter(lambda x:x[0]>400,E)
Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
K Adamu
  • 39
  • 2
  • This is mostly the same as my answer, but you don't need the list comprehension to create `E`. `E = zip(A, B)` is equivalent except that you'll get a list of tuples instead of a list of lists. But there's no need to edit the pairs, so there's also no need to convert them from tuples to lists. – Peter DeGlopper Jul 23 '14 at 22:15
  • You can treat both lists as a tuple , making it easier to plot A vs B where A>400 – K Adamu Jul 23 '14 at 22:16
  • Yes; your answer does unnecessary work to not use the output of `zip` as tuples. – Peter DeGlopper Jul 23 '14 at 22:20
  • I like yours Peter, one stroke – K Adamu Jul 23 '14 at 22:21
0

If the lists are the same size you can use enumerate and make lists of each:

final_a = []
final_c = []
for ind, x in enumerate(C):
    if x > 400:
        final_c.append(x)
        final_a.append(A[ind])
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321