How to get other itertools combinations in Python?

How to get other itertools combinations in Python (such as (0,0,0))?
I could wrote all the 27 coordinates, but that is to much work.

*27 points should come out, now it is only 20

itertools 01.gh (2.7 KB)

You have only two of each number and letter. You need three entries of 0 to get (0, 0, 0)

You probably want to have just all_columns = ['0', 'z', '-z'] then use combinations with replacement.

1 Like

See also this thread for a pretty deep dive into itertools:

1 Like

Thank you both for your response Letwory and Deleuran. Letwory, what do you mean with ‚Äėcombinations with replacement?‚Äô

Thank you. I cannot find a way to make (0, 0, 0), (z, z, z), and the (-z,- z, -z).

https://docs.python.org/2/library/itertools.html#itertools.combinations_with_replacement

1 Like

Yes, I found that one too.
This one was also interesting.
https://www.blog.pythonlibrary.org/2016/04/20/python-201-an-intro-to-itertools/
However, I cannot understand how to have z, -z and 0 placed between ‚Äė‚Äô

When I tried the replacement version, it did not work

Seems to work fine?

Note though that order matters for combinations.

1 Like

Sorry, maybe I have a typo. My version of the solution does not work yet. Do I have to replace something (I try to make points)?

itertools 02.gh (2.7 KB)

line 3, coor = "0,z,-z"

Btw, if you want in your result (0,z,0) as well as (0,0,z) and (z,0,0) you can do just a cartesion product with (on line 5):

a = list(it.product(*coorsplit))
1 Like

I am very glad you helping me, thank you again.
Sorry, but I think I have a typo or some sort.
Replacing line 5 results in:

[('0', ' ', ' '), ('0', ' ', '-'), ('0', ' ', 'z'), ('0', 'z', ' '), ('0', 'z', '-'), ('0', 'z', 'z')]

I do not know what to do.
And yes, I want those other results you mentioned too.

Your code should look like (copy/paste over your own code)

import itertools as it

# our coordinate members as a string *without* spaces
# separated by the comma character
coor = "0,z,-z"
# split with the comma, this will give a list ["0", "z", "-z"]
coorsplit = coor.split(',')

# note that as argument to it.product() we give
# *coorsplit. This 'unpacks' the list, which is
# shorthand for it.product("0", "z", "-z")
a = list(it.product(*coorsplit))

It does not work. I do not know what I am doing wrong.
itertools 03.gh (3.0 KB)

Can you post a screenshot, I am on my phone :slight_smile:

Yah, sorry. My mistake. I forgot one line. The correct code is:

coor = "0,z,-z"
coorsplit = coor.split(',')
coorsplit = [coorsplit for i in range(0, len(coorsplit))]
a = list(it.product(*coorsplit))

I forgot line 3 in this snippet.

1 Like

Thank you Letwory