3
$\begingroup$

This question already has an answer here:

I am dealing with Tuples of n lists each having potentially different length:

longList = Tuples[list1, list2, ..., listn]:

Since I have to iterate over the elements of longList, precomputing of longList becomes a memory issue very quickly. That's why I'm looking for a lazy list implementation where I can access the ith element of longList without precomputing the whole list.

I came across this piece of code which does almost exactly what I want: Lazy lists of Tuples and Subsets

Unfortunately, it seems to only work with Tuples made from a common list, i.e. tuples taking the form:

Tuples[list,n]

I wonder wheter there is a way to either adjust the linked code or whether anyone is aware of a solution to this type of problem.

Thank you very much

$\endgroup$

marked as duplicate by Mr.Wizard list-manipulation Aug 12 '17 at 2:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Is this answering your question? $\endgroup$ – John Joseph M. Carrasco Aug 10 '17 at 16:00
  • 1
    $\begingroup$ Yes and no @John. I actually implemented one of the suggested solutions but they work slightly differently. What the solutions in your link do is iterate through all permutations. What I was hoping for was a way to directly access the ith permutation. $\endgroup$ – slosud Aug 10 '17 at 16:30
  • 1
    $\begingroup$ I think the first part of Leonid Shifrin's answer in that link handles what you want. Specifically take[listOfLists__,{start_,end_}] gives you just one element when start=end. Example: take[{a /@ Range[10^6], b /@ Range[10^6], c /@ Range[10^6]}, {23 10^5, 23 10^5}] $\mapsto$ {{a[1], b[3], c[300000]}}. $\endgroup$ – John Joseph M. Carrasco Aug 10 '17 at 16:50
  • $\begingroup$ You are absolutely right, thanks a lot! I don't know how I missed this. Do you by any chance see a way to obtain only those tuples where the ith entry is equal to some value y? I.e. all tuples satisfying (x1, x2, ...,x(i-1),y,x(i+1),...). $\endgroup$ – slosud Aug 10 '17 at 16:59
  • 1
    $\begingroup$ Wouldn't that literally be restricting your list of lists to only include the element you're interested in y's parent list? i.e. if I only wanted everyone who's second entry was b[15]: take[{a/@Range[10^6], (b/@Range[10^6])[[{15}]], c/@Range[10^6]}, {start,end}] $\endgroup$ – John Joseph M. Carrasco Aug 10 '17 at 17:01