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 get the sum of unique elements from dictionary list values.

In this python dictionary program, we will get the sum of unique elements from dictionary list values.

Steps to solve the program
  1. Take a dictionary with the value list as input and create an empty list.
  2. Use for loop to iterate over the value list of the dictionary.
  3. Add unique elements from the value list to the empty list using a nested for loop and an if statement.
  4. Get the sum of the list using sum().
  5. Print the output.
				
					D1 = {'a':[6,7,2,8,1],'b':[2,3,1,6,8,10],'d':[1,8,2,6,9]}
l = []

for v in D1.values():
    for num in v:
        if num not in l:
            l.append(num)
            
print("Sum: ",sum(l))
				
			

Output :

				
					Sum:  46
				
			

reverse each string value in the dictionary and add an underscore before and after the Keys.

Reverse string value in the dictionary and add an underscore before and after the Keys.

In this python dictionary program, we will reverse string value in the dictionary and add an underscore before and after the Keys.

Steps to solve the program
  1. Take a dictionary with string values as input and create an empty dictionary.
  2. Use for loop to iterate over keys and values of the dictionary.
  3. Add an underscore before and after the key using string concatenation.
  4. Add that key to the empty dictionary and its value also.
  5. Print the output.
				
					D1 = {'a':'Python','b':'Programming','c':'Learning'}
D2 = {}

for k,v in D1.items():
    D2['_'+k+'_'] = v[::-1]
    
print(D2)
				
			

Output :

				
					{'_a_': 'nohtyP', '_b_': 'gnimmargorP', '_c_': 'gninraeL'}
				
			

sum unique elements from dictionary list values.

Python program to invert a dictionary with non-unique hashable values.

In this python dictionary program, we will invert a dictionary with non-unique hashable values.

Steps to solve the program
  1. Take a dictionary as input.
  2. From collections import defaultdict. (The Python defaultdict type behaves almost exactly like a regular Python dictionary, but if you try to access or modify a missing key, then defaultdict will automatically create the key and generate a default value for it)
  3. Take a variable and assign its value equal to defaultdict(list).
  4. Use for loop to iterate over keys and values of the input dictionary.
  5. Add the same values with the different keys as keys and their keys as values to that variable to that variable.
  6. Print the output.
				
					from collections import defaultdict
D1 = {'alex':1,'bob':2,'martin':1,'robert':2}
obj = defaultdict(list)

for key, value in D1.items():
    obj[value].append(key)
    
print(obj)
				
			

Output :

				
					defaultdict(<class 'list'>, {1: ['alex', 'martin'], 2: ['bob', 'robert']})
				
			

rename key of a dictionary

Sort Dictionary by values summation