Found 26504 Articles for Server Side Programming

Python program to find the String in a List

AmitDiwan
Updated on 16-Sep-2021 08:40:11

522 Views

When it is required to find the string in a list, a simple ‘if’ condition along with ‘in’ operator can be used.ExampleBelow is a demonstration of the samemy_list = [4, 3.0, 'python', 'is', 'fun'] print("The list is :") print(my_list) key = 'fun' print("The key is :") print(key) print("The result is :") if key in my_list: print("The key is present in the list") else: print("The key is not present in the list")OutputThe list is : [4, 3.0, 'python', 'is', 'fun'] The key is : fun The result is : The key is present ... Read More

Python program for most frequent word in Strings List

AmitDiwan
Updated on 16-Sep-2021 08:38:16

5K+ Views

When it is required to find the most frequent word in a list of strings, the list is iterated over and the ‘max’ method is used to get the count of the highest string.ExampleBelow is a demonstration of the samefrom collections import defaultdict my_list = ["python is best for coders", "python is fun", "python is easy to learn"] print("The list is :") print(my_list) my_temp = defaultdict(int) for sub in my_list: for word in sub.split(): my_temp[word] += 1 result = max(my_temp, key=my_temp.get) print("The word that has the ... Read More

Python program to get maximum of each key Dictionary List

AmitDiwan
Updated on 16-Sep-2021 08:35:40

238 Views

When it is required to get the maximum of each key in a list of dictionary elements, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [{"Hi": 18, "there": 13, "Will": 89}, {"Hi": 53, "there": 190, "Will": 87}] print("The list is : ") print(my_list) my_result = {} for elem in my_list: for key, val in elem.items(): if key in my_result: my_result[key] = max(my_result[key], val) else: ... Read More

Python program to count the pairs of reverse strings

AmitDiwan
Updated on 16-Sep-2021 08:33:41

172 Views

When it is required to count the pairs of reverse strings, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [{"Python": 8, "is": 1, "fun": 9}, {"Python": 2, "is": 9, "fun": 1}, {"Python": 5, "is": 10, "fun": 7}] print("The list is :") print(my_list) result = {} for dic in my_list: for key, value in dic.items(): if key in result: result[key] = max(result[key], value) else: ... Read More

Python program to print elements which are multiples of elements given in a list

AmitDiwan
Updated on 16-Sep-2021 08:31:21

330 Views

When it is required to print the elements which are multiples of elements given in a list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [45, 67, 89, 90, 10, 98, 10, 12, 23] print("The list is :") print(my_list) my_division_list = [6, 4] print("The division list is :") print(my_division_list) my_result = [element for element in my_list if all(element % j == 0 for j in my_division_list)] print("The result is :") print(my_result)OutputThe list is : [45, 67, 89, 90, 10, 98, 10, 12, 23] The division list is : [6, 4] The result ... Read More

Python - Round number of places after the decimal for column values in a Pandas DataFrame

AmitDiwan
Updated on 16-Sep-2021 08:41:36

500 Views

To round number of places after the decimal, use the display.precision attribute of the Pandas.At first, import the required Pandas library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame(    { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000] } ) Use the set_option() method to set precision. We have set precision value 2 −pd.set_option('display.precision', 2)ExampleFollowing is the complete code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( ... Read More

Python Program to print strings based on the list of prefix

AmitDiwan
Updated on 16-Sep-2021 08:29:27

405 Views

When it is required to print strings based on the list of prefix elements, a list comprehension, the ‘any’ operator and the ‘startswith’ method are used.ExampleBelow is a demonstration of the samemy_list = ["streek", "greet", "meet", "leeks", "mean"] print("The list is : ") print(my_list) prefix_list = ["st", "ge", "me", "re"] print("The prefix list is : ") print(prefix_list) my_result = [element for element in my_list if any(element.startswith(ele) for ele in prefix_list)] print("The result is :") print(my_result)OutputThe list is : ['streek', 'greet', 'meet', 'leeks', 'mean'] The prefix list is : ['st', 'ge', 'me', 're'] The result is : ... Read More

Python Pandas - Convert Nested Dictionary to Multiindex Dataframe

AmitDiwan
Updated on 16-Sep-2021 08:33:18

1K+ Views

At first, let us create a Nested Dictionary −dictNested = {'Cricket': {'Boards': ['BCCI', 'CA', 'ECB'], 'Country': ['India', 'Australia', 'England']}, 'Football': {'Boards': ['TFA', 'TCSA', 'GFA'], 'Country': ['England', 'Canada', 'Germany'] }}Now, create an Empty Dictionary −new_dict = {}Now, loop to assign values −for outerKey, innerDict in dictNested.items(): for innerKey, values in innerDict.items(): new_dict[(outerKey, innerKey)] = values Convert to Multi-index DataFrame −pd.DataFrame(new_dict)ExampleFollowing is the code −import pandas as pd # Create Nested dictionary dictNested = {'Cricket': {'Boards': ['BCCI', 'CA', 'ECB'], 'Country': ['India', 'Australia', 'England']}, 'Football': {'Boards': ['TFA', 'TCSA', 'GFA'], ... Read More

Python prorgam to remove duplicate elements index from other list

AmitDiwan
Updated on 16-Sep-2021 08:26:14

263 Views

When it is required to remove duplicate elements index from other list, the ‘enumerate’ attribute, the list comprehension and a simple iteration are used.ExampleBelow is a demonstration of the samemy_list_1 = [4, 5, 6, 5, 4, 7, 8, 6] my_list_2 = [1, 7, 6, 4, 7, 9, 10, 11] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) temp_set = set() temp = [] for index, value in enumerate(my_list_1): if value not in temp_set: temp_set.add(value) else: ... Read More

Python Program to remove a specific digit from every element of the list

AmitDiwan
Updated on 16-Sep-2021 08:22:09

537 Views

When it is required to remove specific digit from every element of the list, an iteration and ‘set’ operator and ‘str’ methods are used.ExampleBelow is a demonstration of the samemy_list = [123, 565, 1948, 334, 4598] print("The list is :") print(my_list) key = 3 print("The key is :") print(key) my_result = [] for element in my_list: if list(set(str(element)))[0] == str(key) and len(set(str(element))) == 1: my_result.append('') else: my_result.append(int(''.join([element_1 for element_1 in str(element) if int(element_1) != key]))) print("The result ... Read More

Advertisements