Python 有没有一种更短的方法来实现这个功能?

Python 有没有一种更短的方法来实现这个功能?,python,Python,B.front_x给定一个字符串列表,返回一个按排序顺序排列的字符串列表,但将所有以“x”开头的字符串分组。e、 g.[混合],“xyz”,“苹果”,“世外桃源”,“土豚”]产量[世外桃源”,“xyz”,“土豚”,“苹果”,“混合]]提示:这可以通过制作两个列表并在组合它们之前对每个列表进行排序来完成 def front_x(words): xList = [] nonXList = [] for word in words: if word[0] == 'x':

B.front_x给定一个字符串列表,返回一个按排序顺序排列的字符串列表,但将所有以“x”开头的字符串分组。e、 g.[混合],“xyz”,“苹果”,“世外桃源”,“土豚”]产量[世外桃源”,“xyz”,“土豚”,“苹果”,“混合]]提示:这可以通过制作两个列表并在组合它们之前对每个列表进行排序来完成

def front_x(words):
  xList = []
  nonXList = []
  for word in words:
    if word[0] == 'x':
      xList.append(word)
    else:
      nonXList.append(word)
  return sorted(xList) + sorted(nonXList)
一般来说,我对python和编程都是新手,但我觉得有一种更简洁的方法来编写代码,还是有一种更“pythonic”的方法

我还试着回电话:

return xlist.sort() + nonXList.sort()
但这是错误的。为什么会发生这种情况?

您可以通过一次调用sorted对单词进行排序,并使用关键参数来设定排序方式,您希望单词中的项目如何排序:

def front_x(words):
    return sorted(words, key=lambda word: (word[0] != 'x', word))
关键字函数对words中的每个项调用一次,并返回一个代理值,根据该值对words中的项进行排序。元组(如上面lambda函数返回的元组)根据元组中的第一项和第二项按字典顺序进行排序,以打破关系

这项技术和其他技术在优秀的文章中都有解释

比如说,

print(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']))
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
请注意,如果单词包含空字符串,单词[0]将引发索引器。相反,startswith'x'返回False。 因此,如果您希望front_x处理空字符串,请使用word.startswith'x',如果您希望引发异常,请使用word[0]='x'。

您可以通过一次调用sorted对单词进行排序,并使用键参数教您如何对单词中的项目排序:

def front_x(words):
    return sorted(words, key=lambda word: (word[0] != 'x', word))
关键字函数对words中的每个项调用一次,并返回一个代理值,根据该值对words中的项进行排序。元组(如上面lambda函数返回的元组)根据元组中的第一项和第二项按字典顺序进行排序,以打破关系

这项技术和其他技术在优秀的文章中都有解释

比如说,

print(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']))
# ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
请注意,如果单词包含空字符串,单词[0]将引发索引器。相反,startswith'x'返回False。
因此,如果希望front_x处理空字符串,请使用word.startswith'x',如果希望引发异常,请使用word[0]='x'。

返回的错误是因为sort不返回值,它只是修改列表


这似乎是一个相当快的方法,它以线性时间运行,你不会比这快很多。也就是说,您可以使用内联代码来缩短它,但这样做的可读性并不总是更高。

返回的错误是因为sort不返回值,它只是修改列表


这似乎是一个相当快的方法,它以线性时间运行,你不会比这快很多。也就是说,您可以使用内联代码来缩短它,但这样并不总是更具可读性。

使用列表理解

list =  ['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
list.sort()
listA = [item for item in list if item[0] == 'x']
listB = [item for item in list if item[0] != 'x']
listC = listA + listB

使用列表理解

list =  ['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
list.sort()
listA = [item for item in list if item[0] == 'x']
listB = [item for item in list if item[0] != 'x']
listC = listA + listB
错误是因为.sort已就位。它返回None,您不能执行None+None

由于Python的排序是稳定的,所以您还可以通过执行两种排序来实现这一点

>>> L = ['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
>>> sorted(sorted(L), key=lambda x:x[0]!='x')
['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
以及就地版本

>>> L.sort()
>>> L.sort(key=lambda x:x[0]!='x')
>>> L
['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
错误是因为.sort已就位。它返回None,您不能执行None+None

由于Python的排序是稳定的,所以您还可以通过执行两种排序来实现这一点

>>> L = ['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
>>> sorted(sorted(L), key=lambda x:x[0]!='x')
['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
以及就地版本

>>> L.sort()
>>> L.sort(key=lambda x:x[0]!='x')
>>> L
['xanadu', 'xyz', 'aardvark', 'apple', 'mix']

这是可行的,而且简单易懂

def front_x(words):
    xlist = [item for item in words if item[0] =='x']
    nonXList = [item for item in words if item[0] !='x']
    xlist.sort() # The .sort() method sorts a list alphabetically 
    nonXList.sort()
    Combined_Lists = xlist + nonXList
    return Combined_Lists
    #You can also comment Combined_Lists and 
    #just have return xlist + nonXList

因为您是Python新手,所以我尝试使其尽可能简单。

这是可行的,而且简单易懂

def front_x(words):
    xlist = [item for item in words if item[0] =='x']
    nonXList = [item for item in words if item[0] !='x']
    xlist.sort() # The .sort() method sorts a list alphabetically 
    nonXList.sort()
    Combined_Lists = xlist + nonXList
    return Combined_Lists
    #You can also comment Combined_Lists and 
    #just have return xlist + nonXList

因为您是Python新手,所以我尝试使其尽可能简单。

但您不应该使用is来比较values@KEYSER它确实有效,但在这种情况下不起作用。我们想知道第一个字符是否是“x”,而不是任何字符。但是你不应该用is来比较values@KEYSER它确实有效,但在这种情况下不起作用。我们想知道第一个字符是否是“x”,而不是任何字符。Python对所有字符都有一行。虽然并非所有的一行程序都是Python式的,但是Python对所有东西都有一行程序。虽然不是所有的一行都是pythonic.for单词[0]!='由于单词[0]!='x',word不会首先对非x单词进行排序像土豚、苹果、混搭?你能解释一下陈述词[0]!='x',是否读取和评估单词?本质上我不明白为什么它是单词[0]!='x'代替单词[0]=“x”代替单词[0]!='由于单词[0]!='x',word不会首先对非x单词进行排序像土豚、苹果、混搭?你能解释一下陈述词[0]!='x',是否读取和评估单词?本质上我不明白为什么它是单词[0]!='x'而不是字[0]=“x”