Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8096309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:24:46+00:00 2026-06-05T21:24:46+00:00

Each element in the list L is a tuple of the form (fields, size).

  • 0

Each element in the list L is a tuple of the form (fields, size). For example

L = [ (['A','B'], 5), (['A'], 6), ('C', 1)]

I would like to cull the list so it only contains non-intersecting members, and each member that remains was greater than any other members it may have intersected. So the example list L would be reduced to

L = [ (['A'], 6), ('C', 1)]

Currently I have it implemented like so:

def betterItem(x, y):
    return (x != y and
            set(x[0]) & set(y[0]) and
            x[1] > y[1])

for i in range(len(L)-1):
    L[:] = [x for x in L for y in L if betterItem(x, y)]

Is there a better/faster/more Pythonic way of doing this?

Thanks for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:24:49+00:00Added an answer on June 5, 2026 at 9:24 pm
    L = [(['A','B'], 5), (['A'], 6), (['C'], 1)]
    
    # sort by descending value
    L.sort(key=lambda s:s[1], reverse=True)
    
    # keep track of what members have already occurred
    seen = set()
    
    # Cull L - ignore members already in `seen`
    # (Because it is presorted, already-seen members must have had a higher value)
    L = [seen.update(i) or (i,j) for i,j in L if seen.isdisjoint(i)]
    

    results in

    [(['A'], 6), (['C'], 1)]
    

    (This list comprehension uses a bit of sleight-of-hand: seen.update always returns None, and None or x always returns x – so seen.update(i) or (i,j) returns the tuple (i,j) with the side-effect of updating the seen-member list.)

    This should be O(n log n) due to the sort, instead of your O(n^2).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an unordered list and each list element contains a photo and a
I have a list of lists that looks like this: x[[state]][[year]] . Each element
I'm using yield to process each element of a list. However, if the tuple
I have a Python list of tuple. Each tuple contains the attributes of a
How does one return e.g. the first element of a tuple? I would like
I have a singly-linked list containing 1 value in each element. struct listElement {
Ive got a long list of xml elements. Each element got an ID and
OK, I have a list ( <ul> ) then inside each <li> element I
I am trying to access each element in a List<string> list and split each
I need to check if each element in second list has 3 times more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.