3
$\begingroup$

I have a list of the form

{{0, 0, 0, 0, 0, 0, a, b, c, d, e, f}, {0, 0, 0, 0, 0, 1, a, c, b, d, f, e}, {2, 2, 2, 2, 1, 2, b, f, c, a, d, e}, ... } 

and I would like to translate it into variables of the form w[c1, c2, x, y] following these rules:

  • the values c1 and c2 are taken from the numeric values of each sublist in order
  • the values of x and y are taken from the string values of each sublist in order but each pair is reordered in alphanumeric order.

Following my example, I expect something of the form:

{w[0, 0, a, b] w[0, 0, c, d] w[0, 0, e, f], w[0, 0, a, c] w[0, 0, b, d] w[0, 1, e, f], w[2, 2, b, f] w[2, 2, a, c] w[1, 2, d, e], ...}

(notice the terms w[2, 2, c, a] must be w[2, 2, a, c] and similarly with w[0, 1, e, f])

I can use a replace function of the form /. {c1_,c2_,c3_,c4_, ..., a1_,a2_,a3_,a4,...} -> w[c1,c2,a1,a2] ... but it does not seem a very optimal solution. Besides, I will need to generalize it in case I change the number of elements in my initial list.

This question is a follow-up to a previous question: Fast enumeration of all perfect matchings in complete graph, where the solution was Ok for that purpose, but now I need to recover the variables in this particular form.

$\endgroup$
2
  • 1
    $\begingroup$ Are the products between the different w[] meant to be products? Or is it a typo for missing comas? $\endgroup$
    – kcr
    May 27, 2021 at 16:30
  • $\begingroup$ They have to be products $\endgroup$
    – AlbaCL
    May 27, 2021 at 18:26

1 Answer 1

4
$\begingroup$
ClearAll[w, reShape]

SetAttributes[w, Orderless]

reShape = w @@@ Join[##, 2] & @@ ArrayReshape[#, {2, 3, 2}] &;

Example:

lst = {{0, 0, 0, 0, 0, 0, a, b, c, d, e, f}, {0, 0, 0, 0, 0, 1, a, c, 
    b, d, f, e}, {2, 2, 2, 2, 1, 2, b, f, c, a, d, e}};

Map[reShape] @ lst

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you very much! That works perfectly! $\endgroup$
    – AlbaCL
    May 27, 2021 at 18:33

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.