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

  • Home
  • SEARCH
  • 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 753823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:56:29+00:00 2026-05-14T14:56:29+00:00

I recently came across some Java code that simply put some strings into a

  • 0

I recently came across some Java code that simply put some strings into a Java TreeSet, implemented a distance based comparator for it, and then made its merry way into the sunset to compute a given score to solve the given problem.

My questions,

  • Is there an equivalent data structure available for Python?

    • The Java treeset looks basically to be an ordered dictionary that can use a comparator of some sort to achieve this ordering.
  • I see there’s a PEP for Py3K for an OrderedDict, but I’m using 2.6.x. There are a bunch of ordered dict implementations out there – anyone in particular that can be recommended?

PS, Just to add – I could probably import DictMixin or UserDict and implement my own sorted/ordered dictionary, AND make it happen through a comparator function – but that seems to be overkill.

Thanks.


Update. Thanks for the answers. To elaborate a bit, lets say I’ve got a compare function thats defined like, (given a particular value ln),

def mycmp(x1, y1, ln):
  a = abs(x1-ln)
  b = abs(y1-ln)
  if a<b:
    return -1
  elif a>b:
    return 1
  else:
    return 0

I’m a bit unsure about how I’d integrate this into the ordering given in the ordered dict link given here...

Something like,

OrderedDict(sorted(d.items(), cmp=mycmp(len)))

Ideas would be welcome.

  • 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-05-14T14:56:30+00:00Added an answer on May 14, 2026 at 2:56 pm

    The Python 2.7 docs for collections.OrderedDict has a link to a OrderedDict recipe that runs on Python 2.4 or better.

    Edit: In regard to sorting: Use key= rather than cmp=. It tends to lead to faster code and moreover, the cmp= keyword has been eliminated in Python3.

    d={5:6,7:8,100:101,1:2,3:4}
    print(d.items())
    # [(1, 2), (3, 4), (100, 101), (5, 6), (7, 8)]
    

    The code you posted for mycmp doesn’t make it clear what you want passed as x1. Below, I assume x1 is supposed to be the value in each key-value pair. If so, you could do something like this:

    length=4
    print(sorted(d.items(),key=lambda item: abs(item[1]-length) ))
    # [(3, 4), (1, 2), (5, 6), (7, 8), (100, 101)]
    

    key=... is passed a function, lambda item: abs(item[1]-length).
    For each item in d.items(), the lambda function returns the number abs(item[1]-length). This number acts as proxy for the item as far as sorting is concerned. See this essay for more information on sorting idioms in Python.

    PS. len is a Python builtin function. So as to not clobber that len, I’ve changed the variable name to length.

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

Sidebar

Ask A Question

Stats

  • Questions 399k
  • Answers 399k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Assuming that you have to have an 11-dimensional array (doing… May 15, 2026 at 3:55 am
  • Editorial Team
    Editorial Team added an answer Well I ended up finding a solution for this so… May 15, 2026 at 3:55 am
  • Editorial Team
    Editorial Team added an answer Elegant systems provide false/0 as a declarative synonym for the… May 15, 2026 at 3:55 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.