-1

I am new to python. Using it with grasshopper. I have 5 lists, each actually with 8760 items for which i have found max values at each index "but I also need to know which list the value came from at any given index."

I would put a simple example to explain myself better. For 2 lists A = [5,10,15,20,25] B = [4,9,16,19,26]

Max value per index = [5,10,16,20,26]

What I want is something like Max value per index = [5(A), 10(A), 16(B), 20(A), 26(B)]

Or something along the line that can relate. I am not sure whether its possible.

I would really appreciate the help. Thank you.

Eson
  • 1
  • 1

1 Answers1

1

This can be adapted to N lists.

[(max(a),a.index(max(a))) for a in list(zip(A,B))]

The .index(max(a)) gets the index at which the max(a) occurs.

The output for your example is

[(5, 0), (10, 0), (16, 1), (20, 0), (26, 1)]

Of course, if both A and B share the same value, then the index will be the first one found, A.

See https://docs.python.org/3.3/library/functions.html for description of very useful zip built-in function.

jpf
  • 1,447
  • 12
  • 22
  • Thank you for such rapid response. I will check it out. Looking at the result, is it possible then to extract two separate lists into two where one contains [15,10,16,20,26] while the other has [0,0,1,0,1]. Many thanks. – Eson Jun 01 '20 at 19:11
  • @Eson Sure, if the result is `C=[(max(a),a.index(max(a))) for a in list(zip(A,B))]`, then you can use the `zip` function again to extract the two lists (well, tuples), as `maxes,locations=list(zip(*C))` where `C` is "unpacked" with asterisk (https://stackoverflow.com/questions/3480184/unpack-a-list-in-python) before `zip`. – jpf Jun 01 '20 at 19:20
  • may I know, how can I use the "locations = [0,0,1,0,1]" to call the original value (i.e. maxes) from two the two lists? I need kind of reciprocal to this process where I need to make list of data from the lists by using the "locations list indices and location value". Example: Making "C" from "A" and "B" but cant use "max(list) but rather using "location list" If you could help please. Thanks. – Eson Jun 02 '20 at 07:13
  • @Eson If I understand your request, this should work. If you place all your lists into a list, `D=[A,B]`, then `D[locations[i]]` will refer to the correct list for the `i`th maximum (from comparing the ith element of A and B, and `D[locations[i]][i]` will be the value itself. – jpf Jun 02 '20 at 10:59
  • Hi @jpf, "D[locations[i]" and "D[locations[i]][i]" gives me "Runtime error (UnboundNameException): name 'i' is not defined". My guess is I need to define "for i in something". Just to be clear, what i intend is if "A=[1,2,3,4] and B=[6,7,8,9] then if Locations = [0,1,0,0] the result i want is to be res = [1,7,3,4]" As said before, I am applying this to 5 lists each with 8760 indices, actually retrieving energy data for different window states. Thanks – Eson Jun 02 '20 at 16:03
  • @Eson, yes `i` refers to the `i`th column of the list of lists, for which the maximum is `D[locations[i]][i]`. To see every element, location, and maximum, you can do `for i in range(len(A)): print(i,locations[i],D[locations[i]][i])` for example. – jpf Jun 02 '20 at 20:52
  • just to be clear, in this case of last example, I am "NOT" looking for "MAXIMUM" value but only the "CORRESPONDING" value as per location list. As i gave example if "A=[1,2,3,4] and B=[6,7,8,9] then if Locations = [0,1,0,0] the result i want is to be res = [1,7,3,4]", here "res" is not giving maximum but only corresponding values. – Eson Jun 03 '20 at 12:32
  • Yes, whatever criterion you use to define `locations` on per index basis, `D[locations[i]][i]` will give value according to that criterion. In your original problem of finding max, `locations` was determined based on maximum value per index, so that's what is returned. – jpf Jun 03 '20 at 16:01