On Monday, January 26, 2015 at 12:09:40 PM UTC-5, Joshua Adelman wrote:
>
>
>
> On Monday, January 26, 2015 at 12:02:10 PM UTC-5, Wai Yip Tung wrote:
>>
>> I'm trying to construct a list of list and do some operation on it. In
>> Python I would do
>>
>> In [7]: ll = [[1,2],[1,2,3],[7]]
>>
>> Say I want to sort them by the length of the list, many function accepts
>> a `key` parameter. In this case I want the `key` to be `len`.
>>
>> In [8]: max(ll, key=len)
>> Out[8]: [1, 2, 3]
>>
>> In [9]: sorted(ll, key=len)
>> Out[9]: [[7], [1, 2], [1, 2, 3]]
>>
>> In Julia, if I enter the literal [[1,2],[1,2,3],[7]], then are join to
>> together into a long list. Through many trial and error I found a way by
>>
>> ll = (Array)[[1,2],[1,2,3],[7]]
>>
>> What is the proper way to construct a list of list?
>>
>> Secondly, Julia's sort support a `by` parameter that does what `key` do
>> in Python. But there is no equivalent in `maximum`. What is the recommended
>> way to find the longest list in the data structure?
>>
>> Wai Yip
>>
>
> I think it may be more idiomatic to initialize the array as:
>
> ll = Array{Int,1}[[1,2],[1,2,3],[7]]
>
> if all elements will be 1d arrays of type Int. You could also use:
>
> ll = Vector{Int}[[1,2],[1,2,3],[7]]
>
> as a short hand. If the arrays are going to be of arbitrary type:
>
> ll = Any[[1,2],[1,2,3],[7]]
>
> Josh
>
And actually, just writing similarly to what you landed on, but without the
parentheses
ll = Array[[1,2],[1,2,3],[7]]
looks like it would also be reasonable since it returns an array of type
Array{Array{T,N},1}
which I believe holds arrays of arbitrary (but homogenous) type and
dimension. I'm just learning Julia myself, so others might have a better
suggestion. Also I think the reason why your original construction just
returned a long list was that you were using the syntactic shorthand for
the hcat command.
Josh