Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Marko Rauhamaa Newsgroups: comp.lang.python Subject: Re: Most pythonic way of rotating a circular list to a canonical point Date: Sun, 02 Aug 2015 09:25:56 +0300 Organization: A noiseless patient Spider Lines: 35 Message-ID: <877fpetde3.fsf@elektro.pacujo.net> References: <87fv42u3q5.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="b7cb1518d23ec19d482dcc9c31d30fdd"; logging-data="9493"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/aerRfhMCBWyBxAqVGRPp7" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:qBThlAUGBjIeTegTvSQ730rVCmE= sha1:mNdkV3QM1e21eoahHDmEoa4WPC8= Xref: csiph.com comp.lang.python:94888 Lukas Barth : > On Saturday, August 1, 2015 at 10:57:19 PM UTC+2, Marko Rauhamaa wrote: >> ======================================================================== >> def circularly_equal(l1, l2): >> length = len(l1) >> if length != len(l2): >> return False >> twice = l1 + l1 >> for i in range(length): >> if twice[i:i + length] == l2: >> return True >> return False >> ======================================================================== > > Nice idea! But I actually really need those "canonic rotations", since > I'm hashing them somewhere.. First, lists can't be used as dict keys, but that minor point is easy to overcome. Secondly, a hash doesn't need to be unique, it's enough for it to be discerning. So you could, for example, choose: sum([ hash(x) for x in L ]) + hash(sum(L)) That doesn't discern any permutations, but depending on your application might be good enough. That way, you use the "pretty good" hash function together with the circular equality test and you won't be needing any canonical representation for the key. Marko