Found 26504 Articles for Server Side Programming

Python – Replace value by Kth index value in Dictionary List

AmitDiwan
Updated on 20-Sep-2021 08:57:29

610 Views

When it is required to replace the value by Kth index value in a list of dictionary, the ‘isinstance’ method and a simple iteration are used.ExampleBelow is a demonstration of the samemy_list = [{'python': [5, 7, 9, 1], 'is': 8, 'good': 10},    {'python': 1, 'for': 10, 'fun': 9},    {'cool': 3, 'python': [7, 3, 9, 1]}] print("The list is :") print(my_list) K = 2 print("The value of K is") print(K) my_key = "python" for index in my_list:    if isinstance(index[my_key], list): index[my_key] = index[my_key][K] print("The result is :") ... Read More

Python – Summation of consecutive elements power

AmitDiwan
Updated on 20-Sep-2021 08:55:36

177 Views

When it is required to add the consecutive elements power, an ‘if’ condition and a simple iteration along with the ‘**’ operator are used.ExampleBelow is a demonstration of the samemy_list = [21, 21, 23, 23, 45, 45, 45, 56, 56, 67] print("The list is :") print(my_list) my_freq = 1 my_result = 0 for index in range(0, len(my_list) - 1):    if my_list[index] != my_list[index + 1]:       my_result = my_result + my_list[index] ** my_freq       my_freq = 1 else: my_freq += 1 ... Read More

Python program to find the group sum till each K in a list

AmitDiwan
Updated on 20-Sep-2021 08:53:01

183 Views

When it is required to find the group sum till each K in a list, a simple iteration and the ‘append’ method are used.ExampleBelow is a demonstration of the samefrom collections import defaultdict my_list = [21, 4, 37, 46, 7, 56, 7, 69, 2, 86, 1] print("The list is :") print(my_list) my_key = 46 print("The key is ") print(my_key) my_sum = 0 my_result = [] for ele in my_list:    if ele != my_key: my_sum += ele    else:       my_result.append(my_sum)       my_result.append(ele)   ... Read More

Python Pandas - Create Multiindex from arrays

AmitDiwan
Updated on 20-Sep-2021 08:53:23

367 Views

We will see how to create multiindex from arrays using the MultiIndex.from_arrays(). At first, let us create an array of cars −car = ['Audi', 'Lexus', 'Tesla', 'Mercedes', 'BMW', 'Toyota', 'Nissan', 'Bentley', 'Mustang']Create another array for our example, that would include the Registration Price −reg_price = [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350]Now, we will use the MultiIndex.from_arrays(). Also set the names for the levels in the index.ExampleFollowing is the code −import pandas as pd # array of cars car = ['Audi', 'Lexus', 'Tesla', 'Mercedes', 'BMW', 'Toyota', 'Nissan', 'Bentley', 'Mustang'] # array of registration price reg_price = ... Read More

Python program to find Non-K distant elements

AmitDiwan
Updated on 20-Sep-2021 08:38:54

138 Views

When it is required to find non ‘K’ distant elements, a simple iteration along with the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [91, 13, 19, 25, 35, 3, 9, 11, 0] print("The list is :") print(my_list) my_key = 2 print("The key is ") print(my_key) my_result = [] for element in my_list:    if element + my_key not in my_list and element - my_key not in my_list: my_result.append(element) print("The resultant list is :") print(my_result)OutputThe list is : [91, 13, 19, 25, 35, 3, 9, 11, ... Read More

Python – Find Product of Index Value and find the Summation

AmitDiwan
Updated on 20-Sep-2021 08:24:34

148 Views

When it is required to find the product of the index value and the summation, the ‘enumerate’ attribute is used.ExampleBelow is a demonstration of the samemy_list = [71, 23, 53, 94, 85, 26, 0, 8] print("The list is :") print(my_list) my_result = 0 for index, element in enumerate(my_list):    my_result += (index + 1) * element print("The resultant sum is :") print(my_result)OutputThe list is : [71, 23, 53, 94, 85, 26, 0, 8] The resultant sum is : 1297ExplanationA list of integers is defined and is displayed on the console.An integer value is assigned to 0.The ... Read More

Python – Sort Matrix by K Sized Subarray Maximum Sum

AmitDiwan
Updated on 20-Sep-2021 08:21:52

219 Views

When it is required to sort matrix by k sized subarray maximum sum, a method is defined that uses the ‘amx’ and ‘sum’ methods and iterates over the list.ExampleBelow is a demonstration of the samedef sort_marix_K(my_list): return max(sum(my_list[index: index + K]) for index in range(len(my_list) - K)) my_list = [[51, 23, 4, 24, 1], [45, 6, 26, 36, 5], [56, 16, 6, 36, 8], [5, 4, 36, 26, 26]] print("The list is :") print(my_list) K = 4 print("The value of K is ") print(K) my_list.sort(key=sort_marix_K) print("The resultant list is :") print(my_list)OutputThe list is ... Read More

Python - How to Group Pandas DataFrame by Minutes?

AmitDiwan
Updated on 20-Sep-2021 08:28:03

3K+ Views

We will group Pandas DataFrame using the groupby(). Select the column to be used using the grouper function. We will group minute-wise and calculate the sum of Registration Price with minutes interval for our example shown below for Car Sale Records.At first, let’s say the following is our Pandas DataFrame with three columns. We have set Date_of_Purchase with timestamp, including Date and Time both −dataFrame = pd.DataFrame(    {       "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"],       "Date_of_Purchase": [          pd.Timestamp("2021-07-28 00:10:00"),          pd.Timestamp("2021-07-28 00:12:00"), ... Read More

Python – Extract element from a list succeeded by K

AmitDiwan
Updated on 20-Sep-2021 08:17:43

215 Views

When it is required to extract element from a list succeeded by ‘K’, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [45, 65, 32, 78, 99, 10, 21, 2] print("The list is : ") print(my_list) K = 99 print("The value of K is ") print(K) my_result = [] for elem in range(len(my_list) - 1): if my_list[elem + 1] == K: my_result.append(my_list[elem]) print("The result is : " ) print(my_result)OutputThe list is : [45, 65, 32, 78, 99, 10, 21, ... Read More

Python – Test if list is Palindrome

AmitDiwan
Updated on 20-Sep-2021 08:16:18

2K+ Views

When it is required to test if a list is a palindrome, a method is defined that reverses the string and checks if it is equal to the original string. Based on the result, relevant message is displayed on the console. A list comprehension and the ‘join’ method are used.ExampleBelow is a demonstration of the samedef check_palindrome_list(my_str): if my_str == my_str[::-1]: print("The list is a palindrome") else: print("The list isn't a palindrome") my_list = [77, 1, 56, 65, 1, 77] ... Read More

Advertisements