
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
Found 10476 Articles for Python

331 Views
When it is required to convert a list of strings with a delimiter to a list of tuples, a K value is set, and list comprehension along with the ‘split’ method is used.ExampleBelow is a demonstration of the same −my_list = ["33-22", "13-44-81-39", "42-10-42", "36-56-90", "34-77-91"] print("The list is : " ) print(my_list) print("The sorted list is ") my_list.sort() print(my_list) K = "-" print("The value of K is ") print(K) my_result = [tuple(int(element) for element in sub.split(K)) for sub in my_list] print("The resultant list is : ") print(my_result)OutputThe list is : ['33-22', '13-44-81-39', '42-10-42', '36-56-90', '34-77-91'] ... Read More

4K+ Views
To group Pandas dataframe, we use groupby(). To sort grouped dataframe in descending order, use sort_values(). The size() method is used to get the dataframe size.For descending order sort, use the following in sort_values() −ascending=FalseAt first, create a pandas dataframe −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], "Reg_Price": [1000, 1400, 1000, 900, 1700, 900] } )Next, group according to Reg_Price column and sort in descending order −dataFrame.groupby('Reg_Price').size().sort_values(ascending=False) ExampleFollowing is the codeimport pandas as pd # dataframe with one of the columns as Reg_Price ... Read More

983 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

191 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

604 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

637 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

632 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

687 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

146 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

246 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