Py学习  »  Nithin R  »  全部回复
回复总数  1
5 年前
回复了 Nithin R 创建的主题 » 如何在python中基于另一个列表重新排列列表

import numpy as np

fixedList = ['116','117','114','99','102','101','95']
unorderedList = ['99','116','117']

# isin returns a boolean array of the same shape as element 
# that is True where an element of unorderedList is in fixedList and False otherwise.

mask = np.isin(np.array(fixedList), np.array(unorderedList))

#Now form new list using mask
newlist = (np.array(fixedList)[mask]).tolist()

现在新列表包含以下内容

参考 https://docs.scipy.org/doc/numpy/reference/generated/numpy.isin.html