سؤال

Suppose that I have a set of strings. If a string is a substring of another string, then the former should be removed the set.

My idea is to iterate all the strings in the original set, and for each string test against other strings in the set and remove any string that is a substring of others in the original set. But this results in in-place modification of the original set, which might cause some problems in the implementation.

Does anybody one have a better idea how this should be implemented? Thanks.

هل كانت مفيدة؟

المحلول

Your question is not very clear. But if I understand you correctly, you can do something like this

l = sorted(["abcd", "abc", "ab", "a"], key = len)
print [ss for idx, ss in enumerate(l) if all(ss not in cs for cs in l[idx + 1:])]

Output

['abcd']
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top