Python: remove strings with same words
Question: I have a list of strings like this:

    
    
    string_list = ['this is a string', 'and this', 'also this', 'is this a string']
    

What I want to do is remove strings that have the same words in them, which in
this case would be 'this is a string' and 'is this a string', so that there
only remains one (not important which one of the two). In the end the string
should look like this:

    
    
    string_list = ['this is a string', 'and this', 'also this']
    

So basically, remove strings that contain the same words as another string.


Answer: sorted each string after splitting and keep a set of all those sorted
substrings, if we have seen the sorted substring before the remove the current
element from the list :

    
    
    string_list = ['this is a string', 'and this', 'also this',
               'this and this is a string', 'is this a string']
    seen = set()
    for ele in reversed(string_list):
        tp = tuple(sorted(ele.split()))
        if tp in seen:
            string_list.remove(ele)
        seen.add(tp)
    
    print(string_list)
    ['this is a string', 'and this', 'also this', 'this and this is a string']
    

