Programming Articles - Page 1084 of 3363

Python – Sort given list of strings by part the numeric part of string

AmitDiwan
Updated on 13-Sep-2021 07:49:18

1K+ Views

When it is required to sort a given list of strings based on a numeric part of the string, a method is defined that uses the regular expressions, the ‘map’ method and the ‘list’ method to display the result.ExampleBelow is a demonstration of the same −import re print("The regular expression package has been imported successfully.") def my_digit_sort(my_list): return list(map(int, re.findall(r'\d+', my_list)))[0] my_list = ["pyt23hon", "fu30n", "lea14rn", 'co00l', 'ob8uje3345t'] print("The list is : " ) print(my_list) my_list.sort(key=my_digit_sort) print("The list has been sorted based on the pre-defined method..") print("The resultant list is : ") ... Read More

Python – Substitute prefix part of List

AmitDiwan
Updated on 13-Sep-2021 07:39:39

212 Views

When it is required to substitute prefix part of a list, the ‘len’ method and the ‘:’ operator is used.ExampleBelow is a demonstration of the same −my_list_1 = [15, 44, 82] my_list_2 = [29, 77, 19, 44, 26, 18] print("The first list is : " ) print(my_list_1) print("The second list is : " ) print(my_list_2) print("The first list after sorting is :") my_list_1.sort() print(my_list_1) print("The first list after sorting is :") my_list_2.sort() print(my_list_2) my_result = my_list_1 + my_list_2[len(my_list_1) : ] print("The resultant list is : ") print(my_result)OutputThe first list is : [15, 44, 82] ... Read More

Python - Merge DataFrames of different length

AmitDiwan
Updated on 23-Feb-2022 13:20:27

625 Views

To merge dataframes of different length, we need to use the merge() method. Let’s say the following is our 1st DataFrame with length 4 −dataFrame1 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Jaguar']    } ) print("DataFrame1 ...", dataFrame1) print("DataFrame1 length = ", len(dataFrame1))Following is our 2nd DataFrame with length 6 −dataFrame2 = pd.DataFrame(    {       "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley']    } ) print("DataFrame2 ...", dataFrame2) print("DataFrame2 length = ", len(dataFrame2))Now, merge DataFrames using the merge() −mergedRes = dataFrame2.merge(dataFrame1, how='left')ExampleFollowing is the code −import pandas as pd # ... Read More

Python – Cross mapping of Two dictionary value lists

AmitDiwan
Updated on 13-Sep-2021 07:35:34

688 Views

When it is required to cross-map two dictionary valued lists, the ‘setdefault’ and ‘extend’ methods are used.ExampleBelow is a demonstration of the same −my_dict_1 = {"Python" : [4, 7], "Fun" : [8, 6]} my_dict_2 = {6 : [5, 7], 8 : [3, 6], 7 : [9, 8]} print("The first dictionary is : " ) print(my_dict_1) print("The second dictionary is : " ) print(my_dict_2) sorted(my_dict_1.items(), key=lambda e: e[1][1]) print("The first dictionary after sorting is ") print(my_dict_1) sorted(my_dict_2.items(), key=lambda e: e[1][1]) print("The second dictionary after sorting is ") print(my_dict_2) my_result = {} for key, value in my_dict_1.items(): ... Read More

Python – Check if elements index are equal for list elements

AmitDiwan
Updated on 13-Sep-2021 07:31:34

658 Views

When it is required to check if the index of the elements is equal to the elements in the list, a simple iteration and the enumerate attribute is used.ExampleBelow is a demonstration of the same −my_list_1 = [12, 62, 19, 79, 58, 0, 99] my_list_2 = [12, 74, 19, 54, 58, 0, 11] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_list_1.sort() my_list_2.sort() print("The first list after sorting is ") print(my_list_1) print("The second list after sorting is ") print(my_list_2) check_list = [9, 8, 2] print("The check_list is :") print(check_list) my_result ... Read More

Python – Convert List to Index and Value dictionary

AmitDiwan
Updated on 13-Sep-2021 07:28:01

719 Views

