解压缩Julia中的元组数组 [英] Unzip an array of tuples in julia

查看:78
本文介绍了解压缩Julia中的元组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个元组数组:

Suppose I have an array of tuples:

arr = [(1,2), (3,4), (5,6)]

使用python我可以做 zip(* arr)== [(1、3、5),(2、4、6)]

With python I can do zip(*arr) == [(1, 3, 5), (2, 4, 6)]

这在朱莉娅身上相当于什么?

What is the equivalent of this in julia?

推荐答案

对于更大的阵列,请使用下面的@ivirshup解决方案.

对于较小的数组,可以使用 zip 进行分割.

For smaller arrays, you can use zip and splitting.

您可以使用 zip()函数

You can achieve the same thing in Julia by using the zip() function (docs here). zip() expects many tuples to work with so you have to use the splatting operator ... to supply your arguments. Also in Julia you have to use the collect() function to then transform your iterables into an array (if you want to).

这些功能在起作用:

arr = [(1,2), (3,4), (5,6)]

# wtihout splatting
collect(zip((1,2), (3,4), (5,6)))

# Output is a vector of arrays:
> ((1,3,5), (2,4,6))

# same results with splatting
collect(zip(arr...))
> ((1,3,5), (2,4,6))

这篇关于解压缩Julia中的元组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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