Hey guys I hope you can give some help, I have read alot and cannot come up with a solution.

Here is what I'm trying to do, I return a set of values from a web request

[[1, 0, 1], [2, 0, 1], [3, 0, 1], [4, 0, 1], [5, 0, 1], [6, 0, 1], [7, 0, 1], [8, 0, 1], [9, 0, 1], [10, 0, 1], [11, 0, 1], [12, 0, 1], [13, 0, 1], [14, 0, 1], [15, 4431, 1], [16, 0, 1], [17, 0, 1]]

The first number can go from either 1 to 1 or 1 to 18. I need a statement that the number of returns doesnt matter. So that I could get 1 value such a [1,0,1] or to values [1, 0, 1], [2, 0, 1], [3, 0, 1], [4, 0, 1], [5, 0, 1], [6, 0, 1], [7, 0, 1], [8, 0, 1], [9, 0, 1], [10, 0, 1].

I then want to take the second number and run if statements off of it. Here is my code so far, I just cannot seem to get it to run right. Any adv, I have changed around the length and other information? Im sure its an easy fix but my hair wants to fall out. Any help is greatly appreciated. I put the print statement in there just to make sure I'm not crazy when i was coding. It works without the if, so I'm lost... Thanks guys!

    #Get troop counts
    troopsnumber = getTroopsAvailable(city)
    available_troops = []
    for troops in troopsnumber:
        available_troops.append(troops)

    print available_troops

    if len(available_troops) == 14:  
        overlord = available_troops[14][1]
        print "You have ", overlord, " many Overlord"

If len of list is 14, last index is 13, so line 10 does not make sense. It is more clear to index the last element of list with -1.

Edited 4 Years Ago by pyTony

In this case the total length was 15. Each return can have a different total number of lines. That is what I am trying to overcome. I did get this to return a result:

if available_troops[16][1] > 1:
            khak = available_troops[16][1]
            print "You have ", khak, " many Overlord"

I got this to work wih the logic above but when a new city is loaded depending on the return values, in this instance, [16][1] does not exist so it errors out. I cannot figure out how to get it to use a range correctly.

Why you are not using

if available_troops[-1][1] > 1:

as I said in previous post?

Edited 4 Years Ago by pyTony

You can get a list of indexes for which there is more than 1 item

>>> available_troops = [[1, 0, 1], [2, 0, 1], [3, 0, 1], [4, 0, 1], [5, 0, 1], [6, 0, 1], [7, 0, 1], [8, 0, 1], [9, 0, 1], [10, 0, 1], [11, 0, 1], [12, 0, 1], [13, 0, 1], [14, 0, 1], [15, 4431, 1], [16, 0, 1], [17, 0, 1]]
>>> 
>>> indexes = [i for (i, triple) in enumerate(available_troops) if triple[1] > 1]
>>> indexes
[14]

so you know that available_troops[14][1] > 1.

>>> print available_troops[indexes[0]][1]
4431

Edited 4 Years Ago by Gribouillis

Comments
Beautiful Pythonic code
This article has been dead for over six months. Start a new discussion instead.