3

I want to generate all the possible sequences of a specific length from a integer interval. (np.arange(start,end)) If it was only sub-multisets of a set of a specific length I could go for: https://stackoverflow.com/a/4941846/257640 Which I am not sure if it would be the most efficient.

So the "subseq" function will be like this:

subseq(np.arange(1,4),2)

should return:

[[1,1],
[1,2],
[1,3],
[2,2],
[2,3],
[3,3]]

Thank you.

Community
  • 1
  • 1
Cupitor
  • 11,007
  • 19
  • 65
  • 91
  • 7
    I think you're looking for `itertools.combinations_with_replacement`, but I don't know what an integer interval is and I'm not sure why you're concerned about the efficiency. – DSM Mar 25 '13 at 18:58
  • @DSM, I will clarify that in edit. Thanks I am concerned about efficiency because I will deal with large sets with cardinals around 100. Plus, is this function faithful? – Cupitor Mar 25 '13 at 19:00
  • 1
    I think @DSM has answered your question. `list(combinations_with_replacement(range(1,4), 2))` generates your example output, as a list of tuples. See if, for example, `combinations_with_replacement(range(1,5), 3)` does what you want. – Warren Weckesser Mar 25 '13 at 19:46

0 Answers0