Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Say, I have a list

stuff={{eat,tea,now},{how,did,you,say},{the,cat,ate,was,can},{win,din,sin}}

I have a nested list and I want to find the list that has the most words in it and then I want to find the length of those words. How would I do that?

I thought it was

Max[StringLength[stuff]] 

and

Length[Max[StringLength[stuff]]]

but StringLength doesn't do me any good here since the length of each letter is the same.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Since you want to find the sublist with the maximum length (i.e. most number of words), you don't need to concern yourself with StringLength. A simple solution would be to use Pick to select the longest list and then use StringLength to get the lengths.

With[{L = Length /@ stuff}, StringLength@Pick[stuff, L, Max@L]]
(* {{3, 3, 3, 3, 3}} *)
share|improve this answer
    
Would I also be able to use Select in this case? –  thaibak527 Oct 25 '13 at 21:05
    
Yes, you can use Select. Is there a reason you don't want to use Pick? –  rm -rf Oct 25 '13 at 21:05
    
Because I have only used Pick for a simple set of lists, and not really familiar with it. So, if you use Pick here, the program compares the lists, and "picks" the list with the highest amount of strings? –  thaibak527 Oct 25 '13 at 21:10
    
@thaibak527 Yes, it picks the sublists with the maximum length (because I told it to). –  rm -rf Oct 25 '13 at 21:26
    
Great great, thank you I will remember that for future –  thaibak527 Oct 25 '13 at 22:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.