Found 26504 Articles for Server Side Programming

Python – Inverse Dictionary Values List

AmitDiwan
Updated on 07-Sep-2021 10:40:32

349 Views

When it is required to inverse the dictionary values to a list, a simple iteration and ‘append’ method is used.Below is a demonstration of the same −from collections import defaultdict my_dict = {13: [12, 23], 22: [31], 34: [21], 44: [52, 31]} print("The dictionary is :") print(my_dict) my_result = defaultdict(list) for keys, values in my_dict.items(): for val in values: my_result[val].append(keys) print("The result is :") print(dict(my_result))OutputThe dictionary is : {34: [21], 44: [52, 31], 13: [12, 23], 22: [31]} The result is : {52: [44], 31: [44, 22], 12: [13], 21: [34], ... Read More

Python – Sort String list by K character frequency

AmitDiwan
Updated on 07-Sep-2021 09:43:22

360 Views

When it is required to sort a list of strings based on the ‘K’ number of character frequency, the ‘sorted’ method, and the lambda function is used.ExampleBelow is a demonstration of the same −my_list = ['Hi', 'Will', 'Jack', 'Python', 'Bill', 'Mills', 'goodwill'] print("The list is : " ) print(my_list) my_list.sort() print("The list after sorting is ") print(my_list) K = 'l' print("The value of K is ") print(K) my_result = sorted(my_list, key = lambda ele: -ele.count(K)) print("The resultant list is : ") print(my_result)OutputThe list is : ['Hi', 'Will', 'Jack', 'Python', 'Bill', 'Mills', 'goodwill'] The list after sorting is ... Read More

Pandas GroupBy – Count the occurrences of each combination

AmitDiwan
Updated on 07-Sep-2021 09:14:11

2K+ Views

To groupby columns and count the occurrences of each combination in Pandas, we use the DataFrame.groupby() with size(). The groupby() method separates the DataFrame into groups.At first, let us import the pandas library with an alias pd −import pandas as pdInitialize the data of lists −# initializing the data mylist = {'Car': ['BMW', 'Mercedes', 'Lamborgini', 'Audi', 'Mercedes', 'Porche', 'RollsRoyce', 'BMW'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Bangalore', 'Hyderabad', 'Mumbai', 'Mumbai', 'Delhi'], 'Sold': [95, 80, 80, 75, 90, 90, 95, 50 ]}Next, we will create a DataFrame −# DataFrame dataFrame = pd.DataFrame(mylist, columns=['Car', 'Place', 'Sold'])Now, use the groupby() to count the occurrence with ... Read More

Python Program to extracts elements from a list with digits in increasing order

AmitDiwan
Updated on 06-Sep-2021 09:15:38

152 Views

When it is required to extracts elements from a list with digits in increasing order, a simple iteration, a flag value and the ‘str’ method is used.Below is a demonstration of the same −Example Live Demomy_list = [4578, 7327, 113, 3467, 1858] print("The list is :") print(my_list) my_result = [] for element in my_list:    my_flag = True    for index in range(len(str(element)) - 1):       if str(element)[index + 1]

Python – Dual Tuple Alternate summation

AmitDiwan
Updated on 06-Sep-2021 09:13:39

177 Views

When it is required to perform dual tuple alternate summation, a simple iteration and the modulus operator are used.Below is a demonstration of the same −Example Live Demomy_list = [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] print("The list is :") print(my_list) my_result = 0 for index in range(len(my_list)):    if index % 2 == 0:       my_result += my_list[index][0]    else:       my_result += my_list[index][1] print("The result is :") print(my_result)OutputThe list is : [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] The result is : 225ExplanationA list of ... Read More

Python – Extract Rear K digits from Numbers

AmitDiwan
Updated on 06-Sep-2021 08:56:24

217 Views

When it is required to extract rear K digits from numbers, a simple list comprehension, the modulo operator and the ‘**’ operator are used.Below is a demonstration of the same −Example Live Demomy_list = [51645, 24567, 36743, 89452, 2122] print("The list is :") print(my_list) K = 3 print("The value of K is ") print(K) my_result = [element % (10 ** K) for element in my_list] print("The result is :") print(my_result)OutputThe list is : [51645, 24567, 36743, 89452, 2122] The value of K is 3 The result is : [645, 567, 743, 452, 122]ExplanationA list is defined and ... Read More

Python – Combine list with other list elements

AmitDiwan
Updated on 06-Sep-2021 08:54:58

206 Views

When it is required to combine list with other list elements, a simple iteration and ‘append’ method is used.Below is a demonstration of the same −Example Live Demomy_list_1 = [12, 14, 25, 36, 15] print("The first list is :") print(my_list_1) my_list_2 = [23, 15, 47, 12, 25] print("The second list is :") print(my_list_2) for element in my_list_2 :    my_list_1.append(element) print ("The result is :") print(my_list_1)OutputThe first list is : [12, 14, 25, 36, 15] The second list is : [23, 15, 47, 12, 25] The result is : [12, 14, 25, 36, 15, 23, 15, ... Read More

Python program to sort matrix based upon sum of rows

AmitDiwan
Updated on 06-Sep-2021 08:52:13

244 Views

When it is required to sort matrix based upon sum of rows, a method is defined that uses ‘sum’ method to determine the result.Below is a demonstration of the same −Example Live Demodef sort_sum(row):    return sum(row) my_list = [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] print("The list is :") print(my_list) my_list.sort(key = sort_sum) print("The result is :") print(my_list)OutputThe list is : [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] The result is : [[12, 41], [34, 51], [54, 36, 22], [32, 15, 67]]ExplanationA method named ‘sort_sum’ is defined that takes a ... Read More

Python – Sort by Maximum digit in Element

AmitDiwan
Updated on 06-Sep-2021 08:50:49

202 Views

When it is required to sort by maximum digit in element, a method is defined that uses ‘str’ and ‘max’ method to determine the result.Below is a demonstration of the same −Example Live Demodef max_digits(element):    return max(str(element)) my_list = [224, 192, 145, 18, 3721] print("The list is :") print(my_list) my_list.sort(key = max_digits) print("The result is :") print(my_list)OutputThe list is : [224, 192, 145, 18, 3721] The result is : [224, 145, 3721, 18, 192]ExplanationA method named ‘max_digits’ is defined that takes element as a parameter, and converts it into a string, and then gets the maximum ... Read More

Python – Calculate the percentage of positive elements of the list

AmitDiwan
Updated on 06-Sep-2021 08:49:20

362 Views

When it is required to calculate the percentage of positive elements of the list, a list comprehension and the ‘len’ method are used.Below is a demonstration of the same −Example Live Demomy_list = [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] print("The list is :") print(my_list) my_result = (len([element for element in my_list if element > 0]) / len(my_list)) * 100 print("The result is :") print(my_result)OutputThe list is : [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] The result is : 0ExplanationA list is defined and displayed on the console.A list comprehension is used ... Read More

Advertisements