写一个函数有两个输入参数对后一个数组进行排序例如:输入参数为数组a和数组b数组a是wtxehc数组b是xtc给数组b排序期望得到返回值txc
你可以使用Python中的sorted()函数来对数组b进行排序。代码如下:
def sort_array(a, b):
return sorted(b, key=lambda x: a.index(x))
# 测试代码
a = ['w', 't', 'x', 'e', 'h', 'c']
b = ['x', 't', 'c']
sorted_b = sort_array(a, b)
print(sorted_b)
输出结果为:['t', 'x', 'c']
原文地址: http://www.cveoy.top/t/topic/h076 著作权归作者所有。请勿转载和采集!