简体   繁体   中英

Don't understand Python Expression

I have some basic knowledge on Python but I have no idea what's going for the below code. Can someone help me to explain or 'translate' it into a more normal/common expression?

steps = len(t)
sa = [i for i in range(steps)]
sa.sort(key = lambda i: t[i:i + steps])#I know that sa is a list
for i in range(len(sa)):
   sf = t[sa[i] : sa[i] + steps]

't' is actually a string

Thank you.

What I don't understand is the code: sa.sort(key = lambda i: t[i:i + steps])`

sa.sort(key = lambda i: t[i:i + steps])

It sorts sa according to the natural ordering of substrings t[i:i+len(t)] . Actually i + steps will always be greater or equal than steps (which is len(t) ) so it could be written t[i:] instead (which makes the code simpler to understand)

You will better understand using the decorate/sort/undecorate pattern:

>>> t = "azerty"
>>> sa = range(len(t))
>>> print sa
[0, 1, 2, 3, 4, 5]
>>> decorated = [(t[i:], i) for i in sa]
>>> print decorated
[('azerty', 0), ('zerty', 1), ('erty', 2), ('rty', 3), ('ty', 4), ('y', 5)]
>>> decorated.sort()
>>> print decorated
[('azerty', 0), ('erty', 2), ('rty', 3), ('ty', 4), ('y', 5), ('zerty', 1)]
>>> sa = [i for (_dummy, i) in decorated]
>>> print sa
[0, 2, 3, 4, 5, 1]

and sf = t[sa[i] : sa[i] + steps]

This could also be written more simply:

for i in range(len(sa)):
    sf = t[sa[i] : sa[i] + steps]

=>

for x in sa:
    sf = t[x:]
    print sf

which yields:

azerty
erty
rty
ty
y
zerty

You'll notice that this is exactly the keys used (and then discarded) in the decorate/sort/undecorate example above, so the whole thing could be rewritten as:

def foo(t):
    decorated = sorted((t[i:], i) for i in range(len(t)))
    for sf, index in decorated:
        print sf
        # do something with sf here

As to what all this is supposed to do, I'm totally at lost, but at least you now have a much more pythonic (readable...) version of this code ;)

The lambda in sort defines the criteria according to which the list is going to be sorted. In other words, the list will not be sorted simply according to its values, but according to the function applied to the values. Have a look here for more details.

It looks like what you are doing is sorting the list according to the alphabetical ordering of the substrings of the input string t.

Here is what is happening:

t = 'hello'   # EXAMPLE

steps = len(t)
sa = [i for i in range(steps)]

sort_func = lambda i: t[i:i + steps]

for el in sa:
    print sort_func(el)

#ello
#hello
#llo
#lo
#o

So these are the values that determines the sorting of the list.

transf_list = [sort_func(el) for el in sa]
sorted(transf_list) 
# ['ello', 'hello', 'llo', 'lo', 'o']

Hence:

sa.sort(key = sort_func)#I know that sa is a list
# [1, 0, 2, 3, 4]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM