Is there a shorter way to make this function?

后端 未结 7 912
鱼传尺愫
鱼传尺愫 2021-01-24 23:47

B. front_x Given a list of strings, return a list with the strings in sorted order, except group all the strings that begin with \'x\' first. e.g. [\'mix\', \'xyz\', \'apple\',

相关标签:
7条回答
  • 2021-01-25 00:25

    Use list comprehensions

    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
    
    0 讨论(0)
提交回复
热议问题