< Back to IRCAM Forum

Rewriting indexed sublists

Hi,
I would like to know how to rewrite a list of lists structured in the form: (a (1 2 3) b (1 2 3 4) c (5 6 7)) – where a, b, c are the indices for each sublist – rearranging it in the following new form: ((a 1 a 2 a 3) (b 1 b 2 b 3 b 4) (c 5 c 6 c 7)). Basically, I’d need to append the indexes to each element of their corresponding sublist. Is there already a built-in function in OM that does this trick? Thanks in advance for your concern.
Best regards,
Francesco Vitale

ciao francesco,

what results are you expecting? perhaps this?
(((a 1) (a 2) (a 3)) ((b 1) (b 2) (b 3) (b 4)) ((c 1) (c 2) (c 3)))

i’m not sure there’s a library for that, and i’d happy to know if one is available. but if you need a solution right now perhaps this would help…
just copy it in an internal lisp function.

(lambda (lista) (loop for i from 0 to (1- (length lista)) by 2 collect
(loop for j in (nth (1+ i) lista) collect (list (nth i lista) j))))

regards,
m.

Salve M.,
thanks for your reply. I’ve tested your function starting with the data:

(0.0 (0.0) (0.0) (0.0) (0.0))
(0.009366 (0.0) (0.0) (0.0) (0.0) (0.0) (0.0))
(0.032666 (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (205.62857) (205.62857) (205.62857))
(0.055967003 (22.446864) (22.446864) (22.446864) (22.446864) (216.04784) (411.25488))

Unfortunately, with your lambda expression I got this result:

((((0.0 (0.0) (0.0) (0.0) (0.0))) (0.009366 (0.0) (0.0) (0.0) (0.0) (0.0) (0.0))))
((((0.032666 (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (205.62857) (205.62857) (205.62857))) (0.055967003 (22.446864) (22.446864) (22.446864) (22.446864) (216.04784) (411.25488))))

But what I’d need is to get the following result instead:

((0.0 0.0) (0.0 0.0) (0.0 0.0) (0.0 0.0))
((0.009366 0.0) (0.009366 0.0) (0.009366 0.0) (0.009366 0.0) (0.009366 0.0) (0.009366 0.0))
((0.032666 11.223192) (0.032666 11.223192) (0.032666 11.223192) (0.032666 11.223192) (0.032666 11.223192) (0.032666 11.223192) (0.032666 11.223192) (0.032666 11.223192) (0.032666 205.62857) (0.032666 205.62857) (0.032666 205.62857))
((0.05596700 22.446864) (0.05596700 22.446864) (0.05596700 22.446864) (0.05596700 22.446864) (0.05596700 216.04784) (0.05596700 411.25488)).

I hope that with this practical example the task will appear more clearly. Thanks again.
Best,
Francesco

Hi Francesco,

I don’t know if there is a built-in function that realizes what you are seeking for, but if useful I have realized a little patch that, I suppose, perhaps obtains what you are requesting (it is not very straigthforward, but it is the only solution I have found). I enclose it. I am not very skilled in OM lisp code at the moment, but perhaps from this patch someone can realize a function to be used as an object.

Best

Fabio

FV.omp (7.53 KB)

Many thanks Fabio for your effort.
However, you patch – as you may already have noticed – doesn’t work with the kind of data I provided in my last post. This task goes beyond my current OM skills too… Any further help is greatly appreciated.
Best,
Francesco

OK, I have understood my error: of course my algorithm does not run with odd number of elements in initial list, sorry. I try to think a moment, it is an interesting problem, if I find a solution I post it here.

Ciao

Fabio

Briefly: I am observing that your two lists, that is

(a (1 2 3) b (1 2 3 4) c (5 6 7))

and

(0.0 (0.0) (0.0) (0.0) (0.0))
(0.009366 (0.0) (0.0) (0.0) (0.0) (0.0) (0.0))
(0.032666 (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (205.62857) (205.62857) (205.62857))
(0.055967003 (22.446864) (22.446864) (22.446864) (22.446864) (216.04784) (411.25488))

have a different setting in parenthesis, at least it seem to me. I think that the second list, to be similar to the structure of first one, should have to be something like

(0.0 (0.0 0.0 0.0 0.0) 0.009366 (0.0 0.0 0.0 0.0 0.0 0.0) 0.032666 (11.223192 11.223192 11.223192 11.223192 11.223192 11.223192 11.223192 11.223192 205.62857 205.62857 205.62857) 0.055967003 (22.446864 22.446864 22.446864 22.446864 216.04784 411.25488))

In every list you must have the first element (the index) and a second element that is the list of elements to be indexed. I hope to be clear, I apologize for my English…

My patch very probably will continue to be not correct if the number of elements in initial list is odd. But preliminarly I think you should to decide what type of format to have in your initial list.

I hope my observations can be useful.

Ciao

Fabio

Well, sorry, no matter of odd or even number of elements. Of course, because you have always an index and a sublist, the number of elements is always even.

I enclose the previous example with a slight modification (eliminated a flat in omloop), it seems to me that it obtains the result you are requesting, premising that you must introduce the initial list according to the format specified in previous my post.

I hope this is good for you.

Ciao

Fabio

FV1.omp (8.16 KB)

Sorry Fabio,
I forgot to remark that the difference between

(a (1 2 3) b (1 2 3 4) c (5 6 7))

and

(0.0 (0.0) (0.0) (0.0) (0.0))
(0.009366 (0.0) (0.0) (0.0) (0.0) (0.0) (0.0))
(0.032666 (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (11.223192) (205.62857) (205.62857) (205.62857))
(0.055967003 (22.446864) (22.446864) (22.446864) (22.446864) (216.04784) (411.25488))

in the set of parentheses should not be considered important (in the first example the atom sublists are simply “flattened”). What is important is to get the same result that you achieved in your second patch (thanks again for that one). Now the main problem is: what shall we do if the lists are contained in a textfile? and if the length of the list is an odd number (in fact I have a list with length 1141)? I’ll see what I can modify in your patch in order to solve these issues.
Best,
Francesco

Hi Francesco,

The odd/even question I think should be not relevant, because, if I have good understood, your lists are always composed by couples of index + sublist elements, so the final number of elements of top list, to say, is always an even number.

About the use of a textfile object, you can eventually adjust last parameter (text, data list, list or value - I think perhaps list or text is what you need), to have the form of output you need. But the format of your output list should be, I think, (index (sublist) index (sublist) …).

Ciao

Fabio

Hi,
I wasn’t able to find 1 solution to your both examples, so here’re 2 omloops that make the trick.
Let me know if it’s working for you.
Cheers!
RC

cdr_test1.omp (9.46 KB)

Hi RC,
your loops are exactly what’s needed! They work perfectly also for long lists stored in a textfile. Thanks for your brilliant solution.
Best,
Francesco

Hi RC,

Very elegant solution, congratulations! Thank you for sharing.

Best

Fabio