Test whether a number is scalar or not using NumPy.

In this python numpy program, we will test whether a number is scalar or not using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Test whether a number is scalar or not using np.isscalar().
  4. Print the output.
				
					import numpy as np
x = 8.9
print(np.isscalar(x))

y = [8.9]
print(np.isscalar(y))
				
			

Output: 

				
					True
False
				
			

test element-wise for real numbers of a given array.

test whether two arrays are element-wise equal.

Test element-wise for real numbers of a given array using NumPy.

In this python numpy program, we will test element-wise for real numbers of a given array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Test element-wise for real numbers of a given array using np.isreal().
  4. Print the output.
				
					import numpy as np
x = np.array([2,4,6+9j,7])

print(np.isreal(x))
				
			

Output :

				
					[ True  True False  True]
				
			

test element-wise for complex numbers of a given array.

test whether a number is scalar or not.

Test element-wise for complex numbers of a given array using NumPy.

In this python numpy program, we will test element-wise for complex numbers of a given array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array()
  3. Test element-wise for complex numbers of a given array using np.iscomplex().
  4. Print the output.
				
					import numpy as np
x = np.array([2,4,6+9j,7])

print(np.iscomplex(x))
				
			

Output :

				
					[False False  True False]
				
			

test element-wise for NaN of a given array.

test element-wise for real numbers of a given array.

Test element-wise for NaN of a given array using NumPy.

In this python numpy program, we will test element-wise for NaN of a given array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. test element-wise for NaN of a given array using np.isnan().
  4. It will return True if an element is NaN (i.e. not a number).
  5. Print the output.
				
					import numpy as np
x = np.array([2,4,np.nan,7])

print(np.isnan(x))
				
			

Output :

				
					[False False  True False]
				
			

test a given array element-wise is finite or not.

test element-wise for complex numbers of a given array.

Test a given array element-wise is finite or not using NumPy.

In this python numpy program, we will test a given array element-wise is finite or not using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Test whether a given array element-wise is finite or not using np.isfinite(),
  4. It will return True if an element is finite else False.
  5. Print the output.
				
					import numpy as np
x = np.array([6,8,3,np.inf])

print(np.isfinite(x))
				
			

Output :

				
					[ True  True  True False]
				
			

test whether none of the elements of a given array is zero.

test element-wise for NaN of a given array.

Test whether none of the elements of a given array is zero using NumPy.

In this python numpy program, we will test whether none of the elements of a given array is zero using Numpy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Test whether none of the elements of a given array is zero using np.all().
  4. Print the output.
				
					import numpy as np
x = np.array([6,0,1,0])

print(np.all(x))
				
			

Output :

				
					False
				
			

Test whether none of the elements of a given array is zero using NumPy.

In this python numpy program, we will test whether none of the elements of a given array is zero using Numpy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Test whether none of the elements of a given array is zero using np.all().
  4. Print the output.
				
					import numpy as np
x = np.array([6,8,3,5])

print(np.all(x))
				
			

Output :

				
					True
				
			

get the NumPy version

test a given array element-wise is finite or not.

Python program to convert a dictionary into n sized dictionary

Python program to convert a dictionary into n sized dictionary,  In this dictionary program, will break the dictionary into a specified size and store it in a list.

Steps to solve the program

1. Initiate three variables
    N = 3 (size of the dictionary)
    temp = {} (empty dictionary)
    count = 1 (initiate counter)
    output = [] (empty list to store output)
2. Iterate through the dictionary data using the loop.
   
for val in Input_values
3. Store key value in temp dictionary its size is equal to N, and
   add temp to the output list, re-initiate after that  count=1 . and temp={}
4. Print output.

				
					input_dict = { 'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6 }
N = 3
temp = {}
count = 1
output = []
for val in input_dict:
    if count == N:
        output.append(temp)
        count = 1
        temp = {}
    else:
        temp[val] = input_dict[val]
        count+= 1
print("Output : ", output)
				
			

Output :

				
					Output :  [{'a': 1, 'b': 2}, {'d': 4, 'e': 5}]
				
			

Sort Dictionary by values summation

Python program to sort dictionary by value summation

Python program to sort dictionary by value summation,  In this dictionary program will sort the dictionary data as per the sum of list data in ascending order.

Steps to solve the program

1. Create an empty dictionary temp, temp = {}
2. Iterate over the dictionary key and value with for loop.
    for key, value in input_dict.items()
3. Add the key and sum of values in the temp dictionary.
    temp[key] = sum(value)
4. Sort all the elements of the dictionary using the sorted method.
    which will return the list tuple of key and value pairs. 
    sorted(temp.items(), key=lambda x:x[1])
5. Convert a list of data into a dictionary.
    output = dict(sorted_values)
6. print the output.

				
					input_dict = { "x":[1,5,6], "y":[4,8,2], "c":[3,9] }
temp = {}
for key, value in input_dict.items():
    temp[key] = sum(value)

sorted_values = sorted(temp.items(), key=lambda x:x[1])
output = dict(sorted_values)
print(output)
				
			

Output :

				
					 { x:12, c:12, y:14}
				
			

Invert a given dictionary with non-unique hashable values.

convert a dictionary into n sized dictionary.