如何在NSMutableDictionary中比较NSArray的元素?
How to compare the elements of NSArray in NSMutableDictionary?
我的 NSMutableDictionary
包含四个 NSArray
及其各自的密钥。这些 NSArray
的大小为 2,包含二维坐标。我想获得其中最常见的坐标。例如,如果三个数组共有一个坐标,那将是我的首选。我怎样才能找到至少两个数组共有的任何坐标?
这是一个工作示例。我假设你的字典看起来像 coordinatesDict
.
NSDictionary *coordinatesDict = @{@"first": @[@11.58, @40.20], @"second": @[@12.12, @100.12], @"third": @[@11.58, @40.20], @"fourth": @[@13.2, @14.5]};
NSCountedSet *coordinates = [NSCountedSet setWithArray:[coordinatesDict allValues]];
for (NSArray *coordinateArray in coordinates) {
NSUInteger coordinateCount = [coordinates countForObject:coordinateArray];
NSLog(@"Array %@ is included %lu times", coordinateArray, (unsigned long)coordinateCount);
if (coordinateCount >= 2) {
// You found your best coordinate
}
}
下面是一些应该可以工作的代码,使用 NSCountedSet
。
简单来说:
我使用一个 NSCountedSet
来作为 NSSet
来保持出现次数(重复次数)。
然后,我创建了一个 NSArray
根据出现次数对值进行降序排序。
我明确地写了 "count1/count2" 比较,以防你想在出现次数相同的情况下应用不同的值。
NSDictionary *allData = @{@"Key1: %@":@[@(0.0), @(0.1)],
@"Key2: %@":@[@(0.1), @(0.1)],
@"Key3: %@":@[@(0.2), @(0.1)],
@"Key4: %@":@[@(0.1), @(0.1)]};
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:[allData allValues]];
for (NSArray *array in countedSet)
NSLog(@"For %@ counted %@ time(s)", array, @([countedSet countForObject:array]));
NSArray *sortedCountedSetArray = [[countedSet allObjects] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSUInteger count1 = [countedSet countForObject:obj1];
NSUInteger count2 = [countedSet countForObject:obj2];
if (count1 < count2)
return NSOrderedDescending;
if (count1 > count2)
return NSOrderedAscending;
else
return NSOrderedSame; //May want to do additionaly thing (for example if coordinate is closer to actual position, etc.)
}];
NSLog(@"sortedCountedSetArray: %@", sortedCountedSetArray);
NSArray *bestOption = [sortedCountedSetArray firstObject]; //Coordinates the most "popular"
NSLog(@"BestOption: %@", bestOption);
输出是:
> For (
"0.1",
"0.1"
) counted 2 time(s)
> For (
"0.2",
"0.1"
) counted 1 time(s)
> For (
0,
"0.1"
) counted 1 time(s)
> sortedCountedSetArray: (
(
"0.1",
"0.1"
),
(
"0.2",
"0.1"
),
(
0,
"0.1"
)
)
> BestOption: (
"0.1",
"0.1"
)
基本上你想找到出现次数最多的一对坐标。
所以你可以创建一个所有坐标的数组并找到它的模式。
NSMutableArray *allCoordinates = [NSMutableArray new];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([key isEqualToString:@"arrayKey1"] || [key isEqualToString:@"arrayKey2"]) {
NSArray *coordinates = (NSArray *)obj;
[coordinates enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[allCoordinates addObject:coordinates];
}];
}
}];
现在,您需要编写一个自定义方法来查找坐标数组的模式(附加条件是频率 >=3)。
我的 NSMutableDictionary
包含四个 NSArray
及其各自的密钥。这些 NSArray
的大小为 2,包含二维坐标。我想获得其中最常见的坐标。例如,如果三个数组共有一个坐标,那将是我的首选。我怎样才能找到至少两个数组共有的任何坐标?
这是一个工作示例。我假设你的字典看起来像 coordinatesDict
.
NSDictionary *coordinatesDict = @{@"first": @[@11.58, @40.20], @"second": @[@12.12, @100.12], @"third": @[@11.58, @40.20], @"fourth": @[@13.2, @14.5]};
NSCountedSet *coordinates = [NSCountedSet setWithArray:[coordinatesDict allValues]];
for (NSArray *coordinateArray in coordinates) {
NSUInteger coordinateCount = [coordinates countForObject:coordinateArray];
NSLog(@"Array %@ is included %lu times", coordinateArray, (unsigned long)coordinateCount);
if (coordinateCount >= 2) {
// You found your best coordinate
}
}
下面是一些应该可以工作的代码,使用 NSCountedSet
。
简单来说:
我使用一个 NSCountedSet
来作为 NSSet
来保持出现次数(重复次数)。
然后,我创建了一个 NSArray
根据出现次数对值进行降序排序。
我明确地写了 "count1/count2" 比较,以防你想在出现次数相同的情况下应用不同的值。
NSDictionary *allData = @{@"Key1: %@":@[@(0.0), @(0.1)],
@"Key2: %@":@[@(0.1), @(0.1)],
@"Key3: %@":@[@(0.2), @(0.1)],
@"Key4: %@":@[@(0.1), @(0.1)]};
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:[allData allValues]];
for (NSArray *array in countedSet)
NSLog(@"For %@ counted %@ time(s)", array, @([countedSet countForObject:array]));
NSArray *sortedCountedSetArray = [[countedSet allObjects] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSUInteger count1 = [countedSet countForObject:obj1];
NSUInteger count2 = [countedSet countForObject:obj2];
if (count1 < count2)
return NSOrderedDescending;
if (count1 > count2)
return NSOrderedAscending;
else
return NSOrderedSame; //May want to do additionaly thing (for example if coordinate is closer to actual position, etc.)
}];
NSLog(@"sortedCountedSetArray: %@", sortedCountedSetArray);
NSArray *bestOption = [sortedCountedSetArray firstObject]; //Coordinates the most "popular"
NSLog(@"BestOption: %@", bestOption);
输出是:
> For (
"0.1",
"0.1"
) counted 2 time(s)
> For (
"0.2",
"0.1"
) counted 1 time(s)
> For (
0,
"0.1"
) counted 1 time(s)
> sortedCountedSetArray: (
(
"0.1",
"0.1"
),
(
"0.2",
"0.1"
),
(
0,
"0.1"
)
)
> BestOption: (
"0.1",
"0.1"
)
基本上你想找到出现次数最多的一对坐标。 所以你可以创建一个所有坐标的数组并找到它的模式。
NSMutableArray *allCoordinates = [NSMutableArray new];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([key isEqualToString:@"arrayKey1"] || [key isEqualToString:@"arrayKey2"]) {
NSArray *coordinates = (NSArray *)obj;
[coordinates enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[allCoordinates addObject:coordinates];
}];
}
}];
现在,您需要编写一个自定义方法来查找坐标数组的模式(附加条件是频率 >=3)。