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.
任何人都知道如何使用值作为参考对列表进行排序?例如,我有以下列表:
1,3,5,10,12
和单个值7,输出将是:
7
5,10,3,12,1
谢谢!
>>> nums = [1,3,5,10,12] >>> sorted(nums, key=lambda x: abs(7 - x)) [5, 10, 3, 12, 1]
就地排序:
>>> nums.sort(key=lambda x: abs(7 - x)) >>> nums [5, 10, 3, 12, 1]