python - 以可理解的形式重写 for 循环
问题描述
我想用另一种更容易理解的形式重写下面的python for循环。
def geo_margin(W,b,X,y):
return np.min([margin(W,b, X,y[i])
for i, x in enumerate(X)])
解决方案
你的意思是这样吗?
import numpy as np
def geo_margin(W, b, X, y):
data = [] # Declare a list
for i, x in enumerate(X): # loop through X as index -> i and item -> x
data.append(margin(W, b, X, y[i])) # Add the result of the function call to data
return np.min(data) # return the minimum of the data
推荐阅读
- r - 读取 csv 数据集数字字符时出错
- javascript - 将参数传递给控制器中的函数时,Angularjs ng-model返回未定义
- unity3d - 没有找到“com.onesignal.GcmBroadcastReceiver”类统一
- python - 权限覆盖不更改不和谐频道权限。不和谐.py
- google-cloud-platform - 如何使云构建触发器仅在推送命令上触发?
- javascript - 使用管道从 html 传递的对象中获取数据
- c - 在多线程环境中的 2 个函数之间共享公共变量
- c++ - 编译 `operator delete` 时,C++ 编译器如何工作?
- asp.net - 如何真正删除 VSTS / Azure-devops 上的提交
- javascript - 我可以在对象声明中进行数组解构赋值吗?