Python – 替换字符串列表中的子字符串
有时在处理数据时,我们可能会遇到一个问题,即我们需要用映射的字符串替换子字符串以形成某些术语的简短形式。这类问题可以在涉及数据的许多领域中应用。让我们讨论可以执行此任务的某些方式。
方法 #1:使用循环 + 替换() + 枚举()
上述功能的组合可用于执行此任务。在此,我们使用 loop 和 enumerate() 执行迭代任务,并使用 replace() 完成更短形式的替换。
Python3
# Python3 code to demonstrate
# Replace Substrings from String List
# using loop + replace() + enumerate()
# Initializing list1
test_list1 = ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science']
test_list2 = [['Geeks', 'Gks'], ['And', '&'], ['Computer', 'Comp']]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Replace Substrings from String List
# using loop + replace() + enumerate()
sub = dict(test_list2)
for key, val in sub.items():
for idx, ele in enumerate(test_list1):
if key in ele:
test_list1[idx] = ele.replace(key, val)
# printing result
print ("The list after replacement : " + str(test_list1))
Python3
# Python3 code to demonstrate
# Replace Substrings from String List
# using replace() + list comprehension
# Initializing list1
test_list1 = ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science']
test_list2 = [['Geeks', 'Gks'], ['And', '&'], ['Computer', 'Comp']]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Replace Substrings from String List
# using replace() + list comprehension
res = [sub.replace(sub2[0], sub2[1]) for sub in test_list1
for sub2 in test_list2 if sub2[0] in sub]
# printing result
print ("The list after replacement : " + str(res))
输出 :
The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science']
The original list 2 is : [['Geeks', 'Gks'], ['And', '&'], ['Computer', 'Comp']]
The list after replacement : ['GksforGks', 'is', 'Best', 'For', 'Gks', '&', 'Comp Science']
方法 #2:使用 replace() + 列表推导
这是可以执行此任务的另一种方式。在此,我们使用 replace() 执行替换任务,其余任务使用列表推导执行。它删除没有替换的列表。
Python3
# Python3 code to demonstrate
# Replace Substrings from String List
# using replace() + list comprehension
# Initializing list1
test_list1 = ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science']
test_list2 = [['Geeks', 'Gks'], ['And', '&'], ['Computer', 'Comp']]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Replace Substrings from String List
# using replace() + list comprehension
res = [sub.replace(sub2[0], sub2[1]) for sub in test_list1
for sub2 in test_list2 if sub2[0] in sub]
# printing result
print ("The list after replacement : " + str(res))
输出 :
The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science']
The original list 2 is : [['Geeks', 'Gks'], ['And', '&'], ['Computer', 'Comp']]
The list after replacement : ['GksforGks', 'Gks', '&', 'Comp Science']