Python | Joining only adjacent words in list
Last Updated :
14 Mar, 2023
Sometimes, more than one type of data can come in Python list and sometimes it’s undesirably tokenized and hence we require to join the words that have been tokenized and leave the digits as they are. Let’s discuss certain ways in which this task can be achieved. Method #1 : Using list comprehension + “*” operator This task can be performed using the list comprehension, first by joining the words and then joining the digits and then separating only the numbers, while joining to form the resultant string.
Python3
test_list = [ 'Geeks' , '5' , 'for' , '9' , 'Geeks' , '2' , '5' ]
print ("The original list : " + str (test_list))
res = [''.join([i for i in test_list if not i.isdigit()]),
* [j for j in test_list if j.isdigit()]]
print ("The joined adjacent word list (ignoring digits) : " + str (res))
|
Time Complexity: O(n)
Space Complexity: O(n)
Output:
The original list : [‘Geeks’, ‘5’, ‘for’, ‘9’, ‘Geeks’, ‘2’, ‘5’] The joined adjacent word list(ignoring digits) : [‘GeeksforGeeks’, ‘5’, ‘9’, ‘2’, ‘5’]
Method #2 : Using itertools.chain.from_iterable() + groupby() + join() This task can also be performed using the groupby function which groups the digits together and then from_iterables function joins the list and characters together joined by the join function.
Python3
from itertools import chain, groupby
test_list = [ 'Geeks' , '5' , 'for' , 'Geeks' , '2' , '3' ]
print ("The original list : " + str (test_list))
num_group = groupby(test_list, key = str .isalpha)
both_group = [[''.join(i)] if j else list (i)
for j, i in num_group]
res = list (chain.from_iterable(both_group))
print ("The joined adjacent word list (ignoring digits) : "
+ str (res))
|
Time Complexity: O(nlogn)
Space Complexity: O(n)
Output:
The original list : [‘Geeks’, ‘5’, ‘for’, ‘Geeks’, ‘2’, ‘3’] The joined adjacent word list(ignoring digits) : [‘Geeks’, ‘5’, ‘forGeeks’, ‘2’, ‘3’]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...