文字列のリストがある場合-
common = ['the','in','a','for','is']
そして私はリストに分割された文を持っています-
lst = ['the', 'man', 'is', 'in', 'the', 'barrel']
2つを比較するにはどうすればよいですか。共通の単語がある場合は、文字列全体をタイトルとして再度印刷します。一部は機能していますが、最終結果では、元の文字列だけでなく、新しく変更された共通の文字列も出力されます。
new_title = lst.pop(0).title()
for word in lst:
for word2 in common:
if word == word2:
new_title = new_title + ' ' + word
new_title = new_title + ' ' + word.title()
print(new_title)
出力:
The Man is Is in In the The Barrel
だから私は、小文字の単語が共通して、元の単語がなく、タイトルの大文字小文字に変更されることなく、新しい文にとどまるようにしようとしています。