When it is required to convert a list to a set based on a specific common element, a method can be defined that iterates through the set using ‘enumerate’ and places a specific condition on the elements. The ‘union’ method and the ‘map’ methods are used.ExampleBelow is a demonstration of the samedef common_elem_set(my_set): for index, val in enumerate(my_set): for j, k in enumerate(my_set[index + 1:], index + 1): if val & k: my_set[index] = ... Read More
When it is required to convert a list into a list of lists using a step value, a method is defined that uses a simple iteration, the ‘split’ method and the ‘append’ method.ExampleBelow is a demonstration of the samedef convert_my_list(my_list): my_result = [] for el in my_list: sub = el.split(', ') my_result.append(sub) return(my_result) my_list = ['peter', 'king', 'charlie'] print("The list is :") print(my_list) print("The resultant list is :") print(convert_my_list(my_list))OutputThe list is : ['peter', 'king', 'charlie'] The ... Read More
When it is required to find the cube of each list element, a simple iteration and the ‘append’ method are used.ExampleBelow is a demonstration of the samemy_list = [45, 31, 22, 48, 59, 99, 0] print("The list is :") print(my_list) my_result = [] for i in my_list: my_result.append(i*i*i) print("The resultant list is :") print(my_result)OutputThe list is : [45, 31, 22, 48, 59, 99, 0] The resultant list is : [91125, 29791, 10648, 110592, 205379, 970299, 0]ExplanationA list is defined and is displayed on the console.An empty list is defined.The original list is iterated over.Every element ... Read More
When it is required to print all the words occurring in a sentence exactly K times, a method is defined that uses the ‘split’ method, ‘remove’ method and the ‘count’ methods. The method is called by passing the required parameters and output is displayed.ExampleBelow is a demonstration of the samedef key_freq_words(my_string, K): my_list = list(my_string.split(" ")) for i in my_list: if my_list.count(i) == K: print(i) my_list.remove(i) my_string = "hi there how are you, how are u" K = 2 print("The string is :") print(my_string) print"The repeated ... Read More
When it is required to customize the space size padding in a list of strings, an empty list, an iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = ["Python", "is", "great"] print("The list is :") print(my_list) lead_size = 3 trail_size = 2 my_result = [] for elem in my_list: my_result.append((lead_size * ' ') + elem + (trail_size * ' ')) print("The result is :") print(my_result)OutputThe list is : ['Python', 'is', 'great'] The result is : [' Python ', ' is ', ' great ']ExplanationA list is defined and ... Read More
When it is required to find the first occurrence of one list in another list, the ‘set’ attribute and the ‘next’ method is used.ExampleBelow is a demonstration of the samemy_list_1 = [23, 64, 34, 77, 89, 9, 21] my_list_2 = [64, 10, 18, 11, 0, 21] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_list_2 = set(my_list_2) my_result = next((ele for ele in my_list_1 if ele in my_list_2), None) print("The result is :") print(my_result)OutputThe first list is : [23, 64, 34, 77, 89, 9, 21] The second list is : [64, 10, 18, ... Read More
To merge Pandas DataFrame, use the merge() function. The inner join is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −how = “inner”At first, let us import the pandas library with an alias −import pandas as pd Create DataFrame1 −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) Now, create DataFrame2 −dataFrame2 = pd.DataFrame( { ... Read More
To calculate the variance of column values, use the var() method. At first, import the required Pandas library −import pandas as pdCreate a DataFrame with two columns −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) Finding Variance of "Units" column values using var() function −print"Variance of Units column from DataFrame1 = ", dataFrame1['Units'].var()In the same way, we have calculated the Variance from the 2nd DataFrame.ExampleFollowing is the complete code −import pandas as pd ... Read More
To reset index after group by, at first group according to a column using groupby(). After that, use reset_index().At first, import the required library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"], "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350] } ) Group according to Car column −resDF = dataFrame.groupby("Car").mean()Now, reset index after grouping −resDF.reset_index() ExampleFollowing is the code − import pandas as ... Read More
A protocol is a set of rules and standards that primarily outline a language that devices will use to communicate. There are an excellent range of protocols in use extensively in networking, and that they are usually implemented in numerous layers.It provides a communication service where the process is used to exchange the messages. When the communication is simple, we can use only one simple protocol.When the communication is complex, we must divide the task between different layers, so, we need to follow a protocol at each layer, this technique we used to call protocol layering. This layering allows us ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP