注:これにはファイルの読み書きは含まれません。
データ:誰も見たくないワンライナーリストの内包のための今すぐ
file = """
cow
duck
sheep
"""
master_record = """
duck
sheep
cat
dog
"""
:
print([i for i in [x for x in file.replace('\n', ' ').split(' ') if x in master_record.replace('\n', ' ').split(' ')] if i])
ファイル内のすべての単語のリストを返します。マスターレコードにも表示されます。それを分割
:
found = []
# Loop through ever word in `file`, replacing newlines with spaces,
for word in file.replace('\n', ' ').split(' '):
# Check if the word is in the master file,
if word in master_record.replace('\n', ' ').split(' '):
# Make sure the word contains something,
if word:
# Add this word to found,
found += [word]
# Print what we found,
print(found)
・ホープ、このことができます!
-Coolq
結果はCowですか?また、これを実装しようとしましたか?あなたのコードを表示し、あなたの実装に何があるのかを示すことができますか? – idjaw
また、各単語の位置は重要ですか?それとも、もしそれが1つの言葉が両方にあるのを見たらそれを取り除かないのですか? –
また、大きなファイルですか?重複を保つかどうか? –