Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1058 of 3363
896 Views
When it is required to generate all possible permutations of a word in a sentence, a function is defined. This function iterates over the string and depending on the condition, the output is displayed.ExampleBelow is a demonstration of the samefrom itertools import permutations def calculate_permutations(my_string): my_list = list(my_string.split()) permutes = permutations(my_list) for i in permutes: permute_list = list(i) for j in permute_list: print j print() my_string = "hi there" print("The string is :") print(my_string) ... Read More
215 Views
When it is required to extract rows from a matrix with different data types, it is iterated over and ‘set’ is used to get the distinct types.ExampleBelow is a demonstration of the samemy_list = [[4, 2, 6], ["python", 2, {6: 2}], [3, 1, "fun"], [9, (4, 3)]] print("The list is :") print(my_list) my_result = [] for sub in my_list: type_size = len(list(set([type(ele) for ele in sub]))) if len(sub) == type_size: my_result.append(sub) print("The resultant distinct data type rows are :") print(my_result)OutputThe list is : [[4, ... Read More
442 Views
When it is required to split the list into all the possible tuple pairs, a method can be defined that takes a list as a parameter and uses list comprehension to iterate through the list and use ‘extend’ methodExampleBelow is a demonstration of the samedef determine_pairings(my_list): if len(my_list)
211 Views
When it is required to reverse a given range in a list, it is iterated over and the ‘:’ operator along with slicing is used to reverse it.ExampleBelow is a demonstration of the samemy_list = ["Hi", "there", "how", 'are', 'you'] print("The list is : ") print(my_list) m, n = 2, 4 my_result = [] for elem in my_list: my_result.append(elem[m : n + 1]) print("The sliced strings are : " ) print(my_result)OutputThe list is : ['Hi', 'there', 'how', 'are', 'you'] The sliced strings are : ['', 'ere', 'w', 'e', 'u']ExplanationA list is defined, and ... Read More
328 Views
When it is required to find the cumulative mean of the dictionary keys, an empty dictionary is created, and the original dictionary is iterated over, and the items are accessed. If this is present in the dictionary, the key is appended to the empty dictionary, otherwise the value is placed instead of the key.ExampleBelow is a demonstration of the samefrom statistics import mean my_list = [{'hi' : 24, 'there' : 81, 'how' : 11}, {'hi' : 16, 'how' : 78, 'doing' : 63}] print("The list is : ") print(my_list) my_result = dict() for sub ... Read More
5K+ Views
For mean, use the mean() function. Calculate the mean for the column with NaN and use the fillna() to fill the NaN values with the mean.Let us first import the required libraries −import pandas as pd import numpy as npCreate a DataFrame with 2 columns and some NaN values. We have entered these NaN values using numpy np.NaN −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'], "Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] } )Finding mean of the column values with NaN i.e, for Units columns here. So, the Units ... Read More
503 Views
When it is required to increment the last element by 1 when a decimal value is given an input, a method named ‘increment_num’ is defined that checks to see if the last element in the list is less than 9. Depending on this, operations are performed on the elements of the list.ExampleBelow is a demonstration of the samedef increment_num(my_list, n): i = n if(my_list[i] < 9): my_list[i] = my_list[i] + 1 return my_list[i] = 0 ... Read More
773 Views
When it is required to check if a given variable is of power 3, a method named ‘check_power_of_3’ is defined that takes an integer as parameter. The modulus operator and the ‘//’ operator is used to check for the same and return True or False depending on the output.ExampleBelow is a demonstration of the samedef check_power_of_3(my_val): if (my_val == 0): return False while (my_val != 1): if (my_val % 3 != 0): return ... Read More
888 Views
When it is required to check if a given variable is of power 4, a method named ‘check_power_of_4’ is defined that takes an integer as parameter. The modulus operator and the ‘//’ operator is used to check for the same and return True or False depending on the output.ExampleBelow is a demonstration of the samedef check_power_of_4(my_val): if (my_val == 0): return False while (my_val != 1): if (my_val % 4 != 0): return ... Read More
2K+ Views
To merge Pandas DataFrame, use the merge() function. The left outer join is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −how = “left”At first, let us import the pandas library with an alias −import pandas as pd Let’s create two DataFrames to be merged −# Create DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) # Create DataFrame2 dataFrame2 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], ... Read More