0

I have a list that looks like:

trial_lst = [0.5, 3, 6, 40, 90, 130.8, 129, 111, 8, 9, 0.01, 9, 40, 90, 130.1, 112, 108, 90, 77, 68, 0.9, 8, 40, 90, 92, 130.4]

The list represents a series of experiments, each with a minimum and a maximum index. For example, in the list above, the minimum and maximum would be as follows:

Experiment 1:
Min: 0.5
Max: 130.8

Experiment 2:
Min: 0.01
Max: 130.1

Experiment 3:
Min: 0.9
Max: 103.4

I obtained the values for each experiment above because I know that each experiment starts at around zero (such as 0.4, 0.001, 0.009, etc.) and ends at around 130 (130, 131.2, 130.009, etc.). You can imagine a nozzle turning on and off. When it turns on, the pressure rises and as it's turned off, the pressure dips. I am trying to calculate the minimum and maximum values for each experiment.

What I've tried so far is iterating through the list to first mark each index as max, but I can't seem to get that right.

Here is my code. Any suggestions on how I can change it?

for idx, item in enumerate(trial_lst):
    if idx > 0:
        prev = trial_lst[idx-1]
        curr = item
        if prev > curr:
            result.append((curr, "max"))
        else:
            result.append((curr, ""))

I am looking for a manual way to do this, no libraries.

8
  • max((a,i) for (i,a) in enumerate(trial_lst)) and min((a,i) for (i,a) in enumerate(trial_lst)) will give you the max/min values and their indices but I don't see how the experiments are related. Commented Oct 15, 2017 at 21:00
  • Thanks for the suggestion, but this only returns the min value and its index Commented Oct 15, 2017 at 21:03
  • The title of your question does not match what you are asking. Also, where are the denominations in your list for the different experiments??? Commented Oct 15, 2017 at 21:04
  • It is a bit unclear what you are asking. Do you want to split the trial list in certain way? Commented Oct 15, 2017 at 21:04
  • 1
    Please explain, precisely, how you obtained the values for experiments 1, 2 and 3. Commented Oct 15, 2017 at 21:05

3 Answers 3

1

Use the easiest way ( sort your list or array first ):

trial_lst = [0.5, 3, 6, 40, 90, 130.8, 129, 111, 8, 9, 0.01, 9, 40, 90, 130.1, 112, 108, 90, 77, 68, 0.9, 8, 40, 90, 92, 130.4]

trial_lst.sort(key=float)

for count, items in enumerate(trial_lst):

    counter = count + 1
    last_object = (counter, trial_lst[count], trial_lst[(len(trial_lst)-1) - count])

    print( last_object )
Sign up to request clarification or add additional context in comments.

2 Comments

This is incorrect. It does not maintain the order of the maximum values. For instance, it prints out that max of the first period as 130.4 rather than 130.8
@Dunes : LOL ... its because of 0.01! i did not see that, no problem just change key=int to key=float!
1

You can easily get the index of the minimum value using the following:

my_list.index(min(my_list))

3 Comments

thanks, but this only returns the minimum value. I am looking to return multiple max and min values for each experiment. Experiments start around 0 and end around 130, so I am trying to figure out a way to calculating max and min by comparing each element..but to no avail.
What do you mean by 'multiple max and min values'?
I mean that the list is comprised of multiple experiments. Each with one min value and one max value.
1

Here is an interactive demonstration which may help:

>>> trial_lst = [0.5, 3, 6, 40, 90, 130.8, 129, 111, 8, 9, 0.01, 9, 40, 90, 130.1, 112, 108, 90, 77, 68, 0.9, 8, 40, 90, 92, 130.4]

Use values below 1 to identify where one experiment ends and another begins

>>> indices = [x[0] for x in enumerate(map(lambda x:x<1, trial_lst)) if x[1]]

Break list into sublists at those values

>>> sublists = [trial_lst[i:j] for i,j in zip([0]+indices, indices+[None])[1:]]

Compute max/min for each sublist

>>> for i,l in enumerate(sublists):
...     print "Experiment", i+1
...     print "Min", min(l)
...     print "Max", max(l)
...     print
... 
Experiment 1
Min 0.5
Max 130.8

Experiment 2
Min 0.01
Max 130.1

Experiment 3
Min 0.9
Max 130.4

1 Comment

I didn't think of breaking it into sublists. this is really helpful, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.