Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有这个清单
a = ['20 - A', '200 - A', '30 - B']
我想以一种按数字排序的方式对其进行排序,如下所示:
['20 - A', '30 - B', '200 - A']
我知道我可以通过找到第一个空格来拆分值,但我想不出一种方法来对它进行排序
>>> a = ['20 - A', '200 - A', '30 - B'] >>> sorted(a, key = lambda x: int(x.split()[0])) >>> ['20 - A', '30 - B', '200 - A']