I want to extract the highest number from a permutation. I am using the groups module right now so the output in the below code should be 15
from groups import *
a = Perm((1, 2, 3), (4, 15, 6), (7, 8, 9))
max([x for x in a])
I want to extract the highest number from a permutation. I am using the groups module right now so the output in the below code should be 15
from groups import *
a = Perm((1, 2, 3), (4, 15, 6), (7, 8, 9))
max([x for x in a])
First, you are not creating your permutation correctly. The correct syntax, best seen on page 23 in your link, is
Next, that module is set up so that theoretically it permutes all non-negative integers, with finitely many of them mapping to values other than themselves. So theoretically there is no highest number in a permutation in that module. As your link states on page 5,
So in one respect your quest to "extract the highest number from a permutation" is meaningless. However, at any given time, the data structure representing a permutation in that module does have a largest number. The module tries to hide that information from the user, to keep the theoretical viewpoint of acting on all non-negative integers. But since the
Perm
class is derived from thedict
built-in type, you can find the current highest number in that structure withIn your example, that does return the value
15
. But be aware that the largest value could easily change, without changing the permutation that is being represented. For example, if you executeprint(a[20])
, that does not seem to change the permutationa
, and comparing the value ofa
to its previous value using==
yieldsTrue
. But nowmax(a)
yields the value20
. Thusmax(a)
is not consistent and depends on the current internal representation of the permutation, so it is not wise to use this value.Fortunately, you can find a more consistent "highest number", namely the highest number that is changed by the permutation:
This also returns the result you want,
15
. Accessinga[20]
or any other value does not changea.max()
, so you should satisfy yourself with themax()
value.By the way, regarding your linked document, here is a better link to the documentation, which is a finished version of the pre-print you linked to. And here is a link to the source code. However, I referred to your link in what I wrote above. The Python code in that document uses Python 2.6: I made some changes so it runs in Python 3.7 and used that to check my answer.