Если вход является DataFrame
, просто используйте agg
:
import pyspark.sql.functions as sqlf
df = sc.parallelize([
("foo", 1.0), ("foo", 2.5), ("bar", -1.0), ("bar", 99.0)
]).toDF(["k", "v"])
df.groupBy("k").agg(sqlf.min("v"), sqlf.max("v"), sqlf.sum("v")).show()
## +---+------+------+------+
## | k|min(v)|max(v)|sum(v)|
## +---+------+------+------+
## |bar| -1.0| 99.0| 98.0|
## |foo| 1.0| 2.5| 3.5|
## +---+------+------+------+
С помощью RDD вы можете использовать statcounter
:
from pyspark.statcounter import StatCounter
rdd = df.rdd
stats = rdd.aggregateByKey(
StatCounter(), StatCounter.merge, StatCounter.mergeStats
).mapValues(lambda s: (s.min(), s.max(), s.sum()))
stats.collect()
## [('bar', (-1.0, 99.0, 98.0)), ('foo', (1.0, 2.5, 3.5))]
Используя свои функции, вы можете сделайте что-то вроде этого:
def apply(x, y, funs=[minFunc, maxFunc, sumFunc]):
return [f(x_, y_) for f, x_, y_ in zip(*(funs, x, y))]
rdd.combineByKey(lambda x: (x, x, x), apply, apply).collect()
## [('bar', [-1.0, 99.0, 98.0]), ('foo', [1.0, 2.5, 3.5])]
Взаимодействие с пользователем в UIImageView по умолчанию отключено . Вы должны включить его явно, чтобы он реагировал на прикосновения.
imageView.userInteractionEnabled = YES;
Swift 3
Это сработало для меня:
self.isUserInteractionEnabled = true
В моем случае это выглядит так:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = CELL_ROUTE_IDENTIFIER;
RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[RouteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
if ([self.routes count] > 0) {
Route *route = [self.routes objectAtIndex:indexPath.row];
UITapGestureRecognizer *singleTapOwner = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(imageOwnerTapped:)];
singleTapOwner.numberOfTapsRequired = 1;
singleTapOwner.cancelsTouchesInView = YES;
[cell.ownerImageView setUserInteractionEnabled:YES];
[cell.ownerImageView addGestureRecognizer:singleTapOwner];
} else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
И селектор:
- (void)imageOwnerTapped:(UISwipeGestureRecognizer *)gesture {
CGPoint location = [gesture locationInView:self.tableView];
NSIndexPath *tapedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *tapedCell = [self.tableView cellForRowAtIndexPath:tapedIndexPath];
NSIndexPath *indexPath = [self.tableView indexPathForCell:tapedCell];
NSUInteger index = [indexPath row];
Route *route = [self.routes objectAtIndex:index];
}