0

I am trying to do this using list comprehension. I am using a subset of python 2.7 that does not allow the use of the command any or all

string_list1 = ['James Dean', 'Mr. James Dean', 'Jon Sparrow', 'Timothy Hook', 'Captain Jon Sparrow']
string_list2 = []

# Get elements that are a substring of other elements
for str1 in string_list1:
    for str2 in string_list1:
        if str1 in str2 and str1 != str2:
            string_list2.append(str1)
print('Substrings: ', string_list2)

# remove element if another element is within it
for str2 in string_list2:
    for str1 in string_list1:
        if str2 in str1 and str2 != str1:
            string_list1.remove(str1)
print('Desired: ', string_list1) # all elements that are unique

The result should be ['James Dean', 'Jon Sparrow', 'Timothy Hook'] basically the substrings and non substring elements

1 Answers1

2

You could apply the same algorithm with list comprehension like this:

lst = ['James Dean', 'Mr. James Dean', 'Jon Sparrow', 'Timothy Hook', 'Captain Jon Sparrow']

res = [primitive for primitive in lst 
          if primitive not in (superstr for superstr in lst
              if [substr for substr in lst if substr in superstr and substr != superstr]
          )
      ]

print(res)

But an interpreter will not see that the inner expression (superstr ...) has to be evaluated only once, not for every iteration of the outer loop. So I would prefer to do this in two steps:

lst = ['James Dean', 'Mr. James Dean', 'Jon Sparrow', 'Timothy Hook', 'Captain Jon Sparrow']

exclude = [superstr for superstr in lst
              if [substr for substr in lst if substr in superstr and substr != superstr]
          ]
res = [primitive for primitive in lst if primitive not in exclude]

print(res)
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Damn! that's cool. Funnily enough I did this minor change and it still worked! ```lst = ['James Dean', 'Mr. James Dean', 'Jon Sparrow', 'Timothy Hook', 'Captain Jon Sparrow'] exclude = [superstr for superstr in lst if [substr for substr in lst if substr in superstr and substr != superstr] ] lst = [primitive for primitive in lst if primitive not in exclude] print(lst)``` – Jason Hudson Jul 31 '21 at 09:49
  • 1
    Sorry about missing that out - accepting the answer! many thanks – Jason Hudson Aug 01 '21 at 21:29