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.

For a social network analysis I have two columns. The second column has one, two or more values. Something like this:

data = {{A, {k, l, m}}, {B, {k, l, m, n, o}}, {C, {m, n, o}}}

I would like to make a relation between te value of the first column and for each value in the second column. So I would like to get:

data1 = {A -> k, A -> l, A -> m, B -> k,  B -> l, B -> m, B -> n, 
B -> o, C -> m, C -> n, C -> o}

Anyone a suggestion how to solve this?

share|improve this question

3 Answers 3

up vote 3 down vote accepted
data = {{A, {k, l, m}}, {B, {k, l, m, n, o}}, {C, {m, n, o}}}

Flatten@(Thread[#[[1]] -> #[[2]]] & /@ data)

(* {A->k,A->l,A->m,B->k,B->l,B->m,B->n,B->o,C->m,C->n,C->o} *)
share|improve this answer

Maybe I will delete this answer later, but just for good fun (using Riffle and a funky feature of Partition :P)

Quiet[
 List @@
  Partition[
   Apply[Rule, Delete[Riffle[#[[2]], #[[1]], {1, -2, 2}], 0] & /@ data],
   2, 2, {1, -1}, {}]
 ]

Also quite silly

Delete[Thread@Rule@##, 0] & @@@ data
share|improve this answer
    
+1 for the laugh ;-} –  rasher Apr 9 at 21:39

Not so much different but without slots (#):

Rule @@@ Flatten[Thread /@ data, 1]
share|improve this answer

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.