社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Nithin R

Nithin R 最近创建的主题
Nithin R 最近回复了
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