0

i'm trying to figure out how i can identify which objects are a subset/superset within the list that holds them. and thereby remove them from the list.

if i have list a = [ {'john-123'}, {'john-123','john-234'}, {'john-123','john-234','johnnybegood'} ] 

the first two are subsets of the last one.

i've found a lot about comparing two different sets, but not sure how to implement that into an iteration

i've tried something like this:

for j in a: frequency = sum(j.issubset(b) for b in a) print(frequency) 

but no dice

6
  • I dont understand what you are looking for. What do you expect for the example you give? What do you mean with 'no dice'? (sry, maybe my english isnt that good) Commented Aug 25, 2021 at 21:06
  • @H.Doebler well... for each j in the list (meaning for each set in the list) i want to identify if it is a subset or not. no dice means, unlucky or 'isn't working' Commented Aug 25, 2021 at 21:10
  • A subset of what? A subset of any set in the list? A subset of all sets in the list? A subset of all subsequent subsets in the list? Commented Aug 25, 2021 at 21:12
  • @H.Doebler whether it is a a subset of any other set in the same list. Commented Aug 25, 2021 at 21:13
  • 1
    Ok, obviously every set in the list is a subset of itself. But you want the list of elements that are a subset of an other set in the list. Can you assume that the list's elements are unique? I.e. is a = [{1}, {1,2}, {1}] a possible input to your problem? Commented Aug 25, 2021 at 21:17

1 Answer 1

1

The problem in your approach is that you test every element in the list against every element in the list, not every other element. Since every set is a subset of itself, you will always get the whole list as an answer.

Try this:

a = [{1}, {2}, {1,2}, {1,3}, {1,3,4}] 

Clearly, {1,2} and {1,3,4} are not subsets of other sets in a.

subsets = [b for b in a if any(b.issubset(c) and b != c for c in a)] print(subsets) [{1}, {2}, {1, 3}] 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.