Java's TreeSet equivalent in Python?

后端 未结 6 1898
悲&欢浪女
悲&欢浪女 2020-12-30 22:32

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

6条回答
  •  既然无缘
    2020-12-30 23:02

    1. I don't think python has a built-in Sorted sets. How about something like this?

    letters = ['w', 'Z', 'Q', 'B', 'C', 'A']
      for l in sorted(set(letters)):
         print l
    

    2.Java TreeSet is an implementation of the abstraction called SortedSet. Basic types will be sorted on natural order.A TreeSet instance performs all key comparisons using its compareTo (or compare) method.So your custom keys should implement proper compareTo

提交回复
热议问题