Python函数排序数组:根据另一个数组的顺序排序
你可以使用Python中的sorted()函数来对数组b进行排序。代码如下:\n\npython\ndef sort_array(a, b):\n return sorted(b, key=lambda x: a.index(x))\n\n# 测试代码\na = ['w', 't', 'x', 'e', 'h', 'c']\nb = ['x', 't', 'c']\nsorted_b = sort_array(a, b)\nprint(sorted_b)\n\n\n输出结果为:['t', 'x', 'c']
原文地址: https://www.cveoy.top/t/topic/pKbz 著作权归作者所有。请勿转载和采集!