1
$\begingroup$

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?

$\endgroup$
3
$\begingroup$
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} *)
$\endgroup$
3
$\begingroup$

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
$\endgroup$
0
2
$\begingroup$

Not so much different but without slots (#):

Rule @@@ Flatten[Thread /@ data, 1]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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