Sweet, this is an incredibly fun language. Thanks!
On Friday, July 25, 2014 1:37:34 PM UTC-4, Leah Hanson wrote: > > 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 > ~~~ > > > On Fri, Jul 25, 2014 at 12:29 PM, <yaois...@gmail.com <javascript:>> > wrote: > >> 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 >> >> On Friday, July 25, 2014 11:05:45 AM UTC-4, yaois...@gmail.com wrote: >>> >>> Ah, thank you! >>> >>> This Google group community is fantastic. >>> >>> On Thursday, July 24, 2014 6:32:19 PM UTC-4, Kevin Squire wrote: >>>> >>>> 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 >>>> >>>> >>>> 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. >>>>> >>>>> On Thursday, July 24, 2014 5:30:44 PM UTC-4, Leah Hanson wrote: >>>>> >>>>>> 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 >>>>>> >>>>>> >>>>>> >>>>>> On Thu, Jul 24, 2014 at 4:15 PM, <yaois...@gmail.com> wrote: >>>>>> >>>>>>> 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 >>>>>>> >>>>>> >>>>>> >>>> >