Python Program to Join Equi-hierarchy Strings
Last Updated :
14 Mar, 2023
Given a list of strings with several hierarchies, the task is to write a python program to join those which have the same hierarchies.
Elements in the same dimension before a dimension change are known to be in the same hierarchy.
Input : test_list = ["gfg ", " best ", [" for ", " all "], " all ", [" CS " , " geeks "]]
Output : ['gfg best ', [' for all '], ' all ', [' CS geeks ']]
Explanation : Strings at similar hierarchy as joined.
Input : test_list = ["gfg ", " best ", [" for ", " all "], [" CS " , " geeks "]]
Output : ['gfg best ', [' for all '], [' CS geeks ']]
Explanation : Strings at similar hierarchy as joined.
Method 1 : Using type(), loop, recursion and join()
In this, the list element is checked to be string using type(), if found, the joining task is performed using join for that hierarchy. If element is found to be list, the inner list is recurred for similar logic for next level of hierarchy.
Example:
Python3
def hierjoin(test_list):
res = []
temp = []
val = None
for sub in test_list:
# if string then appended
if type(sub) == str:
temp.append(sub)
# if list, the string is joined for hierarchy
# recurred for inner list
else:
res.append(''.join(temp))
temp = []
val = hierjoin(sub)
res.append(val)
if temp != []:
res.append(''.join(temp))
return res
# initializing list
test_list = ["gfg ", " best ", [" for ", " all "],
" all ", [" CS ", " geeks "]]
# printing original list
print("The original list is : " + str(test_list))
# calling recursion
res = hierjoin(test_list)
# printing result
print("The joined strings : " + str(res))
Output:
The original list is : ['gfg ', ' best ', [' for ', ' all '], ' all ', [' CS ', ' geeks ']]
The joined strings : ['gfg best ', [' for all '], ' all ', [' CS geeks ']]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using join(), map(), recursion and groupby()
In this equihierarchy groups are formed using groupby(). Then map() is used to call the recursion function for inner hierarchy of list strings.
Example:
Python3
from itertools import groupby
def hierjoin(test_list):
# groups are formed for similar hierarchy using groupby
return [idx for x, y in groupby(test_list, key=str.__instancecheck__)
for idx in ([''.join(y)] if x else map(hierjoin, y))]
# initializing list
test_list = ["gfg ", " best ", [" for ", " all "],
" all ", [" CS ", " geeks "]]
# printing original list
print("The original list is : " + str(test_list))
# calling recursion
res = hierjoin(test_list)
# printing result
print("The joined strings : " + str(res))
Output:
The original list is : ['gfg ', ' best ', [' for ', ' all '], ' all ', [' CS ', ' geeks ']]
The joined strings : ['gfg best ', [' for all '], ' all ', [' CS geeks ']]
The Time and Space Complexity of all the methods is :
Time Complexity: O(n2)
Space Complexity: O(n)
Approach 3: Using isinstance() and recursion
Python3
# Method 3: Using list comprehensions
def hierjoin(test_list):
res = []
temp = []
for sub in test_list:
if type(sub) == str:
temp.append(sub)
else:
if temp:
res.append(''.join(temp))
temp = []
res.append(hierjoin(sub))
if temp:
res.append(''.join(temp))
return res
# Test
test_list = ["gfg ", " best ", [" for ", " all "], " all ", [" CS ", " geeks "]]
print(hierjoin(test_list))
#This code is contributed by Edula Vinay Kumar Reddy
Output['gfg best ', [' for all '], ' all ', [' CS geeks ']]
Time Complexity: O(n)
Auxiliary Space: O(n)
This approach uses the built-in function isinstance to check if an element is a string or not. The approach is the same as the first method, but using isinstance instead of type for checking the type of an element. The time and space complexity remains the same.
Similar Reads
Python program to Extract Mesh matching Strings Given a character mesh, containing missing characters, match the string which matches the mesh. Example: Input : test_list = ["geeks", "best", "peeks"], mesh = "_ee_s" Output : ['geeks', 'peeks'] Explanation : Elements according to mesh are geeks and peeks. Input : test_list = ["geeks", "best", "tes
4 min read
Python Program to Convert a List to String In Python, converting a list to a string is a common operation. In this article, we will explore the several methods to convert a list into a string. The most common method to convert a list of strings into a single string is by using join() method. Let's take an example about how to do it.Using the
3 min read
Python program to concatenate Strings around K Given List of Strings, join all the strings which occurs around string K. Input : test_list = ["Gfg", "*", "is", "best", "*", "love", "gfg"], K = "*" Output : ['Gfg*is', 'best*love', 'gfg'] Explanation : All elements around * are joined.Input : test_list = ["Gfg", "$", "is", "best", "$", "love", "gf
5 min read
Python program to Concatenate Kth index words of String Given a string with words, concatenate the Kth index of each word. Input : test_str = 'geeksforgeeks best geeks', K = 3 Output : ktk Explanation : 3rd index of "geeksforgeeks" is k, "best" has 't' as 3rd element. Input : test_str = 'geeksforgeeks best geeks', K = 0 Output : gbg Method #1 : Using joi
4 min read
Python - Join strings by multiple delimiters Given two strings, the task is to write a python program to join them by every delimiter from delimiter list. Input : test_str1 = 'Geeksforgeeks', test_str2 = "Best", join_list = ["+", "*", "-", "$", ",", "@"] Output : ['Geeksforgeeks+Best', 'Geeksforgeeks*Best', 'Geeksforgeeks-Best', 'Geeksforgeeks
4 min read