Python Articles

Page 735 of 855

Python - Check if two strings are isomorphic in nature

AmitDiwan
AmitDiwan
Updated on 20-Sep-2021 467 Views

When it is required to check if two strings are isomorphic in nature, a method is defined that takes two strings as parameters. It iterates through the length of the string, and converts a character into an integer using the ‘ord’ method.ExampleBelow is a demonstration of the sameMAX_CHARS = 256 def check_isomorphic(str_1, str_2):    len_1 = len(str_1)    len_2 = len(str_2)    if len_1 != len_2:       return False    marked = [False] * MAX_CHARS    map = [-1] * MAX_CHARS    for i in range(len_2):       if map[ord(str_1[i])] == -1: ...

Read More

Python Pandas – Remove numbers from string in a DataFrame column

AmitDiwan
AmitDiwan
Updated on 20-Sep-2021 3K+ Views

To remove numbers from string, we can use replace() method and simply replace. Let us first import the require library −import pandas as pdCreate DataFrame with student records. The Id column is having string with numbers −dataFrame = pd.DataFrame(    {       "Id": ['S01', 'S02', 'S03', 'S04', 'S05', 'S06', 'S07'], "Name": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } )Remove number from strings of a specific column i.e. “Id” here −dataFrame['Id'] = dataFrame['Id'].str.replace('\d+', '') ExampleFollowing is the code −import pandas as pd # Create DataFrame with ...

Read More

Python - Check if a number and its double exists in an array

AmitDiwan
AmitDiwan
Updated on 20-Sep-2021 642 Views

When it is required to check if a number and its double exists in an array, it is iterated over, and multiple with 2 and checked.ExampleBelow is a demonstration of the samedef check_double_exists(my_list): for i in range(len(my_list)): for j in (my_list[:i]+my_list[i+1:]): if 2*my_list[i] == j: print("The double exists") my_list = [67, 34, 89, 67, 90, 17, 23] print("The list is :") print(my_list) check_double_exists(my_list)OutputThe list is : [67, 34, 89, ...

Read More

Python - Find the number of prime numbers within a given range of numbers

AmitDiwan
AmitDiwan
Updated on 20-Sep-2021 3K+ Views

When it is required to find the prime numbers within a given range of numbers, the range is entered and it is iterated over. The ‘%’ modulus operator is used to find the prime numbers.ExampleBelow is a demonstration of the samelower_range = 670 upper_range = 699 print("The lower and upper range are :") print(lower_range, upper_range) print("The prime numbers between", lower_range, "and", upper_range, "are:") for num in range(lower_range, upper_range + 1): if num > 1: for i in range(2, num): if (num % ...

Read More

Python Pandas - Display unique values present in each column

AmitDiwan
AmitDiwan
Updated on 20-Sep-2021 607 Views

To display unique values in each column, use the unique() method and set the column within it. At first, import the required library −import pandas as pdCreate a DataFrame with two columns and duplicate records −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } )Find unique values by setting each column in the unique() method −resStudent = pd.unique(dataFrame.Student) resResult = pd.unique(dataFrame.Result)ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame(    {       ...

Read More

Python - Given an integer list, find the third maximum number if it exists

AmitDiwan
AmitDiwan
Updated on 20-Sep-2021 453 Views

When it is required to find the third maximum in a list of integers, a method is defined that takes a list as a parameter. It initializes a list of floating point numbers to infinity. The values in the list are iterated over, and compared with infinite values. Depending on the result, the output is displayed on the console.ExampleBelow is a demonstration of the samedef third_max_num(my_num): my_result = [float('-inf'), float('-inf'), float('-inf')] for num in my_num: if num not in my_result: ...

Read More

Python - Find the length of the last word in a string

AmitDiwan
AmitDiwan
Updated on 20-Sep-2021 2K+ Views

When it is required to find the length of the last word in a string, a method is defined that removes the extra empty spaces in a string, and iterates through the string. It iterates until the last word has been found. Then, its length is found and returned as output.ExampleBelow is a demonstration of the samedef last_word_length(my_string): init_val = 0 processed_str = my_string.strip() for i in range(len(processed_str)): if processed_str[i] == " ": init_val ...

Read More

Python Program to accept string starting with vowel

AmitDiwan
AmitDiwan
Updated on 20-Sep-2021 2K+ Views

When it is required to accept a string that starts with a vowel, a ‘startswith’ function is used to check if the string begins with a specific character (vowel) or not.ExampleBelow is a demonstration of the samemy_list = ["Hi", "there", "how", "are", "u", "doing"] print("The list is : ") print(my_list) my_result = [] vowel = "aeiou" for sub in my_list: flag = False for letter in vowel: if sub.startswith(letter): flag = True ...

Read More

Python - Find words greater than given length

AmitDiwan
AmitDiwan
Updated on 20-Sep-2021 1K+ Views

When it is required to find words that are greater than a specific length, a method is defined that splits the string, and iterates through it. It checks the length of the word and compares it with the given length. If they match, it is returned as output.ExampleBelow is a demonstration of the samedef string_check(string_length, my_string):    result_string = []    words = my_string.split(" ")    for x in words:       if len(x) > string_length:          result_string.append(x)    return result_string string_length = 3 my_string ="Python is always fun to learn" print("The string is :") ...

Read More

Python Program to check if String contains only Defined Characters using Regex

AmitDiwan
AmitDiwan
Updated on 20-Sep-2021 550 Views

When it is required to check if a given string contains specific characters using regular expression, a regular expression pattern is defined, and the string is subjected to follow this pattern.ExampleBelow is a demonstration of the sameimport re def check_string(my_string, regex_pattern): if re.search(regex_pattern, my_string): print("The string contains the defined characters only") else: print("The doesnot string contain the defined characters") regex_pattern = re.compile('^[Python]+$') my_string_1 = 'Python' print("The string is :") print(my_string_1) check_string(my_string_1 , regex_pattern) my_string_2 = 'PythonInterpreter' print("The string ...

Read More
Showing 7341–7350 of 8,549 articles
« Prev 1 733 734 735 736 737 855 Next »
Advertisements