1

I have a list of data that I'm trying to find the max value from with python. My current code will loop over the data and render all possible combinations of the data, however I can't figure out how to render the max from the results.

Below is my current setup:

street = %sql SELECT * FROM streets

 for i in range(len(flight)):
    for j in range(len(flight)):
        for k in range(len(flight)):
            A = flight[i][2]
            B = flight[k][2]
            num = flight[i][4] , flight[j][4] , flight[k][4]
            numsum = sum(num)
            print A, B, numsum

Printing flight will render the below

+----+-----------+----------------------+----------------------+---+
| id |   flight  |        Start         |          End         |dis|
+----+-----------+----------------------+----------------------+---+
| 0  |     w     |       SFO            |          DEN         | 4 |
| 1  |     e     |       DEN            |          NYC         | 7 |
| 1  |     e     |       DEN            |          ORD         | 7 |

However the max with throw the below error.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-283-770cd29ebd83> in <module>()
      8             num = street[i][4] , street[j][4] , street[k][4]
      9             numsum = sum(num)
---> 10             print A, B, max(numsum)
     11 

TypeError: 'int' object is not iterable

If I remove the max from the last line everything in the database will print. For example:

SFO ORD 35
DEN JFK 12
SFO JFK 94
LAX DEN 54
...

Can someone help me figure out how to get the max value in numsum so the result prints like this:

 SFO JFK 94

Thanks in advance!

6
  • 1
    Your error does not match up with the code you listed. Please show the exact code you are running. Commented Oct 6, 2015 at 2:07
  • What does numsum look like? Can you post a piece of code that defines an example? Commented Oct 6, 2015 at 2:08
  • 1
    could you provide an example input and the expected output! Commented Oct 6, 2015 at 2:09
  • Thanks guys, I'm still a python noob so any help is greatly appreciated :) I just updated the question to hopefully make it a bit more clear. Please let me know if there is any other information that could help. Commented Oct 6, 2015 at 14:05
  • I checked your updated code, which does INDEED help quite a bit. The thing is that what you're trying to do doesn't really match what your code is accomplishing. I'm assuming you're trying to find the longest distance (column 5) you can travel in three flights, but you don't actually make sure those connecting flights land and depart from the same destinations. For instance your final solution could be PDX -> SFO, HOU -> DEN, JFK -> NYC. They're just nonsensical data points. Commented Oct 6, 2015 at 16:07

1 Answer 1

1

You're not doing what you're trying to do. Your algorithm isn't well thought-out. look at it this way:

for each_item in whatever_iterator:
    a = each_item[some_element]
    b = each_item[another_element]
    num = some, other, numbers
    sumnum = sum(num)  # hey that was easy!
    print a, b, sumnum  # every time through, let's print it!

Nowhere does this find the LARGEST. In order to do that, you'd want to iterate through and save current_max = max(current_max, new_value)

Looks like what you're looking to do is:

max_sumnum = (0, 0, 0)
for i, j, k in itertools.product(len(x), repeat=3):
    num = x[i][4], x[j][4], x[???][1][k][4]
    cur_sumnum = x[i][2], x[k][2], sum(num)
    max_sumnum = max(max_numsum, cur_sumnum, key=lambda tup: tup[2])
print max_sumnum

I use itertools.product here because it's a great shortcut for nested for loops.

for i in range(3):
    for j in range(5):
        for k in range(100):
            for m in range(2):
                foo(i, j, k, m)

# equivalent to....
for i, j, k, m in itertools.product(range(3), range(5),
                                    range(100), range(2)):

And I use the repeat keyword since you're doing a bunch of the same loops.

for i in range(3):
    for j in range(3):
        for k in range(3):

# equivalent to....
for i, j, k in itertools.product(range(3), repeat=3):
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.