python - 修改数组的值,其中相同大小的列表包含另一个列表的值
问题描述
我有这两个列表:
List_large = ['a','b','c','d']
List_small = ['a','c']
这个数组:
check = np.array([0]*len(List_large))
check
Out : array([0, 0, 0, 0])
我想在具有 List_small 值的 List_large 位置的数组“检查”中有 1。因此,我想最终拥有这个数组:
array([1, 0, 1, 0])
请问我该怎么办?
解决方案
作为使用三元运算符的列表理解:
>>> List_large = ['a','b','c','d']
>>> List_small = ['a','c']
>>> np.array([1 if c in List_small else 0 for c in List_large])
array([1, 0, 1, 0])
推荐阅读
- mysql - Mysql 服务器无法以 AssertionError 启动
- javascript - 在 window.load 上,有没有办法知道某个方法是否在任何地方使用
- java - java中的粗体字符串而不是将该字符串发送到打印机
- c# - 此 LINQ 查询如何获取此字符串的初始值?
- html - 当文本数量不同时,底部水平对齐按钮
- python - DBSCAN 根据什么检测异常值?异常值的标准是什么
- php - 如何通过 Swift 使用 multipart/form-date 将文件发送到服务器
- c++ - QStandardItemModel 中项目的重新分配
- maven-plugin - 使用plantuml maven插件生成UML图时出错
- firebase - Flutter-Firebase 存储上传视频