0

How to get minimum value in predefined set list in python 3.x?

I have a list of data 'a'

a = {44.1, 78.2, 74.2, 85.4, 107.6, 167.7, 221.6, 245.2, 299.2, 314.5}

I wanted to get minimum value for 3 consecutive data range, starting from the last three, coming forward to the first three data.

Minimum value of last three (245.2, 299.2, 314.5), next three (221.6, 245.2, 299.2), next three (167.7, 221.6, 245.2) and so on until first three (44.1, 78.2, 74.2). The minimum values should become a list as well.

This is what I tried but failed.

i = list(range(-1, -10, -1))
value = min(a[i - 3:i])
print(value)

I got this error

TypeError: unsupported operand type(s) for -: 'list' and 'int'

at line 2.

Gus
  • 13
  • 2
  • 1. The error message is quite clear about the problem; you're trying to subtract 3 from a list, which doesn't make sense. Read up on `range`. 2. Sets aren't ordered data structures, so the overall task doesn't make much sense. – jonrsharpe Feb 17 '18 at 09:21
  • 1
    You have an inheritent problem: sets arent ordered - so your "first three" "last three" makes not much sense. They might be become "order of insertion guaranteed" in the future, but currently thats an implementation detail that is not guaranteed to hold over all python implementations on any system.- – Patrick Artner Feb 17 '18 at 09:22

5 Answers5

3

Since sets are unordered, you should make a a list instead, which is an ordered data structure:

>>> a = [44.1, 78.2, 74.2, 85.4, 107.6, 167.7, 221.6, 245.2, 299.2, 314.5]

Since you want to start at the end, you need to also reverse this list:

>>> a = a[::-1]
>>> a 
[314.5, 299.2, 245.2, 221.6, 167.7, 107.6, 85.4, 74.2, 78.2, 44.1]

Then you can slice the list into groups of 3 and get the minimum for each group:

>>> [min(x) for x in zip(a, a[1:], a[2:])]
[245.2, 221.6, 167.7, 107.6, 85.4, 74.2, 74.2, 44.1]
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
1

Based on @RoadRunner answer:

You want a moving minimum of 3 consecutive numbers...

a = [44.1, 78.2, 74.2, 85.4, 107.6, 167.7, 221.6, 245.2, 299.2, 314.5]
a.reverse()
[min(a[i:i+3]) for i in range(0, len(a) - 2)]

With the -2 you ensure that you always have 3 numbers.

spadarian
  • 1,604
  • 10
  • 14
1

Start off using a Pandas DataFrame. One of the operations in a DataFrame is the idea of "rolling" (taking X at a time and doing something with it)

import pandas as pd
data = pd.DataFrame([44.1, 78.2, 74.2, 85.4, 107.6, 167.7, 221.6, 245.2, 299.2, 314.5])
answer = data.rolling(3).min()

If you want to start at the end just reverse the list (I know you have a set() there but that's not a good data structure for this)

Back2Basics
  • 7,406
  • 2
  • 32
  • 45
  • Thanks everyone! I think this is easier for me to understand using dataframe. Sorry for the trouble ^_^ – Gus Feb 17 '18 at 10:49
1

This is one way using bottleneck.move_min:

from bottleneck import move_min

a = [44.1, 78.2, 74.2, 85.4, 107.6, 167.7, 221.6, 245.2, 299.2, 314.5]
a = a[::-1]

list(move_min(a, window=3)[2:])

# [ 245.2,  221.6,  167.7,  107.6,   85.4,   74.2,   74.2,   44.1]

The bottleneck algorithm is based on Richard Harter's O(n) The minimum on a sliding window algorithm. An O(1) algorithm is also available.

jpp
  • 159,742
  • 34
  • 281
  • 339
0

Try this code ! Also I am attach the screenshot of the output .

a = [44.1, 78.2, 74.2, 85.4, 107.6, 167.7, 221.6, 245.2, 299.2, 314.5]
a.reverse()
ans=[]
for i in range(0,len(a)-2):
  ans.append(min(a[i:i+3]))
print(ans)

enter image description here

Usman
  • 1,983
  • 15
  • 28