在(Julia)中解压缩数组的正确方法 [英] Correct way to (un)zip arrays in Julia

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

问题描述

我正在使用Julia中的PyPlot库进行绘图,而散点函数似乎带来了一点点"的不便,即仅接受坐标作为两个参数:一个数组用于所有x值,另一个数组用于所有y值,即

I was using the PyPlot library in Julia for plotting, and the scatter function seems to have a "little" inconvenience, namely, that only accepts the coordinates as two arguments: one array for all x values, and another for all y values, i.e.

scatter(xxs,yys)

带有x=[x1,x2,...]y=[y1,y2,...].

如果我有一个带有坐标点的集合或元组,例如

If I have a set or a tuple with points of coordinates, like,

A=([x1,y1],[x2,y2],...)

直接在python中使用pyplot/matplotlib解决了一个衬纸上的不便,经验证

using pyplot/matplotlib directly in Python solves the inconvenience in one liner, as atested here in StackOverflow:

plt.scatter(*zip(*li))

但是似乎朱莉娅(Julia)上的拉链完全不同.到目前为止,我已经提供了以下解决方案,但似乎不太雅致:

but it seems that zip on Julia works completely different. Until now, I have come with following solution, but it seems quite inelegant:

x=[]
y=[]
for j in selectos
  append!(x,j[2])
   append!(y,j[1])
end

scatter(x,y, marker="o",c="black")

是否有更实用"的或单层(或两层)的方法?

Is there a more "functional" or one-liner (or two liner) approach?

推荐答案

如另一个答案中所述,可以在Julia中使用相同的方法,即scatter(zip(A...)...),但这是非常对于较大的向量,速度较慢,应该避免.

As mentioned in the other answer, one can use the same approach in Julia, i.e., scatter(zip(A...)...), but this is very slow for larger vectors and should be avoided.

另一种可能性是使用getindex.(A, i)获取A中向量的所有i个元素的向量.

Another possibility is to use getindex.(A, i) to get a vector of all ith elements of the vectors in A.

julia> A = [[i, 10i] for i=1:5]
5-element Array{Array{Int64,1},1}:
 [1, 10]
 [2, 20]
 [3, 30]
 [4, 40]
 [5, 50]

julia> getindex.(A,1)
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> getindex.(A,2)
5-element Array{Int64,1}:
 10
 20
 30
 40
 50

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

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