Python|仅连接列表中的相邻单词
有时,不止一种类型的数据可以出现在Python列表中,有时它被不希望地标记化,因此我们需要加入已经标记化的单词并保持数字不变。让我们讨论一些可以实现此任务的方法。
方法#1:使用列表理解+ "*" operator
可以使用列表推导来执行此任务,首先通过连接单词,然后连接数字,然后仅分隔数字,同时连接以形成结果字符串。
# Python3 code to demonstrate
# joining only adjacent words in list
# list comprehension + "*" operator
# initializing list
test_list = ['Geeks', '5', 'for', '9', 'Geeks' , '2', '5']
# printing original list
print("The original list : " + str(test_list))
# using list comprehension + "*" operator
# joining only adjacent words in list
res = [''.join([i for i in test_list if not i.isdigit()]),
*[j for j in test_list if j.isdigit()]]
# print result
print("The joined adjacent word list(ignoring digits) : " + str(res))
输出:
The original list : [‘Geeks’, ‘5’, ‘for’, ‘9’, ‘Geeks’, ‘2’, ‘5’]
The joined adjacent word list(ignoring digits) : [‘GeeksforGeeks’, ‘5’, ‘9’, ‘2’, ‘5’]
方法#2:使用itertools.chain.from_iterable() + groupby() + join()
也可以使用 groupby函数执行此任务,该函数将数字组合在一起,然后from_iterables
函数将列表和字符连接在一起,由 join函数连接。
# Python3 code to demonstrate
# joining only adjacent words in list
# itertools.chain.from_iterable() + groupby() + join()
from itertools import chain, groupby
# initializing list
test_list = ['Geeks', '5', 'for', 'Geeks' , '2', '3']
# printing original list
print("The original list : " + str(test_list))
# using itertools.chain.from_iterable() + groupby() + join()
# joining only adjacent words in 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 result
print("The joined adjacent word list(ignoring digits) : "
+ str(res))
输出:
The original list : [‘Geeks’, ‘5’, ‘for’, ‘Geeks’, ‘2’, ‘3’]
The joined adjacent word list(ignoring digits) : [‘Geeks’, ‘5’, ‘forGeeks’, ‘2’, ‘3’]