最有效的方法筛选一个数组

huangapple go评论105阅读模式
英文:

Most efficient way filtering an array

问题

以下是翻译好的代码部分:

  1. 我有一个数组的数组如下所示:
  2. var masterArray = [
  3. ["String", "String", "String", "1"],
  4. ["String", "String", "String", "2"],
  5. ["String", "String", "String", "3"],
  6. ...
  7. ]
  8. 每个数组的最后一个元素都是一个整数字符串。
  9. 根据每个数组的最后一个元素,将主数组过滤为3个数组,最有效的方法是什么?
  10. 当然,我可以像这样做:
  11. var filteredArray1 = [[String]]()
  12. var filteredArray2 = [[String]]()
  13. var filteredArray3 = [[String]]()
  14. for anArray in masterArray {
  15. if anArray[3] == "1" {
  16. filteredArray1.append(anArray)
  17. } else if anArray[3] == "2" {
  18. filteredArray2.append(anArray)
  19. } else if anArray[3] == "3" {
  20. filteredArray3.append(anArray)
  21. }
  22. }
  23. 但我认为应该有一种更有效的方法来实现这一点,也许可以使用过滤器或谓词?我只是无法找到如何精确定位每个数组的最后一个元素。
  24. **编辑**
  25. 只是为了举个例子,以我认为是一个高效的过滤方式。在一个不相关的用例中,我有一个CoreData实体tagRecords的数组。在这种情况下,由于我知道每个实体都有一个dateStart属性,我可以使用NSPredicate轻松地进行过滤,如下所示:
  26. let currentPred = NSPredicate(format: "dateStart >= %@ && dateStart <= %@", argumentArray: [currentMonth.startOfMonth(), currentMonth.endOfMonth()])
  27. let filteredRecords = tagRecords.filter { currentPred.evaluate(with: $0) }
英文:

I have a array of arrays as follows:

  1. var masterArray = [
  2. [&quot;String&quot;, &quot;String&quot;, &quot;String&quot;, &quot;1&quot;],
  3. [&quot;String&quot;, &quot;String&quot;, &quot;String&quot;, &quot;2&quot;],
  4. [&quot;String&quot;, &quot;String&quot;, &quot;String&quot;, &quot;3&quot;],
  5. ...
  6. ]

Last element of each array is an Int String.

What would be the most efficient way of filtering the master array into 3 arrays based on the last element of each array?

Of course I can do smth like this:

  1. var filteredArray1 = [[String]]()
  2. var filteredArray2 = [[String]]()
  3. var filteredArray3 = [[String]]()
  4. for anArray in masterArray {
  5. if anArray[3] == &quot;1&quot; {
  6. filteredArray1.append(anArray)
  7. } else if anArray[3] == &quot;2&quot; {
  8. filteredArray2.append(anArray)
  9. } else if anArray[3] == &quot;3&quot; {
  10. filteredArray3.append(anArray)
  11. }
  12. }

But I think there should be a more efficient way of achieving this, maybe with filters or predicates? I just can't figure out how to pin-point that last element of each array.

Edit

Just to give an example of what in my opinion is an efficient way of filtering.

In a different unrelated use case, I have an array of CoreData entities tagRecords. In this case, as I know that each entity has dateStart property, I can easily filter it with NSPredicate like so:

  1. let currentPred = NSPredicate(format: &quot;dateStart &gt;= %@ &amp;&amp; dateStart &lt;= %@&quot;, argumentArray: [currentMonth.startOfMonth(), currentMonth.endOfMonth()])
  2. let filteredRecords = tagRecords.filter { currentPred.evaluate(with: $0) }

答案1

得分: 1

A two step solution is to group the data into a dictionary and then extract each array from it using the keys (which I assume are given)

let grouped = Dictionary(grouping: masterArray, by: .last)

let filteredArray1 = grouped["1", default: []]
//...

英文:

A two step solution is to group the data into a dictionary and then extract each array from it using the keys (which I assume are given)

  1. let grouped = Dictionary(grouping: masterArray, by: \.last)
  2. let filteredArray1 = grouped[&quot;1&quot;, default: []]
  3. //...

huangapple
  • 本文由 发表于 2023年6月15日 04:07:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定