Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “怎么说?”;key=lambda w:sorted(w)";工作_Python_Sorting - Fatal编程技术网

Python “怎么说?”;key=lambda w:sorted(w)";工作

Python “怎么说?”;key=lambda w:sorted(w)";工作,python,sorting,Python,Sorting,输出: words = "4of Fo1r pe6ople g3ood th5e the2" words = sorted(words.split(), key=lambda w:sorted(w)) 我不知道这个函数是如何根据单词中的数字对单词进行排序的,内部的排序的根据数字代码对每个单词中的符号(字母和数字)排序,数字代码或多或少是按字母顺序排列的,数字在字母之前。正如您可能注意到的,Python字符串的处理方式通常与任何其他ITerable(如列表、集合或字典)相同。例如,sorted(

输出:

words = "4of Fo1r pe6ople g3ood th5e the2"
words = sorted(words.split(), key=lambda w:sorted(w))

我不知道这个函数是如何根据单词中的数字对单词进行排序的,内部的
排序的
根据数字代码对每个单词中的符号(字母和数字)排序,数字代码或多或少是按字母顺序排列的,数字在字母之前。正如您可能注意到的,Python字符串的处理方式通常与任何其他ITerable(如列表、集合或字典)相同。例如,
sorted(“of 4”)
会产生列表
[“4”、“f”、“o”]
,因为数字符号被认为位于字母之前<代码>已排序
将(
“For1”
)映射到以“1”开头的列表中。此列表位于以
“4”
开头的列表和其他列表之前

排序后的
应用于每个单词,您会更清楚


更严格地说,
sorted
将单词转换为一个由一个字母字符串组成的有序列表。外部排序的
通过比较相应的排序列表对这些单词进行排序。Python列表是逐元素比较的

sorted
参数是对输入的每个元素调用的函数,用于生成要排序的值。因此,当
键本身也被
排序时(注意:
键=lambda w:sorted(w)
只是拼写
键=sorted
的一种缓慢方式),这意味着它对
'Fo1r'
进行排序,以产生
键的
值['1','F','o','r']
。由于排序字符按序数值对其进行有效排序,并且ASCII数字在序数值上先于ASCII字母,这意味着在这种特殊情况下,每个输入中有一个唯一的数字,每个字符串的其余部分为字母,它按数字进行有效排序

如果同一个数字出现在多个输入中,它将退回到按数字以外的最高序数值排序;e、 g.
'abc1'
将在
'xyz1'
之前排序,因为回退比较将
'a'
'x'
进行比较。类似地,如果在某些输入中出现空格字符,但在其他输入中没有出现空格字符,则这些输入将在所有其他输入之前排序(因为空格字符是序号32,而
'0'
是序号48)。

以下是您的文字:

['Fo1r', 'the2', 'g3ood', '4of', 'th5e', 'pe6ople']
您可以对每个单词的字母进行排序:

>>> words = "4of Fo1r pe6ople g3ood th5e the2"
>>> words = words.split()
>>> words
['4of', 'Fo1r', 'pe6ople', 'g3ood', 'th5e', 'the2']
如果您
zip
单词和上一个列表,您将看到每个单词以及键:

>>> [sorted(w) for w in words]
[['4', 'f', 'o'], ['1', 'F', 'o', 'r'], ['6', 'e', 'e', 'l', 'o', 'p', 'p'], ['3', 'd', 'g', 'o', 'o'], ['5', 'e', 'h', 't'], ['2', 'e', 'h', 't']]
这就是为什么
Fo1r
(键:
['1',F',o',r']
)在
2
(键:
['2',e',h',t']
)之前,等等…

您可以使用基于字符串中的数字比较字符串的函数版本,而不是“排序”函数

下面我展示了同样的功能

>>> list(zip(words, [sorted(w) for w in words]))
[('4of', ['4', 'f', 'o']), ('Fo1r', ['1', 'F', 'o', 'r']), ('pe6ople', ['6', 'e', 'e', 'l', 'o', 'p', 'p']), ('g3ood', ['3', 'd', 'g', 'o', 'o']), ('th5e', ['5', 'e', 'h', 't']), ('the2', ['2', 'e', 'h', 't'])]

这是一种n00b方式,可以执行
key=sorted
。请注意,对于这个特定的数据,
key=min
将是做同样事情的更有效的方法。这是否回答了您的问题?
 >>> def mysort(string):
        lst=[]
        final=''
        for char in string:
            if char.isdigit():
                lst.append(char)
        lst.sort()
        for i in lst:
            for word in string.split():
                if i in word:
                    final=final+' '+word
        return final

    output:
>>> words= "4of Fo1r pe6ople g3ood th5e the2"
>>> mysort(words)
' Fo1r the2 g3ood 4of th5e pe6ople'