Lists, Arrays, and Tuples!

991
4
11-29-2011 11:16 AM
JamesHood
Occasional Contributor
Lists, Arrays, and Tuples!

I have a complicated script and a complicated problem. I have simplified it down to the following example:

I grab a field from a shapefile using updatecursor and input the field values into a list:

list = ["111-11-1","222-22-2","333-33-3"]


I need to split these lists up into integers so i can do some math on the first column of each string. (need to add 180 if < 180, and need to subtract 180 if > 180.)

for l in list:
    splitlist.append(l.split("-"))

print splitlist

for segment in splitlist:
    print segment


Result:
[['111', '11', '1'], ['222', '22', '2'], ['333', '33', '3']]
['111', '11', '1']
['222', '22', '2']
['333', '33', '3']



So I am able to isolate the values I need to perform the math on, but how do I call them to a variable and then insert them back in?

Here is a copy of my full code for this problem, with the third segment being my failed attempt at 2 d arrays.


list = ["111-11-1","222-22-2","333-33-3"]



splitlist=[]
for l in list:
    splitlist.append(l.split("-"))



print splitlist
records=0
for split in splitlist:
    records=records+1
    print split
    



columns=3
for j in range(records):
    for i in range(columns):
        current = splitlist[[records],[columns]]
        splitarray[[records],[columns]].append(current)
        print current
        print splitarray[[records],[columns]]



Error Message:
line 25, in <module>
    current = splitlist[[records],[columns]]
TypeError: list indices must be integers, not tuple



I found an exceptional resource here but it doesn't help with this problem: http://www.astro.ufl.edu/~warner/prog/python.html

Any advice?
Tags (2)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus
like this?
a_list = ["111-11-1","222-22-2","333-33-3"]
split_list = []
for a_val in a_list:
  vals = a_val.split("-")
  small_list = []
  for a_num in vals:
    small_list.append(int(a_num))
  if small_list[0] <= 180:
    small_list[0] += 180
  else:
    small_list[0] -= 180
  print small_list
0 Kudos
MichaelKyffin
New Contributor
You can get access to the "111" portion of each list item quickly with...

list = ["111-11-1", "222-22-2", "333-33-3"]
for item in list:
     print int(item.split('-')[0])


... which will produce...

111
222
333

So you can put each value of
int(item.split('-')[0])
into a variable, load into a numeric array, test for < or >, etc.

Or am I commenting on the part that isn't the problem?
0 Kudos
JamesHood
Occasional Contributor
Thanks Dan and Mike.
Mike, its been a while but a pleasure to see you on the internets!


I should first clarify where my problem was.
I wanted to pull values from the first position in the first list then the first position in the second list, then the 1st in the 3rd... etc.

list = ["  111-11-1", "  222-22-2", "  333-33-3"]


The only caveat is that after pulling the value I wanted to be able to insert it back into the the same format but with the altered value as required.

I was getting tripped up trying to accomplish this using 2D arrays but the data I have is not actually a 2d array. Calling item[2][0] for instance should have provided me with "222" if this was a 2d array and this is where the error was happening I think.

Its simply a list of lists...


So
list = ["111-11-1", "222-22-2", "333-33-3"]
splitlist=[]
for l in list:
    splitlist.append(l.split("-"))
print splitlist



Prints: [[  '111', '11', '1'], ['  222', '22', '2'], [  '333', '33', '3']]



These have been converted from a list of 3 strings to a list of 3 lists.
Next step is to call first position from each of the lists and do the math.

for item in splitlist:
    value = int(item[0])
    if value>180:
        value=value-180
    elif value<180:
        value=value+180
    item[0]=str(current)

print splitlist


Prints: [[  '291', '11', '1'], [  '42', '22', '2'], [  '153', '33', '3']]


I can leave my data in this format because the script I am working on uses it in this format so...
Success!
0 Kudos
MichaelKyffin
New Contributor
Glad to hear you got it working.
0 Kudos