When it is required to convert a list into an index value dictionary, the ‘enumerate' and a simple iteration are used.ExampleBelow is a demonstration of the same −my_list = [32, 0, 11, 99, 223, 51, 67, 28, 12, 94, 89] print("The list is :") print(my_list) my_list.sort(reverse=True) print("The sorted list is ") print(my_list) index, value = "index", "values" my_result = {index : [], value : []} for id, vl in enumerate(my_list): my_result[index].append(id) my_result[value].append(vl) print("The result is :") print(my_result)OutputThe list is : [32, 0, 11, 99, 223, 51, 67, 28, 12, 94, ... Read More

Python – Extend consecutive tuples

AmitDiwan
Updated on 13-Sep-2021 07:24:47

166 Views

When it is required to extend consecutive tuples, a simple iteration is used.ExampleBelow is a demonstration of the same −my_list = [(13, 526, 73), (23, 67, 0, 72, 24, 13), (94, 42), (11, 62, 23, 12), (93, ), (83, 61)] print("The list is :") print(my_list) my_list.sort(reverse=True) print("The list after sorting in reverse is :") print(my_list) my_result = [] for index in range(len(my_list) - 1): my_result.append(my_list[index] + my_list[index + 1]) print("The result is :") print(my_result)OutputThe list is : [(13, 526, 73), (23, 67, 0, 72, 24, 13), (94, 42), (11, 62, 23, 12), (93, ... Read More

Python – Filter unique valued tuples

AmitDiwan
Updated on 13-Sep-2021 07:23:26

269 Views

When it is required to filter unique valued tuples from a list of tuples, the ‘list’ and ‘set’ methods are used.ExampleBelow is a demonstration of the same −my_list = [(42, 51), (46, 71), (14, 25), (26, 91), (56, 0), (11, 1), (99, 102)] print("The list of tuple is :") print(my_list) my_list.sort() print("The list after sorting is :") print(my_list) my_result = list(set(my_list)) print("The result is :") print(my_result)OutputThe list of tuple is : [(42, 51), (46, 71), (14, 25), (26, 91), (56, 0), (11, 1), (99, 102)] The list after sorting is : [(11, 1), (14, 25), (26, 91), (42, 51), (46, ... Read More

Python – 3D Matrix to Coordinate List

AmitDiwan
Updated on 13-Sep-2021 07:22:19

444 Views

When it is required to convert a three dimensional matrix into a co-ordinate list, the ‘zip’ method, and a list comprehension are used.ExampleBelow is a demonstration of the same −my_list_1 = [[['He', 'Wi'], ['llo', 'll']], [['Pyt', 'i'], ['hon', 'sFun']], [['Ho', 'g'], ['pe', 'ood']]] print("The list is : ") print(my_list_1) my_list_1.sort() print("The list after sorting is ") print(my_list_1) my_result = [ele for sub_elem_1, sub_elem_2 in my_list_1 for ele in zip(sub_elem_1, sub_elem_2)] print("The resultant list is : ") print(my_result)OutputThe list is : [[['He', 'Wi'], ['llo', 'll']], [['Pyt', 'i'], ['hon', 'sFun']], [['Ho', 'g'], ['pe', 'ood']]] The list after sorting is [[['He', ... Read More

Python – Cross Pairing in Tuple List

AmitDiwan
Updated on 13-Sep-2021 07:19:29

321 Views

When it is required to perform cross pairing in a list of tuples, the ‘zip’ method, a list comprehension and the ‘==’ operator is used.ExampleBelow is a demonstration of the same −my_list_1 = [('Hi', 'Will'), ('Jack', 'Python'), ('Bill', 'Mills'), ('goodwill', 'Jill')] my_list_2 = [('Hi', 'Band'), ('Jack', 'width'), ('Bill', 'cool'), ('a', 'b')] print("The first list is : " ) print(my_list_1) print("The second list is :") print(my_list_2) my_list_1.sort() my_list_2.sort() print("The first list after sorting is ") print(my_list_1) print("The second list after sorting is ") print(my_list_2) my_result = [(a[1], b[1]) for a, b in zip(my_list_1, my_list_2) if a[0] == b[0]] print("The ... Read More

Advertisements