Found 26504 Articles for Server Side Programming

Python - Maximum difference across lists

AmitDiwan
Updated on 15-Sep-2021 12:25:07

458 Views

When it is required to find the maximum difference across the lists, the ‘abs’ and the ‘max’ methods are used.ExampleBelow is a demonstration of the samemy_list_1 = [7, 9, 1, 2, 7] my_list_2 = [6, 3, 1, 2, 1] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_result = max(abs(my_list_2[index] - my_list_1[index]) for index in range(0, len(my_list_1) - 1)) print("The maximum difference among the lists is :") print(my_result)OutputThe first list is : [7, 9, 1, 2, 7] The second list is : [6, 3, 1, 2, 1] The maximum difference ... Read More

Python - Remove positional rows

AmitDiwan
Updated on 15-Sep-2021 12:22:58

140 Views

When it is required to remove positional rows, a simple iteration and the ‘pop’ method is used.ExampleBelow is a demonstration of the samemy_list = [[31, 42, 2], [1, 73, 29], [51, 3, 11], [0, 3, 51], [17, 3, 21], [1, 71, 10], [0, 81, 92]] print("The list is :") print(my_list) my_index_list = [1, 2, 5] for index in my_index_list[::-1]: my_list.pop(index) print("The output is :") print(my_list)OutputThe list is : [[31, 42, 2], [1, 73, 29], [51, 3, 11], [0, 3, 51], [17, 3, 21], [1, 71, 10], [0, 81, 92]] The output is : [[31, ... Read More

Python – Strip whitespace from a Pandas DataFrame

AmitDiwan
Updated on 15-Sep-2021 12:28:27

921 Views

To strip whitespace, whether its leading or trailing, use the strip() method. At first, let us import thr required Pandas library with an alias −import pandas as pdLet’s create a DataFrame with 3 columns. The first column is having leading and trailing whitespaces −dataFrame = pd.DataFrame({    'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ' Furniture', 'Stationery'], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Refrigerators', 'Chairs', 'Diaries'], 'Quantity': [10, 50, 10, 20, 25, 50]}) Removing whitespace from a single column “Product Category” −dataFrame['Product Category'].str.strip()ExampleFollowing is the complete code − import pandas as pd # create a dataframe ... Read More

Python program to compute the power by Index element in List

AmitDiwan
Updated on 15-Sep-2021 12:20:03

292 Views

When it is required to compute the power by index element in a list, the simple iteration along with the ‘**’ operator is used.ExampleBelow is a demonstration of the samemy_list = [62, 18, 12, 63, 44, 75] print("The list is :") print(my_list) my_result = [] for my_index, elem in enumerate(my_list): my_result.append(elem ** my_index) print("The result is :") print(my_result)OutputThe list is : [62, 18, 12, 63, 44, 75] The result is : [1, 18, 144, 250047, 3748096, 2373046875]ExplanationA list is defined and is displayed on the console.An empty list is defined.The list is iterated ... Read More

Python - Filter dictionaries by values in Kth Key in list

AmitDiwan
Updated on 15-Sep-2021 12:18:04

249 Views

When it is required to filter dictionaries by values in ‘K’th key in a list, a simple iteration by specifying the condition is used.ExampleBelow is a demonstration of the samemy_list = [{"Python": 2, "is": 4, "cool": 11}, {"Python": 5, "is": 1, "cool": 1}, {"Python": 7, "is": 3, "cool": 7}, {"Python": 9, "is": 9, "cool": 8}, {"Python": 4, "is": 10, "cool": 6}] print("The list is :") print(my_list) search_list = [1, 9, 8, 4, 5] key = "is" my_result = [] for sub in my_list: ... Read More

Python - Most common Combination in Matrix

AmitDiwan
Updated on 15-Sep-2021 12:18:39

264 Views

When it is required to find the most common combination in a matrix, a simple iteration, along with the ‘sort’ method and ‘Counter’ method is used.ExampleBelow is a demonstration of the samefrom collections import Counter from itertools import combinations my_list = [[31, 25, 77, 82], [96, 15, 23, 32]] print("The list is :") print(my_list) my_result = Counter() for elem in my_list:    if len(elem) < 2:       continue    elem.sort()    for size in range(2, len(elem) + 1):       for comb in combinations(elem, size):          my_result[comb] += ... Read More

How to Groupby values count on the Pandas DataFrame

AmitDiwan
Updated on 15-Sep-2021 12:19:04

528 Views

To Groupby value counts, use the groupby(), size() and unstack() methods of the Pandas DataFrame. At first, create a DataFrame with 3 columns −dataFrame = pd.DataFrame({    'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'], 'Quantity': [10, 50, 10, 20, 25, 50]}) Now, groupby values count with groupby() method. For count, use the size() and unstack(). The unstack() gives a new level of column labels −dataFrame = dataFrame.groupby(['Product Category', 'Product Name', 'Quantity']).size().unstack(fill_value=0)ExampleFollowing is the complete code −import pandas as pd # create a dataframe with 3 columns ... Read More

Python program to get all pairwise combinations from a list

AmitDiwan
Updated on 15-Sep-2021 12:11:10

608 Views

When it is required to get all pairwise combinations from a list, an iteration along with the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [15, "John", 2, "Will", 53, 'Rob'] print("The list is :") print(my_list) my_result = [] for i in range(0, len(my_list)): for j in range(0, len(my_list)): if (i!=j): my_result.append((my_list[i], my_list[j])) print("The result is :") print(my_result)OutputThe list is : [15, 'John', 2, 'Will', 53, 'Rob'] The result is : [(15, 'John'), (15, ... Read More

Python - Unique values count of each Key

AmitDiwan
Updated on 15-Sep-2021 12:09:10

392 Views

When it is required to find unique values count of every key, an iteration along with the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [12, 33, 33, 54, 84, 16, 16, 16, 58] print("The list is :") print(my_list) filtered_list = [] elem_count = 0 for item in my_list: if item not in filtered_list: elem_count += 1 filtered_list.append(item) print("The result is :") print(elem_count)OutputThe list is : [12, 33, 33, 54, 84, 16, 16, 16, 58] The result ... Read More

Python program to Mark duplicate elements in string

AmitDiwan
Updated on 15-Sep-2021 12:07:25

229 Views

When it is required to mark duplicate elements in a string, list comprehension along with the ‘count’ method is used.ExampleBelow is a demonstration of the samemy_list = ["python", "is", "fun", "python", "is", "fun", "python", "fun"] print("The list is :") print(my_list) my_result = [value + str(my_list[:index].count(value) + 1) if my_list.count(value) > 1 else value for index, value in enumerate(my_list)] print("The result is :") print(my_result)OutputThe list is : ['python', 'is', 'fun', 'python', 'is', 'fun', 'python', 'fun'] The result is : ['python1', 'is1', 'fun1', 'python2', 'is2', 'fun2', 'python3', 'fun3']ExplanationA list is defined and is displayed on the console.The list comprehension ... Read More

Advertisements