Ich habe eine Liste von Listen wie folgt.Kombinieren Sie mehrere aufeinanderfolgende Wörter in Python
mycookbook= [["i", "love", "tim", "tam", "and", "chocolate", "ice", "cream"], ["cooking",
"fresh", "vegetables", "is", "easy"], ["fresh", "vegetables", "and", "fruits", "are", "good",
"for", "health"]]
Ich habe auch eine Liste wie folgt.
mylist = ["tim tam", "chocolate ice cream", "fresh vegetables and fruits"]
Nun, ich möchte die aufeinander folgenden Wörter in mylist
und kombinieren sie finden, wie mycookbook
zu aktualisieren folgt.
mycookbook = [["i", "love", "tim tam" "and", "chocolate ice cream"], ["cooking", "fresh vegetables",
"is", "easy"],["fresh vegetables and fruits", "are", "good", "for", "health"]]
Ich verwende derzeit zwei Wörter wie folgt.
for sentence in mycookbook:
i = 0
while i < len(sentence) - 1:
if sentence[i] + ' ' + sentence[i + 1] in mylist:
sentence[i] += ' ' + sentence[i + 1]
sentence.pop(i + 1)
i += 1
print(mycookbook)
Das Problem ist schlecht eingeschränkt. Was sollte die Ausgabe für 'mcookbook = [['a', 'b', 'c']]' und 'mylist = ['a b', 'b c']' sein? – wim
@wim 'mylist' hat keine Wortphrasen wie diese :) –
@Volka wissend dass die Ausgabe von wim's Frage uns hilft Ihnen bei der Lösung Ihres Problems zu helfen. – gommb