简体   繁体   中英

How to find largest number in a permutation in python?

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])

Use the following to define the maximum of a permutation:

p = permutations([1, 2, 3])
l = list(p)
max(l[0])

and then iterate over the given permutations.

I'm not getting exactly what you want this these permutation, if only want to find the maximum.But you try this-

    from itertools import permutations 
    perm = permutations([1, 2, 3, 4, 15, 6, 7, 8, 9]) 
    for i in list(perm): 
    print (max(i)) 

This piece of code will always 15 as output.

First, you are not creating your permutation correctly. The correct syntax, best seen on page 23 in your link , is

a = Perm()(1, 2, 3)(4, 15, 6)(7, 8, 9)

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,

The perm size n is undefined because keys not defined explicitly are equal to their values ( p[i] == i ).

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 the dict built-in type, you can find the current highest number in that structure with

highestnum = max(a)

In 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 execute print(a[20]) , that does not seem to change the permutation a , and comparing the value of a to its previous value using == yields True . But now max(a) yields the value 20 . Thus max(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:

highestnum = a.max()

This also returns the result you want, 15 . Accessing a[20] or any other value does not change a.max() , so you should satisfy yourself with the max() 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM