2016-10-16 1 views
1
sample = ['A$$N','BBBC','$$AA'] 

リスト内の他のすべての要素とすべての要素を比較する必要があります。したがって、サンプル[0]とサンプル[1]、サンプル[0]とサンプル[2]、サンプル[1]とサンプル[2]を比較してください。 a 比較のペアのいずれかに "$"がある場合、 "$"と対応する要素を削除する必要があります。 例inリスト(Python)内の要素とそれに対応する文字の中の文字を削除するにはどうすればいいですか?

sample[0] and sample[1] Output1 : ['AN','BC'] 
sample[0] and sample[2] Output2 : ['N', 'A'] 
sample[1] and sample[2] Output3 : ['BC','AA'] 


for i in range(len(sample1)): 
    for j in range(i + 1, len(sample1)): 
     if i == "$" or j == "$": 
      #Need to remove "$" and the corresponding element in the other list 

    #Print the pairs 

答えて

1

これは最も美しいコードではないかもしれませんが、仕事をします。

from itertools import combinations 
sample = ['A$$N','BBBC','$$AA'] 
output = [] 
for i, j in combinations(range(len(sample)), 2): 
    out = ['', ''] 
    for pair in zip(sample[i], sample[j]): 
     if '$' not in pair: 
      out[0] += pair[0] 
      out[1] += pair[1] 
    output.append(out) 
print(output) 
+0

ありがとう、それは完璧に動作します。私はこのようなコードを理解するのが好きです – Biotechgeek

関連する問題