Python | Custom Consecutive Character Pairing
Sometimes, while working with Python Strings, we can have problem in which we need to perform the pairing of consecutive strings with deliminator. This can have application in many domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using join() + list comprehension The combination of above functions can be used to perform this task. In this, we perform the task of joining the characters using join() and perform the compilation using list comprehension.
Python3
import string
test_str = 'geeksforgeeks'
print ("The original string is : " + test_str)
delim = '_'
res = [delim.join(test_str[idx : idx + 2 ]) for idx in range ( len (test_str) - 1 )]
print ("The List of joined Characters : " + str (res))
|
Output :
The original string is : geeksforgeeks
The List of joined Characters : ['g_e', 'e_e', 'e_k', 'k_s', 's_f', 'f_o', 'o_r', 'r_g', 'g_e', 'e_e', 'e_k', 'k_s']
Method #2 : Using windowed() + loop This is one of the method to solve this problem. In this task of forming pairs is done using windowed(). You need to install more_itertools module externally for its execution.
Python3
import more_itertools
test_str = 'geeksforgeeks'
print ("The original string is : " + test_str)
delim = '_'
res = []
for ele in more_itertools.windowed(test_str, 2 ):
res.append(ele[ 0 ] + delim + ele[ 1 ])
print ("The List of joined Characters : " + str (res))
|
Output :
The original string is : geeksforgeeks
The List of joined Characters : ['g_e', 'e_e', 'e_k', 'k_s', 's_f', 'f_o', 'o_r', 'r_g', 'g_e', 'e_e', 'e_k', 'k_s']
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Another approach to perform Custom Consecutive Character Pairing
Using zip() function
Zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.
Python3
def custom_pairing(test_str, delim):
result = [delim.join(pair) for pair in zip (test_str, test_str[ 1 :])]
return result
test_str = 'geeksforgeeks'
delim = '_'
print (custom_pairing(test_str, delim))
|
Output
['g_e', 'e_e', 'e_k', 'k_s', 's_f', 'f_o', 'o_r', 'r_g', 'g_e', 'e_e', 'e_k', 'k_s']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #8: Using a generator function and str.join()
step-by-step breakdown of the program:
- Define the input string test_str as ‘geeksforgeeks’.
- Print the original string by concatenating the string ‘The original string is : ‘ and test_str.
- Define the delimiter as ‘_’.
- Define a generator function consecutive_pairs that takes in a string string and yields consecutive character pairs in the form of tuples. This is done by iterating over the range of the length of the string minus one, and yielding a tuple of the current character and the next character in the string using slicing.
Call the consecutive_pairs function with test_str as the argument and pass the resulting generator to a list comprehension that creates a list of strings where each element is a pair of consecutive characters joined by the delimiter. This is done by calling ”.join(pair) on each pair of characters in the generator and concatenating the delimiter between them.
- Assign the resulting list to the variable res.
- Print the list of joined characters by concatenating the string ‘The List of joined Characters : ‘ and str(res).
Python3
test_str = 'geeksforgeeks'
print ( "The original string is : " + test_str)
delim = '_'
def consecutive_pairs(string):
for i in range ( len (string) - 1 ):
yield string[i:i + 2 ]
res = [delim.join(pair) for pair in consecutive_pairs(test_str)]
print ( "The List of joined Characters : " + str (res))
|
Output
The original string is : geeksforgeeks
The List of joined Characters : ['g_e', 'e_e', 'e_k', 'k_s', 's_f', 'f_o', 'o_r', 'r_g', 'g_e', 'e_e', 'e_k', 'k_s']
Time complexity: O(n)
Auxiliary space: O(n), where n is the length of the input string.
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...