473,387 Members | 1,295 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

some sort of permutations...

hello,

i'm looking for a way to have all possible length fixed n-uples from a
list, i think generators can help, but was not able to do it myself,
maybe some one could point me out to an idea to do it ?

for example, from :
l = [0, 1, 2, 3, 4]

and searching for n-uples of 3, i should produce :
(
(0, 1, 2),
(0, 1, 3),
(0, 1, 4),
(0, 2, 3),
(0, 2, 4),
(0, 3, 4),
(1, 2, 3),
(1, 2, 4),
(1, 3, 4),
(2, 3, 4),
)

does the set module or itertools can help in such cases ? i still have
missed the black magic behind itertools...

best regards
Jul 18 '05 #1
4 1607
"Bernard A." wrote:
i'm looking for a way to have all possible length fixed n-uples from a
list, i think generators can help, but was not able to do it myself,
maybe some one could point me out to an idea to do it ?


did you try googling for "python permutations" ?

here's the first hit:

http://aspn.activestate.com/ASPN/Coo.../Recipe/190465

</F>

Jul 18 '05 #2
On Apr 12, 2005 2:37 AM, Fredrik Lundh <fr*****@pythonware.com> wrote:
"Bernard A." wrote:
i'm looking for a way to have all possible length fixed n-uples from a
list, i think generators can help, but was not able to do it myself,
maybe some one could point me out to an idea to do it ?


did you try googling for "python permutations" ?

here's the first hit:

http://aspn.activestate.com/ASPN/Coo.../Recipe/190465


I've used that recipe a significant amount, and I feel like its
recursion really slows it down (but I haven't been profiled it). Does
anyone know of a non-recursive algorithm to do the same thing?

And, while I'm asking that question, is there a good reference for
finding such algorithms? Do most people keep an algorithms book handy?

Peace
Bill Mill
bill.mill at gmail.com
Jul 18 '05 #3
Bill Mill wrote:
On Apr 12, 2005 2:37 AM, Fredrik Lundh <fr*****@pythonware.com> wrote:
"Bernard A." wrote:
i'm looking ... to have all possible length fixed n-uples from a list...

... http://aspn.activestate.com/ASPN/Coo.../Recipe/190465

And, while I'm asking that question, is there a good reference for
finding such algorithms? Do most people keep an algorithms book handy?

Volume 4 of Knuth's TAOCP (The Art of Computer Programming) is about
combinatorics. Fascicle 2 is out and relevant. Admittedly buying
fascicles is a bit like buying a long chapter at a time, but it is
chock-a-block with great stuff. I paid about twenty bucks a fascicle.

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #4
On Tue, Apr 12, 2005 at 08:41:15AM -0400, Bill Mill wrote:
On Apr 12, 2005 2:37 AM, Fredrik Lundh <fr*****@pythonware.com> wrote:
"Bernard A." wrote:
i'm looking for a way to have all possible length fixed n-uples from a
list, i think generators can help, but was not able to do it myself,
maybe some one could point me out to an idea to do it ?
did you try googling for "python permutations" ?

here's the first hit:

http://aspn.activestate.com/ASPN/Coo.../Recipe/190465


I've used that recipe a significant amount, and I feel like its
recursion really slows it down (but I haven't been profiled it). Does
anyone know of a non-recursive algorithm to do the same thing?


If you need the speed probstat.sf.net has permutations, combinations,
and cartesian products written in C. I'm the author and I use pure-python
versions when I know I'm just doing small lists (to avoid a dependency).
From your original problem it looks like you want a combination, not

a permutation. Combinations don't care about order (just unique sets).
import probstat
for (item) in probstat.Combination([0,1,2,3,4], 3):

.... print item
....
[0, 1, 2]
[0, 1, 3]
[0, 1, 4]
[0, 2, 3]
[0, 2, 4]
[0, 3, 4]
[1, 2, 3]
[1, 2, 4]
[1, 3, 4]
[2, 3, 4]

-jackdied
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
2
by: Alex Vinokur | last post by:
Does STL contain algorithms which generate (enable to generate) exponents, permutations, arrangements, and combinations for any numbers and words? -- Alex Vinokur mailto:alexvn@connect.to...
13
by: Kiran Dalvi | last post by:
Hi, Can anybody please suggest me an efficient approach to find out all possible permutations of a String. e.g. My input string = "ABC". My program should give an output like .... ABC, ACB, BAC,...
44
by: Christoph Zwerschke | last post by:
In Python, it is possible to multiply a string with a number: >>> "hello"*3 'hellohellohello' However, you can't multiply a string with another string: >>> 'hello'*'world' Traceback (most...
1
by: DrNick | last post by:
Hi All I have a problem I'm hoping someone can help me out with. I need to obtain different permutations of a list of objects. However, the list could potentially be enormous (~1000 objects) and I...
17
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
7
by: Christian Meesters | last post by:
Hi, I'd like to hack a function which returns all possible permutations as lists (or tuples) of two from a given list. So far, I came up with this solution, but it turned out to be too slow for...
5
by: Shraddha | last post by:
Suppose we are having 3 variables...a,b,c And we want to print the permutations of these variables...Such as...abc,acb,bca...all 6 of them... But we are not supposed to do it mannually... I...
82
by: Bill Cunningham | last post by:
I don't know if I'll need pointers for this or not. I wants numbers 10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that would be 10^16. So I have int num ; These are of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.