How do you extract a value from an array in python?

I want to calculate means and averages in a data set, but for many reasons that I can't go into here, my array contains my values and some "filler" values [which are currently set to -1000].

How can I calculate the mean [for example] on only the non -1000 values?

res=[-1000 for x in range[0,10]]
res[1]=2
res[5]=3
res[7]=4
#something like this?
np.mean[res>-1000]

#the result should be the mean value of 2,3 and 4 [3]

MVCE

res=[1.0, 1.0, 1.0, 1.0, 1.0, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000]
#for instance
print[np.mean[res[res > -1000]]]

Let’s learn the different ways to extract elements from a Python list When more than one item is required to be stored in a single variable in Python, we need to use lists. It is one of python’s built-in data functions. It is created by using [ ] brackets while initializing a variable.

In this article, we are going to see the different ways through which lists can be created and also learn the different ways through which elements from a list in python can be extracted.

1. Extract Elements From A Python List Using Index

Here in this first example, we created a list named ‘firstgrid’ with 6 elements in it. The print statement prints the ‘1’ element in the index.

firstgrid=["A","B","C","D","E","F"]

print[firstgrid[1]]

2. Print Items From a List Using Enumerate

Here, we created a variable named ‘vara’ and we filled the elements into the list. Then we used ‘varx’ variable to specify the enumerate function to search for ‘1,2,5’ index positions.

vara=["10","11","12","13","14","15"]

print[[varx[1] for varx in enumerate[vara] if varx[0] in [1,2,5]]]

Output: ['11', '12', '15']

3. Using Loops to Extract List Elements

You can also Extract Elements From A Python List using loops. Let’s see 3 methods to pull individual elements from a list using loops.

Method 1:

Directly using a loop to search for specified indexes.

vara=["10","11","12","13","14","15"]

print[[vara[i] for i in [1,2,5]]]

Output: ['11', '12', '15']

Method 2:

Storing list and index positions into two different variables and then running the loop to search for those index positions.

elements = [10, 11, 12, 13, 14, 15]
indices = [1,1,2,1,5]

result_list = [elements[i] for i in indices]
print[result_list]

Output: [11, 11, 12, 11, 15]

Method 3:

In this example, we used a different way to create our list. The range function creates a list containing numbers serially with 6 elements in it from 10 to 15.

numbers = range[10, 16]
indices = [1, 1, 2, 1, 5]

result = [numbers[i] for i in indices]
print[result]

Output: [12, 11, 11, 14, 15]

4. Using Numpy To View Items From a List

We can also use the popular NumPy library to help us Extract Elements From A Python List. Let’s see how that can be done here using two different methods.

Method 1:

Here, we used the numpy import function to print the index specified in variable ‘sx’ from the elements present in list ‘ax’ using np.array library function.

ax = [10, 11, 12, 13, 14, 15];
sx = [1, 2, 5] ;

import numpy as np
print[list[np.array[ax][sx]]]

Method 2:

This example uses variable storing index positions and another variable storing numbers in an array. The print statement prints the index positions stored in variable ‘sx’ with respect to a variable containing the list – ‘ay’.

sx = [1, 2, 5];
ay = np.array[[10, 11, 12, 13, 14, 15]]
print[ay[sx]]

5. Extract Elements Using The index function

The index function specifies the program to search for given indexes mentioned in brackets and then runs a loop to check for the indexes present. The statement, ‘0

Chủ Đề