【发布时间】:2012-09-26 11:59:40
【问题描述】:
Python 有一个集合库。在我的 Web 应用程序中,我使用 collections.Counter(aList).most_common([n]) 来返回 n 中 aList 中最常见的项目。
不错,简洁,相当快。
现在我正在开发一个 iOS 应用程序。 Objective-C 或 Cocoa 框架中是否有任何东西可以为字符串数组提供相同的功能?
假设aList 是NSArrary 的NSStrings。
NSMutableDictionary *aCounter = [NSMutableDictionary dictionaryWithCapacity:[aList count]];
for aString in aList {
NSUInteger keyCount = [aCounter valueForKey:aString];
if keyCount == nil {
[aCounter setValue:1 forKey:aString];
}
else {
[aCounter setValue:(keyCount+1) forKey:aString];
}
对压缩此内容或改进其性能有何建议?
【问题讨论】:
标签: arrays cocoa-touch collections counter