Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Seperate array into three new arrays using inequalities in Python

I am trying to split an array into three new arrays using inequalities.

This will give you an idea of what I am trying to achieve:

measurement = [1, 5, 10, 13, 40, 43, 60]

for x in measurement:
    if 0 < x < 6:
        small = measurement
    elif 6 < x < 15:
        medium = measurement
    else
        large = measurement

Intended Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

small = [1, 5]
medium = [10, 13]
large = [40, 43, 60]

>Solution :

You can use numpy:

arr = np.array(measurement)     
small = arr[(arr>0)&(arr<6)]    # array([1, 5])
medium = arr[(arr>6)&(arr<15)]  # array([10, 13])
large = arr[(arr>15)]           # array([40, 43, 60])

You can also use dictionary:

d = {'small':[], 'medium':[], 'large':[]}
for x in measurement:
    if 0 < x < 6:
        d['small'].append(x)
    elif 6 < x < 15:
        d['medium'].append(x)
    else:
        d['large'].append(x)

Output:

{'small': [1, 5], 'medium': [10, 13], 'large': [40, 43, 60]}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading