7

I have a list like following

list = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};

I want get this list by a more better method:

I don't care the ordering of its elements.This is my current method.I'm missed in cloud when I manage to write it.

Flatten[MapThread[
  Distribute[List[##], List, List, List, UndirectedEdge] &, {{a, b}, 
   list}]]

{a<->1,a<->2,a<->3,a<->4,a<->5,b<->6,b<->7,b<->8,b<->9,b<->10}

rhermans
  • 36,518
  • 4
  • 57
  • 149
yode
  • 26,686
  • 4
  • 62
  • 167

4 Answers4

14

Since it hasn't been proposed yet:

Inner[UndirectedEdge, {a, b}, {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}, Sequence]
{a <-> 1, b <-> 6, a <-> 2, b <-> 7, a <-> 3,
 b <-> 8, a <-> 4, b <-> 9, a <-> 5, b <-> 10}

(You did say that "I don't care (about) the ordering of its elements"! ;))

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
11

As per request:

Join@@ MapThread[Thread[#1 <-> #2] &, {{a, b}, list}] 

Flatten or Catenate to flatten list result from MapThread.

See comment by Kuba below.

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
6

How about:

{a, b} + list /. a_ + b_ :> a <-> b // Flatten
xzczd
  • 65,995
  • 9
  • 163
  • 468
3
♭ = ## & @@ (♯  #1 <-> ♯) /@ #2 & @@@ ({##}) &

Mathematica graphics

♭[{a, b}, list]

Mathematica graphics

or

♭2 = #2 <-> # & @@@ (## & @@@ +##) &;
♭2[{a, b}, list]

Mathematica graphics

kglr
  • 394,356
  • 18
  • 477
  • 896