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 8594299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:09:59+00:00 2026-06-12T00:09:59+00:00

In Python, I have 3 lists of floating-point numbers (angles), in the range 0-360,

  • 0

In Python, I have 3 lists of floating-point numbers (angles), in the range 0-360, and the lists are not the same length. I need to find the triplet (with 1 number from each list) in which the numbers are the closest. (It’s highly unlikely that any of the numbers will be identical, since this is real-world data.) I was thinking of using a simple lowest-standard-deviation method to measure agreement, but I’m not sure of a good way to implement this. I could loop through each list, comparing the standard deviation of every possible combination using nested for loops, and have a temporary variable save the indices of the triplet that agrees the best, but I was wondering if anyone had a better or more elegant way to do something like this. Thanks!

  • 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-12T00:10:01+00:00Added an answer on June 12, 2026 at 12:10 am

    I wouldn’t be surprised if there is an established algorithm for doing this, and if so, you should use it. But I don’t know of one, so I’m going to speculate a little.

    If I had to do it, the first thing I would try would be just to loop through all possible combinations of all the numbers and see how long it takes. If your data set is small enough, it’s not worth the time to invent a clever algorithm. To demonstrate the setup, I’ll include the sample code:

    # setup
    def distance(nplet):
        '''Takes a pair or triplet (an "n-plet") as a list, and returns its distance.
        A smaller return value means better agreement.'''
        # your choice of implementation here. Example:
        return variance(nplet)
    
    # algorithm
    def brute_force(*lists):
        return min(itertools.product(*lists), key = distance)
    

    For a large data set, I would try something like this: first create one triplet for each number in the first list, with its first entry set to that number. Then go through this list of partially-filled triplets and for each one, pick the number from the second list that is closest to the number from the first list and set that as the second member of the triplet. Then go through the list of triplets and for each one, pick the number from the third list that is closest to the first two numbers (as measured by your agreement metric). Finally, take the best of the bunch. This sample code demonstrates how you could try to keep the runtime linear in the length of the lists.

    def item_selection(listA, listB, listC):
        # make the list of partially-filled triplets
        triplets = [[a] for a in listA]
        iT = 0
        iB = 0
        while iT < len(triplets):
            # make iB the index of a value in listB closes to triplets[iT][0]
            while iB < len(listB) and listB[iB] < triplets[iT][0]:
                iB += 1
            if iB == 0:
                triplets[iT].append(listB[0])
            elif iB == len(listB)
                triplets[iT].append(listB[-1])
            else:
                # look at the values in listB just below and just above triplets[iT][0]
                # and add the closer one as the second member of the triplet
                dist_lower = distance([triplets[iT][0], listB[iB]])
                dist_upper = distance([triplets[iT][0], listB[iB + 1]])
                if dist_lower < dist_upper:
                    triplets[iT].append(listB[iB])
                elif dist_lower > dist_upper:
                    triplets[iT].append(listB[iB + 1])
                else:
                    # if they are equidistant, add both
                    triplets[iT].append(listB[iB])
                    iT += 1
                    triplets[iT:iT] = [triplets[iT-1][0], listB[iB + 1]]
            iT += 1
        # then another loop while iT < len(triplets) to add in the numbers from listC
        return min(triplets, key = distance)
    

    The thing is, I can imagine situations where this wouldn’t actually find the best triplet, for instance if a number from the first list is close to one from the second list but not at all close to anything in the third list. So something you could try is to run this algorithm for all 6 possible orderings of the lists. I can’t think of a specific situation where that would fail to find the best triplet, but one might still exist. In any case the algorithm will still be O(N) if you use a clever implementation, assuming the lists are sorted.

    def symmetrized_item_selection(listA, listB, listC):
        best_results = []
        for ordering in itertools.permutations([listA, listB, listC]):
            best_results.extend(item_selection(*ordering))
        return min(best_results, key = distance)
    

    Another option might be to compute all possible pairs of numbers between list 1 and list 2, between list 1 and list 3, and between list 2 and list 3. Then sort all three lists of pairs together, from best to worst agreement between the two numbers. Starting with the closest pair, go through the list pair by pair and any time you encounter a pair which shares a number with one you’ve already seen, merge them into a triplet. For a suitable measure of agreement, once you find your first triplet, that will give you a maximum pair distance that you need to iterate up to, and once you get up to it, you just choose the closest triplet of the ones you’ve found. I think that should consistently find the best possible triplet, but it will be O(N^2 log N) because of the requirement for sorting the lists of pairs.

    def pair_sorting(listA, listB, listC):
        # make all possible pairs of values from two lists
        # each pair has the structure ((number, origin_list),(number, origin_list))
        # so we know which lists the numbers came from
        all_pairs = []
        all_pairs += [((nA,0), (nB,1)) for (nA,nB) in itertools.product(listA,listB)]
        all_pairs += [((nA,0), (nC,2)) for (nA,nC) in itertools.product(listA,listC)]
        all_pairs += [((nB,1), (nC,2)) for (nB,nC) in itertools.product(listB,listC)]
        all_pairs.sort(key = lambda p: distance(p[0][0], p[1][0]))
        # make a dict to track which (number, origin_list)s we've already seen
        pairs_by_number_and_list = collections.defaultdict(list)
        min_distance = INFINITY
        min_triplet = None
        # start with the closest pair
        for pair in all_pairs:
            # for the first value of the current pair, see if we've seen that particular
            # (number, origin_list) combination before
            for pair2 in pairs_by_number_and_list[pair[0]]:
                # if so, that means the current pair shares its first value with
                # another pair, so put the 3 unique values together to make a triplet
                this_triplet = (pair[1][0], pair2[0][0], pair2[1][0])
                # check if the triplet agrees more than the previous best triplet
                this_distance = distance(this_triplet)
                if this_distance < min_distance:
                    min_triplet = this_triplet
                    min_distance = this_distance
            # do the same thing but checking the second element of the current pair
            for pair2 in pairs_by_number_and_list[pair[1]]:
                this_triplet = (pair[0][0], pair2[0][0], pair2[1][0])
                this_distance = distance(this_triplet)
                if this_distance < min_distance:
                    min_triplet = this_triplet
                    min_distance = this_distance
            # finally, add the current pair to the list of pairs we've seen
            pairs_by_number_and_list[pair[0]].append(pair)
            pairs_by_number_and_list[pair[1]].append(pair)
        return min_triplet
    

    N.B. I’ve written all the code samples in this answer out a little more explicitly than you’d do it in practice to help you to understand how they work. But when doing it for real, you’d use more list comprehensions and such things.

    N.B.2. No guarantees that the code works 😛 but it should get the rough idea across.

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

Sidebar

Related Questions

Imagine you have a large array of floating point numbers, of all kinds of
Possible Duplicate: Python: How to find list intersection? I have two lists of data
I have several long lists in python and have compare them and find the
I have two python lists of floats (lists are of same size i.e same
Does python have immutable lists? Suppose I wish to have the functionality of an
In Python if I have 2 lists say: l1 = ['a', 'b', 'c', 'd']
Possible Duplicate: Python, compute list difference I have two lists For example: A =
I have about 50 million lists of strings in Python like this one: [1,
Python beginner here, I have a list of lists and want to refer to
I've been playing with Python and I have this list that I need worked

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.