例如:
list1 = ['mory','alphabet','fruit']
list2 = ['apple','banana','pear']
string = " There was a mory and the thing was a alphabet within the fruit. There were still fruit. But the banana was still there."
現在我想要做的是將單詞的每個外觀list1從其等效的 in更改為list2. 例如,“There was a mory”將更改為“There was a apple”。
我想使用 for 回圈來做到這一點,但是我不知道如何多次運行串列來更改字串的每個部分。
uj5u.com熱心網友回復:
你可以:
- 在要替換的單詞和它們的替換詞之間創建一個字典映射,使用
zip() - 用于
.split()獲取原始文本中的單詞(拆分為單個句子,然后拆分為單個單詞),以及 - 遍歷這些詞并找到它們的替代品(如果它們存在):
list1 = ['mory','alphabet','fruit']
list2 = ['apple','banana','pear']
words_to_replace = {to_replace: replacement for to_replace, replacement in zip(list1, list2)}
string = ("There was a mory and the thing was a alphabet within the fruit. "
"There were still fruit. But the banana was still there.")
sentences = string.split('. ')
original_words = [sentence.split(' ') for sentence in sentences]
result_sentences = []
for sentence in sentences:
replacement_sentence_words = []
for word in sentence.split(' '):
if word in words_to_replace:
replacement_sentence_words.append(words_to_replace[word])
else:
replacement_sentence_words.append(word)
result_sentences.append(' '.join(replacement_sentence_words))
result = '. '.join(result_sentences)
print(result)
這列印:
There was a apple and the thing was a banana within the pear. There were still pear. But the banana was still there.
順便說一句,您不應該重復.replace()使用此任務。考慮以下:
list1 = ['mory','apple']
list2 = ['apple','banana']
data = 'mory apple'
for word, replacement in zip(list1, list2):
data = data.replace(word, replacement)
print(data)
這將輸出banana banana,即使mory映射到apple.
uj5u.com熱心網友回復:
有3種方法可以解決您的問題
最基本的方法:
list1 = ['mory','alphabet','fruit']
list2 = ['apple','banana','pear']
string = "There was a mory and the thing was a alphabet within the fruit. "
for i in range(len(list1)):
string = string.replace(list1[i], list2[i]
在這里,我只是for loop在 list1 的長度范圍內使用,并將 list1 的索引 i 替換為 list2 的索引 i。
使用字典
list1 = ['mory','alphabet','fruit']
list2 = ['apple','banana','pear']
string = "There was a mory and the thing was a alphabet within the fruit. "
List_dict= {j:list2[i] for i,j in enumerate(list1)}
for i in list1:
string=string.replace(i, List_dict.get(i)
我們剛剛讀取了 list1 的元素并使用enumerate了函式來索引。然后我們使用 adict將 list1 的元素關聯到 list2 的元素。之后,我們使用.replace函式將 list1 的值替換為 list2 的值。
使用串列:
list1 = ['mory','alphabet','fruit']
list2 = ['apple','banana','pear']
string = "There was a mory and the thing was a alphabet within the fruit. "
List_3= [j, list2[i] for i,j in enumerate (lists)]
for i in List_3:
String=string.replace(i[0],i[1])
字典方法的相同概念在這里,不同之處在于我們使用字典理解的第一種方法,您也可以使用普通for loop來實作它。
解決這個問題的方法還有很多,但我提到的方法是主要的或基本的方法。
uj5u.com熱心網友回復:
你可以試試這個,我是從一些資源編譯的。我在下面放的資源作為參考。
list1 = ['mory','alphabet','fruit']
list2 = ['apple','banana','pear']
string = "There was a mory and the thing was a alphabet within the fruit. There were still fruit. But the banana was still there."
for i in range(len(list1)):
string = string.replace(list1[i], list2[i])
# Output
# There was a apple and the thing was a banana within the pear. There were still pear. But the banana was still there.
參考 :
- 替換字串中的特定單詞(Python)
- 回圈
uj5u.com熱心網友回復:
我認為這個簡潔的代碼解決了這個問題:
string = " There was a mory and the thing was a alphabet within the fruit. There were still fruit. But the banana was still there."
list1 = ['mory','alphabet','fruit']
list2 = ['apple','banana','pear']
for i in range(len(list1)):
while string.find(list1[i]) != -1:
index = string.find(list1[i])
string = string[:index] list2[i] string[(index len(list1[i])):]
print(string)
輸出:
There was a apple and the thing was a banana within the pear. There were still pear. But the banana was still there.
這段代碼的作業原理是,對于 in 中的每個值list1,當該元素存在于其中時string,我們將其替換為 in 中對應的值list2。
但是,如果您想使用內置函式來更輕松地替換,則可以使用:
for i in range(len(list1)):
string = string.replace(list1[i],list2[i])
我希望這有幫助!如果您需要任何其他說明或詳細資訊,請告訴我:)
uj5u.com熱心網友回復:
嘗試這個:
from functools import reduce
original_values = ["mory", "alphabet", "fruit"]
updated_values = ["apple", "banana", "pear"]
original_sentence = "There was a mory and the thing was a alphabet within the fruit. There were still fruit. But the banana was still there."
updated_sentence = reduce(lambda x, y: x.replace(y[0], y[1]), zip(original_values, updated_values), original_sentence)
print(updated_sentence)
functools.reduce 的檔案:https ://docs.python.org/3/library/functools.html#functools.reduce
str.replace 的檔案:https ://docs.python.org/3/library/stdtypes.html#str.replace
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/433025.html
上一篇:只想在第二個/內部for回圈中使用join命令,以使所有作者都在一個單元格中
下一篇:對for回圈運行x次分析
