-1

I have looked thoroughly, but have not found a clear (or roundabout) answer to the following question: How can I create a for loop that will allow me to find the greatest pair where x and y in (x,y) are greater than or equal to all other x's and y's in the list?

So, suppose I have a list of 20 pseudorandom integer ordered pairs and I want to find which ordered pair is the greatest using a for loop.

The code I currently have is:

np.random.seed(1234)
a = np.random.randint(0, 11,(20,2))
alst = list(allocationsset)
print alst

Where np is the imported form of numpy and which prints, [[1,0],[8,9],[2,1],[3,2],[10,10] etc...]

Then, I attempt to make the for loop with the following function:

def dominantx (pairs):
    def maxx (pairs):
        maxlist = []
        for [a,b] in pairs:
            if a >= b:
                maxlist.append([a,b])
        return maxlist

    dom = maxx(pairs)
    domlist = []

    for [a,b] in dom:
        for [c,d] in dom:
            if a > c:
                domlist.append([a,b])
            if a == c:
                if b > d:
                    domlist.append([a,b])
                if b == d:
                    domlist.append([a,b])
                    domlist.append([c,d])
                else:
                    domlist.append([c,d])
            else:
                domlist.append([c,d])
    return domlist

dominantx(alst)

I believe part of the problem I am having is replacing what is in "domlist" with the new greatest ordered pair and returning that as the answer, i.e. I don't want to append "domlist" with the new greatest ordered pair until I remove the old greatest ordered pair from domlist.

My first loop works fine, it is just the second for loop in defining dominantx where I am having trouble. I am relatively new to Python, so please forgive any simply fixes or errors. I am open to any suggestions that still implement a loop and, of course, any help is greatly appreciated. Thank you!

Martin
  • 51
  • 1
  • 3
  • 7
  • 3
    What exactly do you mean with "the greatest pair where x and y in (x,y) are greater than or equal to all other x's and y's in the list"? Wich is the greatest: (2, 5) or (4, 0)? – leogama Oct 19 '14 at 00:43
  • (4,0). I suppose I didn't specify enough. The ordered pair is a relation between x and y, so I want the largest x to start. Then, I am comparing all y's to each other, such that x and y are both maximized. So y is being maximized subject to x having already been maximized. – Martin Oct 19 '14 at 01:03

1 Answers1

2

Just use max:

arr = [[1,0],[8,9],[2,1],[3,2],[10,10]]
print(max(arr))

[10, 10]

If you want actual pairs as in [1,1],[2,2] etc..

arr = [[1,1],[8,9],[2,2],[3,2],[13,10]]
pairs = [x for x in arr if x[0] == x[1]] # get all subelements with equal elements
print(max(pairs))
[2, 2]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Thanks for the suggestion! I didn't use it quite the way you suggested, but I implemented it into the dominantx function and it seemed to work for finding the maximum pair. Though I will take suggestions from others in terms of replacing the elements in "domlist" and appending new variables. – Martin Oct 19 '14 at 01:00
  • No worries, I was not fully clear on what you considered pairs but max and filtering with a list comp will work – Padraic Cunningham Oct 19 '14 at 01:01