简体   繁体   English

不懂Python表达式

[英]Don't understand Python Expression

I have some basic knowledge on Python but I have no idea what's going for the below code. 我有一些关于Python的基本知识,但我不知道下面的代码是什么。 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 't'实际上是一个字符串

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])`

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

It sorts sa according to the natural ordering of substrings t[i:i+len(t)] . 它根据子串t[i:i+len(t)]的自然排序对sa进行排序。 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) 实际上i + steps总是大于或等于stepslen(t) )所以它可以写成t[i:]而不是(这使得代码更容易理解)

You will better understand using the decorate/sort/undecorate pattern: 您将更好地理解使用decorate / sort / undecorate模式:

>>> 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] 和sf = t [sa [i]:sa [i] +步骤]

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: 您会注意到这正是上面的decorate / sort / undecorate示例中使用(然后丢弃)的键,所以整个事情可以重写为:

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 ;) 至于所有这一切应该做什么,我完全迷失了,但至少你现在有一个更加pythonic(可读...)版本的代码;)

The lambda in sort defines the criteria according to which the list is going to be sorted. sortlambda定义了要对列表进行排序的条件。 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. 看起来你正在做的是根据输入字符串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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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