The first number can go from either 1 to 1 or 1 to 18. There can also be between 1 and 18 lines in the array (this is causing the issue) 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. 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... This code breaks at the moment where the return array is different. Say my first city has 20 available troops thus returning [20][1] works, but for city 2 it has 12 types of available troops so using [20][1] in a if statement break it. I have tried multiple different formats of the below if, but still craps out if I use an array value that doesnt exist. Thanks guys

Replies To: Finding specific values in a array with different lengths

Re: Finding specific values in a array with different lengths

Posted 07 August 2012 - 07:29 AM

your first condition assumes that there are at least 15 lists (not arrays) inside of available_troops, the second condition assumes that there are at least 20 lists inside of available_troops. What happens if there are less?

What you'd really like to do here would be to loop through the available_troops list, so you wouldn't have to do write this for each troop type.
There are a few ways you could go about this. One way would be to put a type identifier in the information you get from getTroopsAvailable: insert "soldiers", "grenadiers", etc., directly in that data. Another way would be to put an identifying key that you'd use for a lookup, and pass around a table. Since python lists aren't locked into types, this seems a bit of a rigamarole, but maybe it's useful. Another way would be to ensure that your list is always of a fixed length and use the position in the list to tell you what type of troop you're dealing with. I don't like this one much, since it seems flexibility is part of your design.

Quote

This code breaks at the moment where the return array is different.

The problem is that you're trying to address elements of a list that aren't there. As I say above you could ensure that the list always has 20 members, by inserting null members, or you could let each member of the list know what troop type it is.

Another option, I suppose, would be for each city to provide a "key" - a list of troop types, as a separate list.
Then you could do something like:

As you can see, we don't really have a list containing 5 lists, we have a list containing 5 references to the same list! Because of these sorts of gotchas, I never recommend declaring lists in this fashion.