Kotlin用前面两个索引的元素压缩每个元素
我有一个e1,e2,e3,e4,e5的列表。。。。我想用e3压缩e1,用e4压缩e2等等 我找不到一个zip方法,该方法允许您选择使用当前元素压缩哪个元素Kotlin用前面两个索引的元素压缩每个元素,kotlin,Kotlin,我有一个e1,e2,e3,e4,e5的列表。。。。我想用e3压缩e1,用e4压缩e2等等 我找不到一个zip方法,该方法允许您选择使用当前元素压缩哪个元素 items.map { item -> item.zipWithNext() } [e1, e2, e3, e4, e5, e6, e7, e8] -> [(e1, e3), (e2, e4), (e5, e7), (e6, e8)] 对于此要求,列表的大小必须是4的倍数,如下所示: val list = mutableLis
items.map { item -> item.zipWithNext() }
[e1, e2, e3, e4, e5, e6, e7, e8] -> [(e1, e3), (e2, e4), (e5, e7), (e6, e8)]
对于此要求,列表的大小必须是4的倍数,如下所示:
val list = mutableListOf("e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8")
因此,您可以首先交换位置1和2、5和6、9和10中的项目,依此类推:
for (i in 0..list.size / 4 - 1)
list[4 * i + 1] = list[4 * i + 2].also { list[4 * i + 2] = list[4 * i + 1] }
现在的清单如下:
[e1, e3, e2, e4, e5, e7, e6, e8]
最后,使用chunked2将列表拆分为两个连续项的列表:
val newList = list.chunked(2)
println(newList)
将打印:
[[e1, e3], [e2, e4], [e5, e7], [e6, e8]]
如果您的初始列表不可变或希望其保持不变,则创建一个新的可变列表并对其进行操作:
val originalList = listOf("e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8")
val list = originalList.toMutableList()
对于此要求,列表的大小必须是4的倍数,如下所示:
val list = mutableListOf("e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8")
因此,您可以首先交换位置1和2、5和6、9和10中的项目,依此类推:
for (i in 0..list.size / 4 - 1)
list[4 * i + 1] = list[4 * i + 2].also { list[4 * i + 2] = list[4 * i + 1] }
现在的清单如下:
[e1, e3, e2, e4, e5, e7, e6, e8]
最后,使用chunked2将列表拆分为两个连续项的列表:
val newList = list.chunked(2)
println(newList)
将打印:
[[e1, e3], [e2, e4], [e5, e7], [e6, e8]]
如果您的初始列表不可变或希望其保持不变,则创建一个新的可变列表并对其进行操作:
val originalList = listOf("e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8")
val list = originalList.toMutableList()
你应该做很多工作
val items = listOf("e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8")
val partitionedItems = items.withIndex().partition { (index, _) -> index % 2 == 0 }
val oddList = partitionedItems.first.map { it.value }
val evenList = partitionedItems.second.map { it.value }
这里有奇数和偶数列表,如下所示:
[e1、e3、e5、e7]
[e2、e4、e6、e8]
下一步做一个拉链
oddList.zipWithNext()
这会创建这样的数据
[e1、e3、e3、e5、e5、e7]
但我们不需要这个项目:
e3,e5
因此,我们应该过滤它
val oddListPair = oddList.zipWithNext().withIndex().filter { (index, _) -> index % 2 == 0 }.map { it.value }
终于有了
[e1、e3、e5、e7]
在最后一步中,我们应该合并两个奇偶列表
oddListPair.zip(evenListPair){ a,b -> listOf(a,b)}.flatten()
完整代码如下
val items = listOf("e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8")
val partitionedItems = items.withIndex().partition { (index, _) -> index % 2 == 0 }
val oddList = partitionedItems.first.map { it.value }
val evenList = partitionedItems.second.map { it.value }
val oddListPair = oddList.zipWithNext().withIndex().filter { (index, _) -> index % 2 == 0 }.map { it.value }
val evenListPair = evenList.zipWithNext().withIndex().filter { (index, _) -> index % 2 == 0 }.map { it.value }
oddListPair.zip(evenListPair){ a,b -> listOf(a,b)}.flatten()
你应该做很多工作
val items = listOf("e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8")
val partitionedItems = items.withIndex().partition { (index, _) -> index % 2 == 0 }
val oddList = partitionedItems.first.map { it.value }
val evenList = partitionedItems.second.map { it.value }
这里有奇数和偶数列表,如下所示:
[e1、e3、e5、e7]
[e2、e4、e6、e8]
下一步做一个拉链
oddList.zipWithNext()
这会创建这样的数据
[e1、e3、e3、e5、e5、e7]
但我们不需要这个项目:
e3,e5
因此,我们应该过滤它
val oddListPair = oddList.zipWithNext().withIndex().filter { (index, _) -> index % 2 == 0 }.map { it.value }
终于有了
[e1、e3、e5、e7]
在最后一步中,我们应该合并两个奇偶列表
oddListPair.zip(evenListPair){ a,b -> listOf(a,b)}.flatten()
完整的代码就是这个
val items = listOf("e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8")
val partitionedItems = items.withIndex().partition { (index, _) -> index % 2 == 0 }
val oddList = partitionedItems.first.map { it.value }
val evenList = partitionedItems.second.map { it.value }
val oddListPair = oddList.zipWithNext().withIndex().filter { (index, _) -> index % 2 == 0 }.map { it.value }
val evenListPair = evenList.zipWithNext().withIndex().filter { (index, _) -> index % 2 == 0 }.map { it.value }
oddListPair.zip(evenListPair){ a,b -> listOf(a,b)}.flatten()
items.zipWithNext函数本质上是items.zipitems.drop1的优化版本
由于您需要使用两个位置之外的项目来压缩每个项目,因此您可以使用
items.zipitems.drop2:
items.zipWithNext函数本质上是items.zipitems.drop1的优化版本
由于您需要使用两个位置之外的项目来压缩每个项目,因此您可以使用
items.zipitems.drop2:
如果找不到,可以使用实现自己的。例如,fun List.zipWithNext:List{//…}更新标记,考虑到标题正确指出了它可能是特定于Kotlin的。如果找不到,可以使用实现自己的标记。例如,fun List.zipWithNext:List{//…}更新标记,考虑到标题正确指出它是Kotlin特定的。这不是OP想要的。@forpas不是吗?怎么会这样?这是预期结果:[e1,e3,e2,e4,e5,e7,e6,e8]好吧,就这样吧。我希望这个片段仍然可以作为问题第一部分的答案:我想用e3压缩e1,用e4压缩e2,所以我只想说你的片段实际上也很有用。当处理另一个类似的问题时,它就派上了用场!这不是OP想要的。@forpas不是吗?怎么会这样?这是预期结果:[e1,e3,e2,e4,e5,e7,e6,e8]好吧,就这样吧。我希望这个片段仍然可以作为问题第一部分的答案:我想用e3压缩e1,用e4压缩e2,所以我只想说你的片段实际上也很有用。当处理另一个类似的问题时,它就派上了用场!