1

I use python3.6 I want to delete the same string,and this is my code.

r = [['I1', 'I2'], ['I1', 'I3'], ['I1', 'I5'], ['I2', 'I3'], ['I2','I4'], ['I2', 'I5']]
for v in range(len(r)):
    for k in range(v+1,len(r)):
        union = list(set(r[v]) | set(r[k]))
        print(sorted(union))

and results is:

['I1', 'I2', 'I3']
['I1', 'I2', 'I5']
['I1', 'I2', 'I3']
['I1', 'I2', 'I4']
['I1', 'I2', 'I5']
['I1', 'I3', 'I5']
['I1', 'I2', 'I3']
['I1', 'I2', 'I3', 'I4']
['I1', 'I2', 'I3', 'I5']
['I1', 'I2', 'I3', 'I5']
['I1', 'I2', 'I4', 'I5']
['I1', 'I2', 'I5']
['I2', 'I3', 'I4']
['I2', 'I3', 'I5']
['I2', 'I4', 'I5']

how can I delete the same string?

2
  • 7
    Do you mean duplicate strings? Also, please clarify if you don't mean list of strings. You can show that by adding your expected output to your question. Commented Dec 12, 2018 at 14:20
  • @Kasrâmvd Thank you for your explanation. Commented Dec 12, 2018 at 14:33

1 Answer 1

2

I created Union_list to keep track of what already got printed:

Union_list = [] 
r = [['I1', 'I2'], ['I1', 'I3'], ['I1', 'I5'], ['I2', 'I3'], ['I2','I4'], ['I2', 'I5']]
for v in range(len(r)):
    for k in range(v+1,len(r)):
        union = sorted(list(set(r[v]) | set(r[k])))
        if union not in Union_list:
            Union_list.append(union)
            print(union)

Output:

['I1', 'I2', 'I3']
['I1', 'I2', 'I5']
['I1', 'I2', 'I4']
['I1', 'I3', 'I5']
['I1', 'I2', 'I3', 'I4']
['I1', 'I2', 'I3', 'I5']
['I1', 'I2', 'I4', 'I5']
['I2', 'I3', 'I4']
['I2', 'I3', 'I5']
['I2', 'I4', 'I5']
Sign up to request clarification or add additional context in comments.

1 Comment

@mad_ sure. Fixed it now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.