Articles on Trending Technologies

Technical articles with clear explanations and examples

Python Program – Convert String to matrix having K characters per row

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 874 Views

When it is required to convert a string into a matrix that has K characters per row, we can use string slicing and list comprehension. This technique is useful for formatting text data into fixed-width rows for display or processing. Method 1: Using String Slicing and List Comprehension This approach creates a matrix by slicing the string into chunks of K characters ? def convert_string_to_matrix(text, k): matrix = [] for i in range(0, len(text), k): row = list(text[i:i+k]) ...

Read More

Python – To convert a list of strings with a delimiter to a list of tuple

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 475 Views

Converting a list of strings with delimiters to a list of tuples is a common task in Python. This can be achieved using list comprehension combined with the split() method to break strings at delimiter positions. Basic Example Here's how to convert delimiter-separated strings into tuples of integers ? data_list = ["33-22", "13-44-81-39", "42-10-42", "36-56-90", "34-77-91"] print("Original list:") print(data_list) delimiter = "-" result = [tuple(int(element) for element in item.split(delimiter)) for item in data_list] print("Converted to tuples:") print(result) Original list: ['33-22', '13-44-81-39', '42-10-42', '36-56-90', '34-77-91'] Converted to tuples: [(33, 22), ...

Read More

Python – Descending Order Sort grouped Pandas dataframe by group size?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 4K+ Views

Sorting grouped Pandas DataFrame by group size in descending order is useful for analyzing data distribution. We use groupby() to group data, size() to count group members, and sort_values(ascending=False) to sort in descending order. Creating a Sample DataFrame Let's start by creating a DataFrame with car information ? import pandas as pd # Create dataframe with Car and Registration Price columns dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], "Reg_Price": [1000, 1400, 1000, 900, 1700, 900] }) print("DataFrame:") print(dataFrame) DataFrame: ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

When it is required to sort a given list of strings based on the numeric part of the string, we can use regular expressions with the sort() method's key parameter to extract and compare the numeric values. Example Below is a demonstration of sorting strings by their numeric parts ? import re def extract_numeric_part(string): return list(map(int, re.findall(r'\d+', string)))[0] strings = ["pyt23hon", "fu30n", "lea14rn", 'co00l', 'ob8uje3345t'] print("Original list:") print(strings) strings.sort(key=extract_numeric_part) print("Sorted list by numeric part:") print(strings) Original list: ['pyt23hon', 'fu30n', 'lea14rn', 'co00l', 'ob8uje3345t'] Sorted ...

Read More

Python – Substitute prefix part of List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 321 Views

When working with lists, you may need to substitute the prefix (beginning) part of one list with another list. Python provides a simple approach using list slicing with the : operator and the len() function. What is Prefix Substitution? Prefix substitution means replacing the first N elements of a list with elements from another list, where N is the length of the replacement list. Example Here's how to substitute the prefix part of a list ? # Define two lists original_list = [29, 77, 19, 44, 26, 18] replacement_list = [15, 44, 82] ...

Read More

Python - Merge DataFrames of different length

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 722 Views

To merge DataFrames of different lengths, we use the merge() method with different join types. The left join keeps all rows from the left DataFrame and matches rows from the right DataFrame where possible. Creating DataFrames of Different Lengths Let's create two DataFrames with different lengths to demonstrate merging ? import pandas as pd # Create DataFrame1 with length 4 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Jaguar'], "Price": [50000, 45000, 48000, 60000] }) print("DataFrame1 ...", dataFrame1) print("DataFrame1 length =", len(dataFrame1)) DataFrame1 ... ...

Read More

Python – Cross mapping of Two dictionary value lists

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 853 Views

When it is required to cross-map two dictionary valued lists, the setdefault and extend methods are used. This technique allows you to map values from one dictionary to another based on matching keys and indices. Understanding Cross Mapping Cross mapping involves using values from the first dictionary as keys to lookup corresponding values in the second dictionary. The result combines these lookups into a new dictionary structure. Example Below is a demonstration of cross mapping two dictionaries − my_dict_1 = {"Python" : [4, 7], "Fun" : [8, 6]} my_dict_2 = {6 : [5, 7], ...

Read More

Python – Check if elements index are equal for list elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 826 Views

When it is required to check if the index of elements matches their values or compare elements at the same positions across lists, we can use simple iteration with the enumerate() function. Basic Example: Check if Index Equals Value Here's how to check if any element's index equals its value ? data = [0, 2, 1, 3, 5] print("The list is:") print(data) # Check if any element equals its index index_equals_value = [] for index, element in enumerate(data): if index == element: ...

Read More

Python – Convert List to Index and Value dictionary

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 847 Views

When you need to convert a list into a dictionary containing separate index and value arrays, Python's enumerate() function provides an elegant solution. This approach creates a structured dictionary with index and values keys. Basic Example Here's how to convert a list to an index-value dictionary ? 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) ...

Read More

Python – Extend consecutive tuples

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 244 Views

When working with tuples in Python, you may need to extend consecutive tuples by combining each tuple with the next one in the sequence. This creates a new list where each element is the concatenation of two adjacent tuples. Example Below is a demonstration of extending consecutive tuples ? 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) - ...

Read More
Showing 3831–3840 of 61,299 articles
« Prev 1 382 383 384 385 386 6130 Next »
Advertisements