Python – Fill list characters in String
Given String and list, construct a string with only list values filled.
Input : test_str = “geeksforgeeks”, fill_list = [‘g’, ‘s’, ‘f’, k]
Output : g__ksf__g__ks
Explanation : All occurrences are filled in their position of g, s, f and k.Input : test_str = “geeksforgeeks”, fill_list = [‘g’, ‘s’]
Output : g___s___g___s
Explanation : All occurrences are filled in their position of g and s.
Method #1 : Using loop
This is brute force approach to this problem. In this, we iterate for all the elements in string, if it’s in list we fill it, else fill with “space” value.
Python3
# Python3 code to demonstrate working of # Fill list characters in String # Using loop # initializing string test_str = "geeksforgeeks" # printing original string print ( "The original string is : " + str (test_str)) # initializing fill list fill_list = [ 'g' , 's' , 'f' ] # loop to iterate through string res = "" for chr in test_str: # checking for presence if chr in fill_list: temp = chr else : temp = "_" res + = temp # printing result print ( "The string after filling values : " + str (res)) |
The original string is : geeksforgeeks The string after filling values : g___sf__g___s
Time complexity: O(n), where n is the length of the input string “test_str”. This is because the code iterates through the elements of “test_str” one by one in a single loop using list comprehension.
Auxiliary space: O(n), where n is the length of the input string “test_str”. This is because the code creates a list of the elements of “test_str” using the “list()” function, which takes up O(n) space. Additionally, the “res” string is also of size O(n), as it is created by concatenating the elements of the list comprehension.
Method #2 : Using join() + list comprehension
The combination of above functions can be used to solve this problem. In this, we formulate logic using list comprehension and join() is used to perform join of required values using conditions.
Python3
# Python3 code to demonstrate working of # Fill list characters in String # Using join() + list comprehension # initializing string test_str = "geeksforgeeks" # printing original string print ( "The original string is : " + str (test_str)) # initializing fill list fill_list = [ 'g' , 's' , 'f' ] # join() used to concatenate result # using conditionals in list comprehension res = " ".join([chr if chr in fill_list else " _" for chr in list (test_str)]) # printing result print ( "The string after filling values : " + str (res)) |
The original string is : geeksforgeeks The string after filling values : g___sf__g___s
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using in, not in operators and replace() method
Python3
# Python3 code to demonstrate working of # Fill list characters in String # initializing string test_str = "geeksforgeeks" # printing original string print ( "The original string is : " + str (test_str)) # initializing fill list fill_list = [ 'g' , 's' , 'f' ] # loop to iterate through string res = "" for i in test_str: if i not in fill_list: test_str = test_str.replace(i, "_" ) # printing result print ( "The string after filling values : " + str (test_str)) |
The original string is : geeksforgeeks The string after filling values : g___sf__g___s
Time complexity: O(n^2), where n is the length of the input string.
Auxiliary space: O(1),
Method #4 : Using regular expressions:
This Python code takes a string test_str and a list fill_list as inputs. The aim of the code is to replace all the characters in the string that are not present in the list with an underscore.
To do this, the code uses the sub() method from the re module which replaces all occurrences of a given pattern with a specified string. The pattern in this case is a regular expression that includes all the characters not present in the fill_list, and the specified string is an underscore. The join() method is used to concatenate all the characters in fill_list into a string that is used to construct the regular expression.
The resulting string with the replaced characters is stored in the result variable and is printed to the console using the print() function
Python3
# importing re module import re # initializing string test_str = "geeksforgeeks" # printing original string print ( "The original string is : " + str (test_str)) # initializing fill list fill_list = [ 'g' , 's' , 'f' ] # using sub() method of re module to fill the characters result = re.sub( "[^" + " ".join(fill_list) + " ] ", " _", test_str) # printing the result print ( "The string after filling values : " + str (result)) #This code is contributed by Jyothi pinjala. |
The original string is : geeksforgeeks The string after filling values : g___sf__g___s
Time complexity: O(n), n is the length of the input string
Auxiliary space: O(n), n is the length of the input string
The space complexity of this code is O(n), where n is the length of the fill_list array. This is because the “”.join(fill_list) operation creates a new string with length k, which is used in the regular expression passed to the re.sub() method. Additionally, the result variable holds a new string of length n, which is the result of the substitution.
Method#5:Using enumeration
Algorithm:
- Initialize an empty string to store the result.
- Convert the input string into a list of characters.
- For each character in the list, check if it is in the fill_list using enumeration.
- If the character is in the fill_list, append it to the result string.
- If the character is not in the fill_list, append an underscore to the result string.
- Return the result string.
Python3
#Python3 code to demonstrate working of #Fill list characters in String #Using join() + enumerate() #initializing string test_str = "geeksforgeeks" #printing original string print ( "The original string is : " + str (test_str)) #initializing fill list fill_list = [ 'g' , 's' , 'f' ] #using enumerate() and join() to concatenate result res = " ".join([chr if chr in fill_list else " _" for idx, chr in enumerate (test_str)]) #printing result print ( "The string after filling values : " + str (res)) #This code is contributed by Vinay Pinjala. |
The original string is : geeksforgeeks The string after filling values : g___sf__g___s
Time Complexity: O(n), where n is the length of the input string. We need to iterate over each character in the string.
Auxiliary Space: O(n), where n is the length of the input string. We need to store the result string, which can be as long as the input string.
Method #6:
Define the fill_list function: This step has a time complexity of O(1) and a space complexity of O(1), as we are only defining a function and not performing any operations.
Define the string to fill the list with characters: This step has a time complexity of O(1) and a space complexity of O(1), as we are only defining a string and not performing any operations.
Call the fill_list function to fill a list with the characters in the string: This step has a time complexity of O(n), where n is the length of the string, as we need to iterate over each character in the string to fill the list. The space complexity of this step is also O(n), as we need to create a new list to store the characters from the string.
Print the list: This step has a time complexity of O(n), as we need to iterate over each element in the list to print it. The space complexity of this step is also O(n), as we need to output each element in the list.
Python3
# define function to fill list with characters in string def fill_list(string): return list (string) # define string to fill list with characters string = "Hello, World!" # call function to fill list with characters in string char_list = fill_list(string) # print the list print (char_list) |
['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!']
Time complexity:O(n)
Auxiliary space:O(n)
Please Login to comment...