【发布时间】:2019-11-06 22:45:30
【问题描述】:
给定两个列表,我想比较 list1 和 list2,并在 list1 中替换它,并添加括号。
str1 = "red man juice"
str2 = "the red man drank the juice"
one_lst = ['red','man','juice']
lst1 = ['the','red','man','drank','the','juice']
预期输出:
lst1 = ['the','[red]','[man]','drank','the','[juice]']
到目前为止我尝试了什么:
lst1 = list(str1.split())
for i in range(0,len(lst1)):
for j in range(0,len(one_lst)):
if one_lst[j] == lst1[i]:
str1 = str1.replace(lst1[i],'{}').format(*one_lst)
lst1 = list(str1.split())
print(lst1)
我可以替换它,但只是没有括号。
感谢您的帮助!
【问题讨论】:
-
您需要检查订单吗?如果
str1是"man juice red",结果会是什么? -
它将始终按时间顺序通过主通道。
标签: python string list replace