A couple tuple questions

561 views
Skip to first unread message

yaois...@gmail.com

unread,
Jul 24, 2014, 5:15:51 PM7/24/14
to julia...@googlegroups.com
Hi all,

I thought to just add to my previous thread, but created a new one because the topic is a bit different. Hope y'all don't mind.

Anyhow:

a) How would I concatenate two tuples? Or a tuple with a variable?

Say I have

testtuple = (1,2,3)
testvar = 4

and want to get

newtuple = (1,2,3,4)

(not ((1,2,3),4)

I've tried
newtuple = tuple(testtuple...,testvar...)
newtuple = tuple(testtuple...,testvar)
newtuple = testtuple...,testvar

but none of those have worked to produce the desired result.

b) I have an array of tuples

tuplearray = [(a1,b1,c1),(a2,b2,c2)...,(an,bn,cn)]

How could I then unpack the array into

aarray = [a1,a2...,an]
barray = [b1,b2...,bn]
carray = [c1,c3...,cn]

such that each position in the tuple gets unpacked into a corresponding individual array?

In Python, I would use

alist,blist,clist = zip(*tuplelist)

It appears that

aarray,barray,carray = zip(tuplearray...)

is not the Julia equivalent.

My version of Julia is the .3 RC.

Thanks for your help

Leah Hanson

unread,
Jul 24, 2014, 5:30:44 PM7/24/14
to julia...@googlegroups.com
a) Your second option works for me:
~~~
julia> testtuple = (1,2,3)
(1,2,3)

julia> testvar = 4
4

julia> tuple(testtuple...,testvar)
(1,2,3,4)
~~~

b) I'm not sure what the cleanest code for your example would be, but here's one possibility:

~~~
julia> tuplearray = [(1,2,3),(10,20,30),(100,200,300)]
3-element Array{(Int64,Int64,Int64),1}:
 (1,2,3)      
 (10,20,30)   
 (100,200,300)

julia> aarray = Int[]
0-element Array{Int64,1}

julia> barray = Int[]
0-element Array{Int64,1}

julia> carray = Int[]
0-element Array{Int64,1}

julia> for (a,b,c) in tuplearray
         push!(aarray,a)
         push!(barray,b)
         push!(carray,c)
       end

julia> aarray
3-element Array{Int64,1}:
   1
  10
 100

julia> barray
3-element Array{Int64,1}:
   2
  20
 200

julia> carray
3-element Array{Int64,1}:
   3
  30
 300
~~~

If would be faster to pre-allocate the arrays (to the length of the tuplearray), and then just put the elements in at the correct indices, but I'm not sure if that matters for your application.

-- Leah

yaois...@gmail.com

unread,
Jul 24, 2014, 5:42:51 PM7/24/14
to julia...@googlegroups.com
Leah, thanks for your detailed response --

I will take your advice and try to rework the code to pre-allocate for b).

In regards to a), I keep on getting the error message with the second option
ERROR: type: apply: expected Function, got (Int64,Int64,Int64) 

Just making sure, are you using 0.3? If so, hmm, I would be a bit stumped as to why the same line is not working for me.

Kevin Squire

unread,
Jul 24, 2014, 6:32:19 PM7/24/14
to julia...@googlegroups.com
Hi there,

In regards to a), I keep on getting the error message with the second option
ERROR: type: apply: expected Function, got (Int64,Int64,Int64) 

I'm guessing you accidentally redefined the symbol "tuple" to a tuple:

 julia> tuple(testtuple...,testvar)
(1,2,3,4)

julia> tuple = (1,2,3)
Warning: imported binding for tuple overwritten in module Main
(1,2,3)

julia> tuple(testtuple...,testvar)
ERROR: type: apply: expected Function, got (Int64,Int64,Int64)


For your second example, zip(tuplearray...) is actually about right.  The main differences from what you want are

1) it returns an iterator, not a set of arrays
2) the iterator yields tuples, not arrays

julia> zip(tuplearray)
Zip{(Array{(Int64,Int64,Int64),1},)}(([(1,2,3),(10,20,30),(100,200,300)],))

julia> collect(zip(tuplearray...))
3-element Array{(Int64,Int64,Int64),1}:
 (1,10,100)
 (2,20,200)
 (3,30,300)

Cheers,
   Kevin

yaois...@gmail.com

unread,
Jul 25, 2014, 11:05:45 AM7/25/14
to julia...@googlegroups.com
Ah, thank you!

This Google group community is fantastic.

yaois...@gmail.com

unread,
Jul 25, 2014, 1:29:48 PM7/25/14
to julia...@googlegroups.com
One more tuple question -- I am trying to find a tuple associated with a minimum x[n].

So taking let's say

tuplearray = [(3,2,1),(1,2,3),(3,1,2)]

I want to to find the min x[2] for x in tuplearray

minx2tuple = (3,1,2)

Using the method Kevin showed above, I can unpack the tuples to get three element arrays
 
x1array,x2array,x3array = collect(zip(tuplearray...))

Then, I can do

minx2 = minimum(x2array)

to get

minx2 = 1

However, if I do a comprehension,

[x for x in tuplearray if x[2] == minx2]

or

[x for x in tuplearray if x[2] == 1]

this comprehension does not work. The error I get is -- ERROR: syntax: unexpected "]".

How can I fix the comprehension to make it work in Julia?

Thanks,
Wally

Leah Hanson

unread,
Jul 25, 2014, 1:37:34 PM7/25/14
to julia...@googlegroups.com
Here's one way to go about it:

~~~
julia> tuplearray = [(3,2,1),(1,2,3),(3,1,2)]
3-element Array{(Int64,Int64,Int64),1}:
 (3,2,1)
 (1,2,3)
 (3,1,2)

julia> minimum(tuplearray)
(1,2,3)

julia> minimum(x->x[2],tuplearray)
1

julia> find(x->x[2]==1,tuplearray)
1-element Array{Int64,1}:
 3

julia> tuplearray[3]
(3,1,2)
~~~

Similarly, if you want to use your x2array:
~~~
julia> x2array = [2,2,1]
3-element Array{Int64,1}:
 2
 2
 1

julia> minimum(x2array)
1

julia> find(x->x==1,x2array)
1-element Array{Int64,1}:
 3
~~~

yaois...@gmail.com

unread,
Jul 25, 2014, 1:39:29 PM7/25/14
to julia...@googlegroups.com
Sweet, this is an incredibly fun language. Thanks!
Reply all
Reply to author
Forward
0 new messages