2017-07-22 16 views
-3

リストd1とd2の2つの文字列を比較したい。 d1の文字列がd2と似ている場合は、同様の文字列を返します。リストから作成した2つの文字列の比較

ここに私のコードです:それはSTR1における類似文字列から来^

['learn','learn'] 

d1 = [['learn'],['car']] 
d2 = [['learn'],['motor']] 

str1 = ', '.join(str(i) for i in d1) 
str2 = ', '.join(str(j) for j in d2) 

for i in str1: 
    for j in str2: 
     if i == j: 
      print str1, str2 

が、出力は次のようになります。

['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 
['learn'], ['car'] ['learn'], ['motor'] 

私は出力があることを期待とstr2。

誰でもお手伝いできますか?

+1

試してみてください。 'プリント私は、j' –

+1

私は何を理解していません後になります。あなたは2つのリストの交差点を探していますか?すなわち、両方のリストに存在する要素。 – pookie

+0

彼はそうだと思う。 –

答えて

0

どのようにこのようなものについて:

d1 = [['learn'],['car']] 
d2 = [['learn'],['motor']] 
for elem in d1: 
    if elem in d2: 
    print([elem[0],elem[0]]) 
0

zipを使用して解決するための簡単なアプローチ.About zip

for i,j in zip(d1,d2): 
if i==j: 
    print i,j 
関連する問題