How to get indices of a specific number in an array? - python-3.x

I want to pick the indices of number 8 without knowing its position in the array.
a = np.arange(10)

You can use np.where like :
>>> import numpy as np
>>> a = np.array([1,4,8,2,6,7,9,8,7,8,8,9,1,0])
>>> a
array([1, 4, 8, 2, 6, 7, 9, 8, 7, 8, 8, 9, 1, 0])
>>> np.where(a==8)[0]
array([ 2, 7, 9, 10], dtype=int64)

Related

When i use set( list_a + list_b ) it returns a dictionary. Do sets naturally return dictionaries?

I'm doing some beginner python exercises and one of them is to remove duplicates from a list. I've successfully done it, but the strange thing is that it is returning a dictionary instead of a list.
This is my code.
import random
a = []
b = []
for i in range(0,20):
n = random.randint(0,10)
a.append(n)
for i in range(0,20):
n = random.randint(0,10)
b.append(n)
print(sorted(a))
print(sorted(b))
c = set(list(a+b))
print(c)
and this is what it's spitting out
[0, 0, 1, 1, 1, 1, 2, 3, 4, 4, 6, 6, 7, 7, 7, 8, 9, 9, 10, 10]
[0, 1, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 7, 8, 9, 9, 10, 10, 10]
{0, 1, 2, 3, 4, 6, 7, 8, 9, 10}
thanks in advance!
{0, 1, 2, 3, 4, 6, 7, 8, 9, 10} is a set, not a dictionary, a dictionary would be printed as {key:value, key:value, ...}
Try print(type(c)) and you'll see it prints <class 'set'> rather than <class 'dict'>
Also try the following
s = {1,2,3}
print(type(s))
d = {'a':1,'b':2,'c':3}
print(type(d))
You'll see the type is different

Array conforming shape of a given variable

I need to do some calculations with a NetCDF file.
So I have two variables with following dimensions and sizes:
A [time | 1] x [lev | 12] x [lat | 84] x [lon | 228]
B [lev | 12]
What I need is to produce a new array, C, that is shaped as (1,12,84,228) where B contents are propagated to all dimensions of A.
Usually, this is easily done in NCL with the conform function. I am not sure what is the equivalent of this in Python.
Thank you.
The numpy.broadcast_to function can do something like this, although in this case it does require B to have a couple of extra trailing size 1 dimension added to it to satisfy the numpy broadcasting rules
>>> import numpy
>>> B = numpy.arange(12).reshape(12, 1, 1)
>>> B
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> B = B.reshape(12, 1, 1)
>>> B.shape
(12, 1, 1)
>>> C = numpy.broadcast_to(b, (1, 12, 84, 228))
>>> C.shape
(1, 12, 84, 228)
>>> C[0, :, 0, 0]
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> C[-1, :, -1, -1]
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

Python code to print elements from list that meet certain criteria [duplicate]

This question already has answers here:
Print an element in a list based on a condition
(3 answers)
Select value from list of tuples where condition
(4 answers)
How to return a subset of a list that matches a condition [duplicate]
(1 answer)
Closed 3 years ago.
I have a list with 22 integers (ranging from 1 through 9) and want to create/ print a new list containing only those integers that are above 5.
This is what I have tried so far - the result (obviously) is that 'the_list' gets printed multiple times - i.e. the number of times = the number of instances above 5.
the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)
k=5
tl2=[]
for i in the_list:
if i > k :
tl2.append(the_list)
Try this code:
>>> the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
>>> print(the_list)
[1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
>>> the_filtered_list = list(filter(lambda x: x > 5, the_list))
>>> print(the_filtered_list)
[7, 6, 6, 7, 6, 6, 7]
See
filter
lambda
EDIT:
Another option is to use a generator expression:
>>> the_filtered_list = list(i for i in the_list if i > 5)
>>> print(the_filtered_list)
[7, 6, 6, 7, 6, 6, 7]
See
generator expressions and list comprehensions
EDIT:
My initial answer was indeed slow and memory inefficient. Here is the comparison of several possibilities. Which one to choose depends on how big the list is and what it is used for later.
>>> import random
>>> import timeit
>>> import sys
>>>
>>> the_list = [random.randrange(1, 10) for _ in range(100)]
>>>
>>> timeit.timeit('filter(lambda x: x > 5, the_list)', setup=f'the_list = {the_list}')
0.15890196000000856
>>> timeit.timeit('[i for i in the_list if i > 5]', setup=f'the_list = {the_list}')
2.633208761999981
>>> timeit.timeit('(i for i in the_list if i > 5)', setup=f'the_list = {the_list}')
0.227755295999998
>>>
>>> timeit.timeit('list(filter(lambda x: x > 5, the_list))', setup=f'the_list = {the_list}')
7.5565902380000125
>>> timeit.timeit('list(i for i in the_list if i > 5)', setup=f'the_list = {the_list}')
3.599053368
>>>
>>> sys.getsizeof(filter(lambda x: x > 5, the_list))
64
>>> sys.getsizeof([i for i in the_list if i > 5])
440
>>> sys.getsizeof((i for i in the_list if i > 5))
128
>>>
>>> sys.getsizeof(list(filter(lambda x: x > 5, the_list)))
480
>>> sys.getsizeof(list(i for i in the_list if i > 5))
480
The problem is you are appending the list, not the number 'i'
the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)
k=5
tl2=[]
# the_list refers to the entire list
# i is an element in the list
for i in the_list:
if i > k :
# append the number 'i' if it is greater than k
tl2.append(i)
print (t12)
the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)
k=5
new_ls = [x for x in the_list if x >k]
print(new_ls)
try this solution

calculate the arithmetic mean

I would like to know how to calculate the arithmetic mean for all of two consecutive elements in a python-numpy array, and save the values in another array
col1sortedunique = [0.0610754, 0.27365186, 0.37697331, 0.46547072, 0.69995587, 0.72998093, 0.85794189]
thank you
If I understood you correctly you want to do something like this:
import numpy as np
arr = np.arange(0,10)
>>> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
conse_mean = (arr[:-1]+arr[1:])/2
>>> array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5])
so that would be a mapping from an array with length N to one with length N-1.
Maybe an additional explanation of the syntax:
arr[1:])
>>> array([1, 2, 3, 4, 5, 6, 7, 8, 9])
would give you your array from without the first element and
arr[:-1])
>>> array([0,1, 2, 3, 4, 5, 6, 7, 8])
without the last.
Therefore you have two smaller arrays where a element and its consecutive neighbor have the same index and you can just calculate the mean as it is done above.

Adding 2 numpy nd.array

I have to numpy.ndarray A & B which are of the following shape
A=(500000,784),B =(500000,).I need to add these 2 arrays in a way that the array B , which has labels gets added as the 785th column in the array without changing any sequence in its row- wise data.
i.e, A becomes of shape (500000,785).
np.append(A.T,[B.T], axis=0).T
For example:
A = np.array([[1,2,3],[4,5,6],[7,8,9],[10,9,11]])
B = np.array([4,5,3,6])
np.append(A.T,[B.T], axis=0).T
Output:
array([[ 1, 2, 3, 4],
[ 4, 5, 6, 5],
[ 7, 8, 9, 3],
[10, 9, 11, 6]])

Resources