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.

I've got data of the type:

list = {"Class","MW","10 - 11"}

which I would like to split into a number of lists based on the number of characters in the second list element. In the above case, the desired output would be:

newlists = {{"Class","M","10-11"},{"Class","W","10-11"}}

StringSplit[list[[2]],""] gets me in the right direction (the string I want to split will always be in the same position in the list); however, I don't know how to thread this new sublist through the original list.

share|improve this question
1  
Guessing where you're going with this, you might want to look at Generating schedules/timetables in Mathematica –  rm -rf Oct 2 '13 at 15:56
    
@rm-rf Wish I had posted this question before writing the grid layout code... –  bobthechemist Oct 2 '13 at 17:50
    
did I really write one of those answers? sloppy stuff... :) –  cormullion Oct 2 '13 at 19:27
    
@cormullion I found it ironic that a 10k+ rep'er was referring to others as 'experts'. I wonder if SE gives us a way to see what the rep was when a person answered the question. –  bobthechemist Oct 2 '13 at 19:59
1  
@bobthechemist You can find a user's network profile from their site profile. From there, the reputation tab has a nice plot that you can use to quickly find out what the user's rep was on a given date. –  rm -rf Oct 2 '13 at 20:24
add comment

4 Answers 4

up vote 8 down vote accepted

Here's how you can thread them:

newlists = Thread[{#, Characters@#2, ##3}] & @@ list
(* {{"Class", "M", "10 - 11"}, {"Class", "W", "10 - 11"}} *)
share|improve this answer
    
Is there a less ambiguous way to map this command over a list of list: Thread[{#, Characters@#2, ##3}]& @@ # & /@ multilists? I don't know how concerned I should be about nested pure functions. –  bobthechemist Oct 2 '13 at 18:02
1  
@bobthechemist Thread[{#, Characters@#2, ##3}] & @@@ multilists you can also add // Flatten[#, 1] & if you need. –  Kuba Oct 2 '13 at 19:08
add comment

Here is an approach that doesn't use threading.

data = {"Class", "MW", "10 - 11"};
{a, b, c} = data;
new = {a, #, c} & /@ Characters @ b
{{"Class", "M", "10 - 11"}, {"Class", "W", "10 - 11"}}
share|improve this answer
add comment

Maybe not handy but interesting way to apply automatic threading:

SetAttributes[h, Listable];
h @@ MapAt[Characters, list, {2}] /. h -> List
{{"Class", "M", "10 - 11"}, {"Class", "W", "10 - 11"}}
multilist = {{"Class", "MW", "10 - 11"}, {"Class", "MK", "10 - 11"}}

h @@@ MapAt[Characters, multilist, {All, 2}] /. h -> List
{{{"Class", "M", "10 - 11"}, {"Class", "W", "10 - 11"}}, 
   {{"Class", "M", "10 - 11"}, {"Class", "K", "10 - 11"}}}
share|improve this answer
add comment
fun[u_] := Module[{dow},
  dow = Characters[#[[2]]] &@u;
  ReplacePart[u, 2 -> #] & /@ dow]
share|improve this answer
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.