私は何時間も苦労していて、うまくいかないので、助けてください!私はいくつかのタプルを含むリストの2つのリストを持っています。Python:繰り返し実行して2つのタプルのリストを要素ごとに比較します
list_1 = [[('This', 'DT'), ('is', 'VBZ'), ('an', 'DT'), ('apple', 'NN')], [('This', 'DT'), ('Should', 'MD'), ('be', 'VB'), ('taught', 'VBN'), ('at','IN'), ('school', 'NN')], [('you', 'PRP'), ('might', 'MD'), ('take', 'VB'), ('them', 'PRP')]]
list_2 = [[('This', 'DT'), ('is', 'VBN'), ('an', 'DT'), ('apple', 'NNS')], [('This', 'DT'), ('Should', 'VB'), ('be', 'VB'), ('taught', 'VBN'), ('at','IN'), ('school', 'NNP')], [('you', 'PRP'), ('might', 'MD'), ('take', 'VB'), ('them', 'PRP')]]
私はlist_1
とlist_2
要素単位で各文の各タプルを比較し、異なるタグを持っている唯一のタプルを返すようにしたいです。
def get_similarity():
for list_1_sentence, list_2_sentence in zip(list_1, list_2):
if len(list_1) != len(list_2):
try:
raise Exception('this is an exception, some tags are missing')
except Exception as error:
print('caught this error : ', repr(error))
else:
return [(x, y) for x, y in zip(list_1_sentence, list_2_sentence) if x != y]
get_similarity()
問題は、それが別のタプルを返しますが、唯一の最初の文のためということである。
[(('is', 'VBZ), ('is', 'VBN)), ('apple', 'NN'), ('apple', 'NNS'))]
なぜすべての文章を反復処理しないのですか?
ありがとう、面白いアプローチの原因は後で私は文とそのタグの間の類似率を検索したい。したがって、異なるタグを1回だけ返す方が便利です。 – joasa
これは興味深いアプローチですが、恐らく最も読みにくい方法の1つです。ゴルフのように、+1:P – HyperNeutrino