Select to view content in your preferred language

ArcPy / Python:  Join two lists (and more , big problem!)

1570
2
04-13-2011 03:25 PM
JamesHood
Regular Contributor
Hello,

First my of my experience with python is in 9.3x and I only just started on arc10 and arcpy last week! This means I am Newb all over again.


I have a large dataset that contains latitude and longitude fields. These fields are stored as such:

111-22-22, 23-4-55, 33-3-3 etc,

For labeling purposes we would like to convert and store these in their standard display format as such: 103°22'77"


I have encountered a number of issues and am now doubting if this is possible. I would like to solve how to join two lists, however.


Essentially I have done the following:

Update_cursor = arcpy.UpdateCursor(feature_class)
For rows in update cursor:
    coord = rows.latitude
    courd_list = coord.split("-")  #This should print something like ["000", "00", "00"]
    Symbol_list = ["°", "'", "''"]




So what I would like to do is join the coord_list with the symbol_list but not just join, splice....

In theory it would give me the result I need.

I went about it many different ways and when each one failed, I seem to have erased them and moved on to the next which is a shame because I would have liked to have shared with you my process.

One method that I think might be the answer is the following which I gleaned from the old forums but I can't remember who suggested it and I have deleted the link already :



symbol = ["°", "'", "''"] 

values = ["100-22-111", "12-232-22", "33-3-33", "993-34-7"]

testSet1 = set()
for v in values:
   testSet1.add(v)
testSet2 = set()
for s in symbol:
   testSet2.add(s)
print set.union(testSet1, testSet2)

PRINTS  set(["''", '12-232-22', "'", '\xb0', '100-22-111', '33-3-33', '993-34-7'])


My Problems:

This would seem to work EXCEPT even though I asked python to union set1 then set2, it seems to union in reverse.
Specifically, the symbols set, set2, is unioned in reverse, putting the ° at the end and the '' at the beginning...
I could fix both of these I guess by formatting my code but what if it is not consistant?


The character ° does not seem to be supported by python, can anybody confirm this? In my test result , the ° is converted to \xb0. Is there an ascii or some other module to import so that python will support it?

Finally, if I am able to ge the lists to properly union, I will need to output them to a single starting from position 1 in the list and moving to the last , before I can re assign this to the new field in the table.

For some reason I can't figure out how to output multiple instances in a List to create 1 back to back string.




I appriciate any ideas or comments!
Thanks

James
Tags (2)
0 Kudos
2 Replies
JasonScheirer
Esri Alum
Python does support the degree symbol, that \x00 sequence you're seeing is the escaped version of the string so that it will work in ASCII. You're safer using a unicode object with an escaped character.

Sets will not work here, Python sets are unordered meaning you can only assume that all the elements will be there, but when asking for them you will get them back in an arbitrary manner.

You can use zip to unify two lists in the way you're looking for. I would rewrite the script like this:

symbols = (u'\u00b0', u"'", u"''")
Update_cursor = arcpy.UpdateCursor(feature_class)
for rows in update cursor:
    coord = row.latitude
    merged_items = zip(coord.split(u"-"), symbols)
    merged_value = "".join(x + y for (x, y) in merged_items)
    row.new_field = merged_value
    Update_cursor.updateRow(row)


The 'u' in front of the quotes signifies it's a unicode object -- this gives you more predictable behavior when working with extended characters. Then zip gives you a list of tuples of the form [(num1, sym1), (num2, sym2), (num3, sym3)]. Then you add each of the two items together into a single string, leaving you with a list of string, and then join each of those together into a single string.
0 Kudos
JamesHood
Regular Contributor
jscheirer,

You are a hero of our times.  I only wish esri still had a star system so you could be rewarded appropriately. 

I had attempted to use the zip method previously, though for some reason I determined that it didn't work.  I suppose I went about it wrong.

Thanks again for the help!





You can use zip to unify two lists in the way you're looking for. I would rewrite the script like this:

symbols = (u'\u00b0', u"'", u"''")
Update_cursor = arcpy.UpdateCursor(feature_class)
for rows in update cursor:
    coord = row.latitude
    merged_items = zip(coord.split(u"-"), symbols)
    merged_value = "".join(x + y for (x, y) in merged_items)
    row.new_field = merged_value
    Update_cursor.updateRow(row)


0 Kudos