Java的TreeSet相当于Python? [英] Java's TreeSet equivalent in Python?
问题描述
我的问题,
-
是否有可用于Python的等效数据结构?
- Java treeset看起来基本上是一个有序的字典,可以使用某种比较器来实现这种排序。
-
我看到有一个 PEP for Py3K 的OrderedDict,但我使用的是2.6.x.有一些有序的dict实现,特别是可以推荐的?
PS,Just to添加 - 我可以可能导入DictMixin或UserDict并实现我自己的排序/排序字典,并通过比较函数实现 - 但这似乎是过度的。
谢谢。
更新。感谢您的答案。要说明一点,让我说我有一个比较功能,定义如下(给定一个特定的值ln),
def mycmp(x1,y1,ln):
a = abs(x1-ln)
b = abs(y1-ln)
如果一个< b:
return -1
elif a> b:
返回1
其他:
返回0
我有点不确定我将如何将它整合到有序dict 此处给出的链接.. 。
像
OrderedDict(sorted(d.items( ),cmp = mycmp(len)))
想法是受欢迎的。
Python 2.7 docs for collections.OrderedDict
有一个 OrderedDict配方。
编辑: 关于排序:使用 key =
而不是 cmp =
。它往往导致更快的代码,而且,在Python3中已经消除了 cmp =
关键字。
d = {
print(d.items())
#[(1,2),(3,4),(100 ,101),(5,6),(7,8)]
mycmp
并不清楚您想要作为 x1
传递什么。下面我假定x1应该是每个键值对中的值。如果是这样,你可以这样做:
length = 4
print(sorted(d.items ,key = lambda item:abs(item [1] -length)))
#[(3,4),(1,2),(5,6),(7,8) 101)]
key = ...
被传递一个函数, lambda项:abs(item [1] -length)
。
对于 d.items()
中的每个项目
,lambda函数返回数字 abs(item [1] -length)
。就排序而言,该数字作为项目的代理。有关Python中排序习语的更多信息,请参阅本文。
PS。 len
是一个Python内置函数。所以,为了不扼杀 len
,我已将变量名称更改为 length
。
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.
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, so as to not clobber that len
, I've changed the variable name to length
.
这篇关于Java的TreeSet相当于Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!