# Finding specific values in a array with different lengths

Page 1 of 1

## 9 Replies - 648 Views - Last Post: 07 August 2012 - 10:56 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288187&amp;s=4c229811e5d15edb92c2ce5c4b7f8e07&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 toyotajon93

Reputation: 0
• Posts: 21
• Joined: 04-August 12

# Finding specific values in a array with different lengths

Posted 07 August 2012 - 06:05 AM

Hey guys I hope you can give some help, I have read alot and cannot come up with a solution. Im no pro by any means.

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. 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

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

barracks = len(available_troops)

print available_troops
if available_troops[14][1] > 1:
soldier = available_troops[14][1]
soldiernum = soldier
print "You have ", soldiernum, " many soldiers"
if available_troops[19][1] > 1:
soldier1 = available_troops[19][1]
soldier1num = soldier1
print "You have", soldier1num, " many grenaders"

```

Is This A Good Question/Topic? 0

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

### #2 atraub

• Pythoneer

Reputation: 788
• Posts: 2,095
• Joined: 23-December 08

## 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?

Also, in almost all your examples, like these [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], the element at position 1 is a 0. did you intend to use the element at position 0 instead?

### #3 jon.kiparsky

• Pancakes!

Reputation: 8430
• Posts: 14,526
• Joined: 19-March 11

## Re: Finding specific values in a array with different lengths

Posted 07 August 2012 - 07:38 AM

Warning: I'm still learning python myself, so if my suggestions don't seem to work, it's likely that I'm wrong. But for what it's worth:

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

```

I don't see how available_troops can differ from troopsnumber in the circumstances you're describing. Perhaps you don't need to do the append loop.

```        barracks = len(available_troops)

```

you don't seem to use this for anything.

```        print available_troops
if available_troops[14][1] > 1:
soldier = available_troops[14][1]
soldiernum = soldier
print "You have ", soldiernum, " many soldiers"
if available_troops[19][1] > 1:
soldier1 = available_troops[19][1]
soldier1num = soldier1
print "You have", soldier1num, " many grenaders"

```

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:
```for type, troop in zip(key, available_troops):
if troop[1] > 1:
print "You have ", troop[1], " many ", type
```

Or something like that.

This post has been edited by jon.kiparsky: 07 August 2012 - 10:15 AM

### #4 toyotajon93

Reputation: 0
• Posts: 21
• Joined: 04-August 12

## Re: Finding specific values in a array with different lengths

Posted 07 August 2012 - 08:07 AM

The process fails when it looks at a base with less troops than the code looks for.

I want to use position two [1] as the return value since the first field declars the type of troop and position two indicates the number of troops.

I had an idea to declare troops_avaiable as troops_available[none]*20

But I'll have to try when I get home, no python compiler at work, lol

This should give me 20 blank rows correct? Then I can append each row with my data. If it does come back empty it will return a 0 which is what I would expect.

Thanks guys

### #5 atraub

• Pythoneer

Reputation: 788
• Posts: 2,095
• Joined: 23-December 08

## Re: Finding specific values in a array with different lengths

Posted 07 August 2012 - 09:17 AM

[[none]]*20 won't give you 20 blank lists, it'll give you 20 references to 1 single list (ie, if you change any of those 20 list, you'll be changing all of them).

EDIT:
If you want 20 blank LISTS inside of one list, you can do it like this
```masterList = [[None] for i in range(20)]
```

EDIT 2:
Fixed bracket typo

This post has been edited by atraub: 07 August 2012 - 09:43 AM

### #6 jon.kiparsky

• Pancakes!

Reputation: 8430
• Posts: 14,526
• Joined: 19-March 11

## Re: Finding specific values in a array with different lengths

Posted 07 August 2012 - 09:25 AM

```I had an idea to declare troops_avaiable as troops_available[none]*20

```

troops_available is a list, so troops_available[None] is going to fail - index must be an int.

atraub, on 07 August 2012 - 11:17 AM, said:

[none]*20 won't give you 20 blank arrays, it'll give you 20 references to 1 single array (ie, if you change any of those 20 arrays, you'll be changing all of them).

Maybe I'm misunderstanding you, but this is not what I got when I tried it in the interpreter.

>>> l = [None] * 3
>>> l
[None, None, None]
>>> l[1] = 1
>>> l
[None, 1, None]

>>> l = [[None]] * 3
>>> l
[[None], [None], [None]]
>>> l[1] = [1]
>>> l
[[None], [1], [None]]

EDIT: l = []*3 returns a single empty list, is that maybe what you were getting at?

This post has been edited by jon.kiparsky: 07 August 2012 - 09:26 AM

### #7 atraub

• Pythoneer

Reputation: 788
• Posts: 2,095
• Joined: 23-December 08

## Re: Finding specific values in a array with different lengths

Posted 07 August 2012 - 09:34 AM

you must be careful. Your last example used the assignment operator, which doesn't really change the underlying list, it reassigns it. This example should display my concern:

```>>> x=[[None]]*5
>>> print(x)
[[None], [None], [None], [None], [None]]
>>> x[0].append(5)
>>> print(x)
[[None, 5], [None, 5], [None, 5], [None, 5], [None, 5]]
>>>

```
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.

Now, if we use a list comprehension
```>>> x = [[None] for i in range(5)]
>>> print(x)
[[None], [None], [None], [None], [None]]
>>> x[0].append(5)
>>> print(x)
[[None, 5], [None], [None], [None], [None]]
>>>

```

EDIT:
I ran into this same problem when I was building a tic tac toe game... imagine how annoyed I was when the game would end after 1 move!

This post has been edited by atraub: 07 August 2012 - 09:46 AM
Reason for edit:: BAH NOW I'M SAYING ARRAY

### #8 jon.kiparsky

• Pancakes!

Reputation: 8430
• Posts: 14,526
• Joined: 19-March 11

## Re: Finding specific values in a array with different lengths

Posted 07 August 2012 - 09:54 AM

Ach, ganz klar. Danke.

### #9 baavgai

• Dreaming Coder

Reputation: 6071
• Posts: 13,150
• Joined: 16-October 07

## Re: Finding specific values in a array with different lengths

Posted 07 August 2012 - 10:14 AM

I'm not clear why you're copying here:
```available_troops = []
for troops in troopsnumber:
available_troops.append(troops)

```

Perhaps I'm messing something. Just do a check?
```def processTroops(troopsnumber):
available_troops = [ i for i in troopsnumber ]
barracks = len(available_troops)

print available_troops
if barracks>14 and available_troops[14][1] > 1:
print "You have ", available_troops[14][1], " many soldiers"

if barracks>19 and available_troops[19][1] > 1:
print "You have", available_troops[19][1], " many grenaders"

```

This post has been edited by baavgai: 07 August 2012 - 10:14 AM

### #10 toyotajon93

Reputation: 0
• Posts: 21
• Joined: 04-August 12

## Re: Finding specific values in a array with different lengths

Posted 07 August 2012 - 10:56 AM

I had it that way originally, but when it would get to a differnt base that say had max troop 14, and I request troop 19 it would crap out.

I guess I should have meant available_troops[0]*20

I would try leaving it blank but will it still give an error if the above happens?

I was thinking after I declare it available_troops[0]*20 I then append it with the results from.

I will try the above code
```masterList = [[None] for i in range(20)]
```

When I get home to see if it works. Thanks guys I will post results/more questions.