将多个数组组合为一个,按顺序编制索引 [英] Combining multiple arrays into one, indexing sequentially

查看:105
本文介绍了将多个数组组合为一个,按顺序编制索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个数组(最多15个),每个最多可以有1800个对象。



我需要将它们组合成一个数组,然后才能应用用于生成csv文件的分隔符(',')。问题是,当我将它们组合成单个数组时,它必须按顺序组合,比如每个数组的第一个对象应该先插入,然后再插入对象的第二个索引,然后是第三个等。



我可以通过使用for-in循环来实现我想要的结果。然而这并不很快。我觉得它可以使用swift中可用的函数方法以更简洁的方式完成(使用map,reduce和filter函数)。

然而,我无法合并他们完美。任何人都可以帮助我使用swift函数方法来实现结果。



PS:让我知道你是否希望我发布for-in循环代码,但是我认为这不是必需的。

解决方案给定4个(或更多)数组

  let list0:[Int] = [1,2,3,6,7,8,9] 
let list1:[Int] = [10,20,30 ,40,50,60,70,80,90]
let list2:[Int] = [100,200,300,400,500,600,700,800,900]
let list3: [Int] = [1000,2000,3000,4000,5000,6000,7000,8000,9000]

枚举每一个,并将它们放入另一个数组中。

  let lists = [
list0.enumerate()。map {(index:$ 0,array:0,value:$ 1)},
list1.enumerate()。map {(index:$ 0,array:1,value:$ 1)},
list2.enumerate ().map {(index:$ 0,array:2,value:$ 1)},
list3.enumerate()。map {(index:$ 0,array:3,value:$ 1)}
]

现在你可以写出

  let sorted = lists 
.flatten()
.sort { ($ 0.index,$ 0.array)< ($ 1.index,$ 1.array)}
.map {$ 0.value}




[1,10,100,1000,2,20,200,2000,3,30,300,3000,6,40,400,4000,7,50,500,5000,8000, 60,600,6000,9,70,700,7000,80,800,8000,90,900,9000]


I have multiple array (max 15), each can max have 1800 objects.

I need to combine them into a single array, so that I then can apply a delimiter (',') to generate a csv file. The problem is when I combine them into a single array, it has to be sequentially combined, like first objects of each array should be inserted initially followed by the 2nd index of objects then 3rd and so on.

I'm able to achieve the result I want by using a for-in loop. However this doesn't very swifty. I feel it can be done in a much cleaner way using functional methods available in swift (using map, reduce and filter functions).

However I'm not able to combine them perfectly. Can anyone help me with using the swift functional methods to achieve the result.

P.S: Let me know if you want me to post the for-in loop code, but I believe that's not required.

解决方案

Given 4 (or more) arrays

let list0: [Int] = [ 1, 2, 3, 6, 7, 8, 9 ]
let list1: [Int] = [ 10, 20, 30, 40, 50, 60, 70, 80, 90]
let list2: [Int] = [ 100, 200, 300, 400, 500, 600, 700, 800, 900]
let list3: [Int] = [ 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000 ]

enumerate each one and put them into another array

let lists = [
    list0.enumerate().map { (index: $0, array: 0, value: $1) },
    list1.enumerate().map { (index: $0, array: 1, value: $1) },
    list2.enumerate().map { (index: $0, array: 2, value: $1) },
    list3.enumerate().map { (index: $0, array: 3, value: $1) }
]

Now you can write

let sorted = lists
    .flatten()
    .sort { ($0.index, $0.array) < ($1.index, $1.array) }
    .map { $0.value }

[1, 10, 100, 1000, 2, 20, 200, 2000, 3, 30, 300, 3000, 6, 40, 400, 4000, 7, 50, 500, 5000, 8, 60, 600, 6000, 9, 70, 700, 7000, 80, 800, 8000, 90, 900, 9000]

这篇关于将多个数组组合为一个,按顺序编制索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