如何使数组'string'为真?



假设我们得到了一个小写字符串列表,

我想要一个函数返回一个列表,其中的字符串按字母顺序排序,除了将列表开头以"x"开头的所有字符串分组。

['mix', 'xyz', 'apple', 'xanadu', 'aardvark']

这可能会对您有所帮助。

words = ['mix', 'xyz', 'apple', 'xanadu', 'aardvark']    
custom_sort_order = ['x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'y', 'z']
words = sorted(words, key=lambda word: [custom_sort_order.index(c) for c in word])
print(words)

最新更新