来自http://php.net/manual/en/function.iterator-apply.php
我从文档中无法理解为什么在下面的示例中需要第三个参数,即array($ it)。
function print_caps(Iterator $iterator) {
echo strtoupper($iterator->current()) . "\n";
return TRUE;
}
$it = new ArrayIterator(array("Apples", "Bananas", "Cherries"));
iterator_apply($it, "print_caps", array($it));
?>
答案 0 :(得分:1)
如果您查看第二个参数(回调)的文档,该手册将说:
此函数仅接收给定的args,因此默认情况下为null。
这是一种相当尴尬的说法,即默认情况下(即如果没有第三个参数),将在不带参数的情况下调用回调。
换句话说,这是
def process_data_curve_fitting(temperature, strain):
#mean_T = (temperature[[i for i in temperature.columns.tolist() if str(i)[:2] == 'TW']].mean(axis=1))
print("process data")
T1 = temperature['T1'].tolist()
T2 = temperature['T2'].tolist()
T3 = temperature['T3'].tolist()
T4 = temperature['T4'].tolist()
T5 = temperature['T5'].tolist()
T6 = temperature['T6'].tolist()
T7 = temperature['T7'].tolist()
T8 = temperature['T8'].tolist()
T9 = temperature['T9'].tolist()
T10 = temperature['T10'].tolist()
df = pd.DataFrame(list(zip(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)))
mean_T = df.mean(axis = 1)
print(mean_T)
Sensor_Names = [ 'W_A1', 'W_A2', 'W_F1', 'W_F2', 'W_F4', 'W_S1', 'W_S2', 'W_S3', 'W_S4', 'W_KF1', 'W_KF2', 'W_KF3', 'W_KF4', 'W_DB1', 'W_DB2']
ys = []
for i in range(len(strain)):
cof = np.polyfit(mean_T, strain[i], 2)
poly = np.polyval(cof, mean_T)
ys.append(poly)
print (cof)
print (poly)
for i in range(len(strain)):
fig = plt.figure()
plt.scatter(mean_T, strain[i],s=0.1)
# fig.savefig(r'c:\\ahmed\\'+Sensor_Names[i]+'.png')
plt.plot(mean_T, ys[i], color='r')
fig.savefig(r'c:\\ahmed\\'+"Curve_fitting__" + Sensor_Names[i]+'.png',dpi=300)
plt.ylabel('strain' + Sensor_Names[i])
plt.xlabel('temperature')
大致等于:
iterator_apply($some_iterator, $some_callback);
由于回调未传递任何参数,因此无法看到迭代器或当前值。
如果改为传递第三个值,则每次使用这些参数调用该函数,因此:
foreach ( $some_iterator as $value ) {
$some_callback();
}
大致等于:
iterator_apply($some_iterator, $some_callback, [1,2,3]);
因此,您正在查看的示例因此通过将回调作为参数传递给回调,从而允许回调在每次调用迭代器时都看到迭代器;即:
foreach ( $some_iterator as $value ) {
$some_callback(1,2,3);
}
大致等于:
iterator_apply($some_iterator, $some_callback, [$some_iterator]);