0

I have been looking and reading for a few days about how to collect the last word from a varied length string. I have found lots of postings about how to collect/split the last word, but none of the content I have read addresses varied length stings.

I would like to use this function for column population, automated labeling and content filtering from inside either the field calculator or label expression interfaces.

String examples: Morgan County, Johnson Parrish, John Brown County, Rick de la Rosa City, Big Wild life Area.

Output example : County, Parish, City, Area

Nothing I have tried has worked 100%. The following code just about works, and would work if all my strings were two words long: s.split( " " )[1:][0]

I am using arcmap 10.2 / python

closed as off-topic by PolyGeo Feb 26 '15 at 22:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development are off-topic here unless they relate directly to Geographical Information Systems, but they can be asked on Stack Overflow." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • If there are spaces and commas standard in there, why not split by the comma and then further split each by the space? Then just take either the first value in the 2nd split (only word left) or the last one? – Branco Feb 26 '15 at 21:39
  • I think this is a pure Python question and so best researched/asked at Stack Overflow but take a look at the len function of Python which should tell you how many words are in the string and then use that to pull out the last of those words. – PolyGeo Feb 26 '15 at 21:46
3

You are missing a very little detail: the fact that in Python you can pull elements from a list starting at the back. The last element of a list corresponds to index -1.

For example:

location = "Cedar Wood Park"
locationChoppped = location.split(" ")
print locationChoppped[-1]

would return: Park, and:

location = "Highland Grove Hyper Megalopolis"
locationChoppped = location.split(" ")
print locationChoppped[-1]

would return: Megalopolis

Or, all in one line, and using s instead of location, like in your question:

s.split(" ")[-1]


PS: to actually use this in ArcGIS, you would have to right-click your new field in the attribute table and open the Field Calculator. Make sure to select Python and check the Pre-Logic Script Code box. Then enter:

def returnLastElement(s):
    return s.split(" ")[-1]

This would create a function. Then, in the narrow block below, you can call that function. Just repeat the name and call the field which you want to pass to your function and whose values should be truncated. Make sure to embrace the field name by exclamation points (!), or it will not work, such as this:

returnLastElement(!TheFieldWithLongNames!)

The safest is always to just place your cursor in between the parentheses and then double-click your field in the list above, which will then correctly insert it for you.

  • thanks so much... your answer works great. this is the most clear answer to this type of question I have read yet. – Robin Feb 26 '15 at 22:09
  • You are welcome! I just added how you would actually use it in the Field Calculator. Just for future reference :-) – BritishSteel Feb 26 '15 at 22:12
  • I am about to head into class.. I will share with fellow students. Your answer will make a lot of peoples lives so much easier. – Robin Feb 26 '15 at 22:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.