Python Forum
Remove an item from a list contained in another item in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove an item from a list contained in another item in python
#11
You can step through the code at the following link
visualize code execution
Reply
#12
I have an even more avant-garde version that works for me. I don't know if this is documented somewhere
roots = []
roots.extend(s for s in S if not any(s.issubset(r) for r in roots))
Reply
#13
(Nov-09-2021, 07:17 PM)deanhystad Wrote: Ahh, I understand your confusion now. It is clever, and I guess potentially confusing.

You are correct that roots starts out as an empty list the first time through the "for s in S:" loop.

roots = [], s = {2, 3, 4, 5}

Since roots is empty, s is not going to be a subset of any of the sets in roots and is appended to roots.

roots = [{2, 3, 4, 5}], s = {4, 5, 6}

Second time through the outer loop roots contains {2, 3, 4, 5}. s is not a subset of {2, 3, 4, 5}, so it is appended to roots.

roots = [{2, 3, 4, 5}, {4, 5, 6}] s = {2, 3}

Third time through the outer loop {2, 3} is a subset of {2, 3, 4, 5}, so it is not appended to roots.

Do follow how this goes now? Each time through the outer loop we test if s is a subset of any of the sets in root. If s is a subset of one of the sets in root, it is not appended to root. If s is not a subset of any set in root, s is appended to root.

Okay, thanks, but how would you write it if you don't want the "for r in roots" on the same line as the "any(s.issubset( r )"? I am trying to rewrite it that way, because I still have a hard time understanding the " line ??? of the for loop" and I just can't do it.
Reply
#14
items = [[2, 3, 4, 5], [2, 3], [7, 8], [2], [4, 5, 6]]
sorted_items = sorted((set(item) for item in items), key=len, reverse=True)


roots = []
for item in sorted_items:
    for root in roots:
        if item.issubset(root):
            break
    else:
        roots.append(item)

print(roots)
Output:
[{2, 3, 4, 5}, {4, 5, 6}, {8, 7}]
Gribouillis likes this post
Reply
#15
You can replace the "any" with a loop, but you need the "for r in roots".
l = [[2,3,4,5],[2,3],[7,8],[2],[4,5,6]]
S = sorted((set(x) for x in l), key=len, reverse=True)

roots = []
for s in S:
    for r in roots:
        if s.issubset(r):
            break  # Exclude s from results
    else:
        roots.append(s)  # Include s in results

print(roots)
Echo...echo...
Reply
#16
Also
sorted_items = sorted(map(set, items), key=len, reverse=True)
would be prettier.
Reply
#17
(Nov-09-2021, 10:19 PM)deanhystad Wrote: You can replace the "any" with a loop, but you need the "for r in roots".
l = [[2,3,4,5],[2,3],[7,8],[2],[4,5,6]]
S = sorted((set(x) for x in l), key=len, reverse=True)

roots = []
for s in S:
    for r in roots:
        if s.issubset(r):
            break  # Exclude s from results
    else:
        roots.append(s)  # Include s in results

print(roots)
Echo...echo...

Well i tried rewriting it from my own comprehension and i came up with that which is in my mind the same thing, but it doesn't work??? What am i missing?

ls = [[2,3,4,5],[2,3],[7,8],[2],[4,5,6]]
l = []
roots = []
for s in ls:    
    l.append(set(s))

for s in l:
    for r in roots:
        if s.issubset(r):
            break
        else:
            roots.append(s)

print(roots)
I got an empty list : roots = []
Reply
#18
The else needs to be attached to for loop, not the if statement. Read about for..else.
Reply
#19
Thank you very much everyone. Your help has been very helpful! I really appreciated it
Reply
#20
@CompleteNewb WARNING If you don't sort the list of sets by decreasing lengths, the algorithm may fail. It will keep sets that are subsets of other sets that come later in the list.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 371 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,163 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Copy item from one dict to another Pavel_47 3 941 Dec-23-2022, 11:19 AM
Last Post: Pavel_47
  Remove numbers from a list menator01 4 1,251 Nov-13-2022, 01:27 AM
Last Post: menator01
  code to send attachments contained on the drive. stefanoste78 1 822 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  How to sort .csv file test log which item first fail and paint color SamLiu 24 4,700 Sep-03-2022, 07:32 AM
Last Post: Pedroski55
Question Finding string in list item jesse68 8 1,798 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  how to mouse click a specific item in pygame? Frankduc 5 1,661 May-03-2022, 06:22 PM
Last Post: Frankduc
  Can I check multi condition for 1 item in a easy way? korenron 4 1,534 May-01-2022, 12:43 PM
Last Post: deanhystad
  Remove empty keys in a python list python_student 7 2,899 Jan-12-2022, 10:23 PM
Last Post: python_student

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020