我得到了一个日期数组(日期是从0到371的数字)。
private void btnCalc_Click(object sender, EventArgs e)
{
tbMax.Text = values.Max().ToString();
tbMin.Text = values.Min().ToString();
float sum = values.Sum();
tbSum.Text = sum.Tosting();
float average = sum / values.Length;
tbAverage.Text = average.ToString();
}
,然后清空数组dates = [234, 12, 343, ...]
(大小为372)。
date_counter
对于date_counter = [0, 0, ..., 0]
之后的每个日期,我想增加dates
。如何使该操作向量化?
答案 0 :(得分:0)
计算值的出现次数,然后更新日期计数器。
例如,要获得增量:
unique, counts = np.unique(dates, return_counts=True)
然后:
date_counter[unique] += counts
答案 1 :(得分:0)
我认为// ChessPiece copy constructor
public ChessPiece(ChessPiece other) {
this.Position = new int[2] { other.Position[0], other.Position[1]};
this.isWhite = other.isWhite;
this.movementType = other.movementType;
this.hasMoved = other.hasMoved;
this.turnDoubleMoved = other.turnDobleMoved;
}
// instead of boardLayout.Clone() in the ChessBoardSim constructor:
for (int x=0 ; x<8;x++){
for(int y=0; y<8;y++){
if (boardLayout[x,y] != null)
this.boardLayout[x,y] = new ChessPiece(boardLayout[x,y]);
}
}
// In SimulateBoard, take out the redundant Clone call
ChessBoardSim simBoard = new ChessBoardSim(boardLayout, turnNumber);
是您的朋友。它完全符合您的要求:计算项目并将其放入“存储桶”中。一个较小的(人类可读的)示例:
np.histogram
每个索引对应于值等于该索引值的项目数(即In [9]: vals = [9, 0, 1, 2, 2, 9, 3, 7, 8, 9, 9]
In [10]: date_counter, _ = np.histogram(vals, bins=np.arange(11)) # 11 -> 373 for your case
In [11]: date_counter
Out[11]: array([1, 1, 2, 1, 0, 0, 0, 1, 1, 4], dtype=int64)
中的最后一个数字是date_counter
,位于索引4
处,意味着有四个9
s。
HTH。