Open In App

Python | Custom Consecutive Character Pairing

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Custom Consecutive Character Pairing
# Using join() + list comprehension
import string
 
# initializing string
test_str = 'geeksforgeeks'
 
# printing original string
print("The original string is : " + test_str)
 
# initializing Delim
delim = '_'
 
# Custom Consecutive Character Pairing
# Using join() + list comprehension
res = [delim.join(test_str[idx : idx + 2]) for idx in range(len(test_str) - 1)]
 
# printing result
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




# Python3 code to demonstrate working of
# Custom Consecutive Character Pairing
# Using windowed() + loop
import more_itertools
 
# initializing string
test_str = 'geeksforgeeks'
 
# printing original string
print("The original string is : " + test_str)
 
# initializing Delim
delim = '_'
 
# Custom Consecutive Character Pairing
# Using windowed() + loop
res = []
for ele in more_itertools.windowed(test_str, 2):
    res.append(ele[0] + delim + ele[1])
 
# printing result
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):
# zipping the consecutive elements of the string and join them using the deliminator
  result = [delim.join(pair) for pair in zip(test_str, test_str[1:])]
  return result
 
#test
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:

  1. Define the input string test_str as ‘geeksforgeeks’.
  2. Print the original string by concatenating the string ‘The original string is : ‘ and test_str.
  3. Define the delimiter as ‘_’.
  4. 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.
  5. Assign the resulting list to the variable res.
  6. Print the list of joined characters by concatenating the string ‘The List of joined Characters : ‘ and str(res).

Python3




# Python3 code to demonstrate working of
# Custom Consecutive Character Pairing
# Using a generator function and str.join()
 
# initializing string
test_str = 'geeksforgeeks'
 
# printing original string
print("The original string is : " + test_str)
 
# initializing Delim
delim = '_'
 
# generator function for consecutive character pairs
def consecutive_pairs(string):
    for i in range(len(string)-1):
        yield string[i:i+2]
 
# Custom Consecutive Character Pairing
res = [delim.join(pair) for pair in consecutive_pairs(test_str)]
 
# printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads