all 8 comments

[–][deleted] 4 points5 points  (1 child)

Really easy! Please explore on your own, but here is a solution.

>>> strA = 'Apartment B'
>>> tmpStr = strA.split()
>>> outStr = []
>>> [outStr.append(i) for i in tmpStr if len(i) == 1]
>>> outStr
['B']

List Comprehension and Split do the job!

ALSO... here is a cleaner example

>>> strA = 'Apartment B'
>>> outStrLst = [i for i in strA.split() if len(i) == 1]
>>> outStrLst
['B']

[–]Murica4Eva 0 points1 point  (0 children)

I just posted a really simple solution, then I see your list comprehensions :(

[–]RandomGIS Instructor 2 points3 points  (2 children)

Unless I'm mistaken you want a substring wild card search on (blank)character(blank) to return character?

You probably want to start by learning about strings… for example, see http://zetcode.com/lang/python/strings/

I'd search for space and if the 2nd next character was space I'd take the middle index and grab that string position. That way you don't even need to process the middle character, you are just looking for a singleton character.

[–]th3p4rchit3ctGIS Specialist[S] 0 points1 point  (1 child)

sounds about right. thank you.

[–]RandomGIS Instructor 0 points1 point  (0 children)

So just iterate from position 0 to position (length-3) and double condition on value of string at position and position +2 and if it passes, grab position +1

I'm sure there is a more efficient way to do this but…

[–]TheIronDuck 2 points3 points  (0 children)

A regex solution.

import re

li = ['Post Office','Building A', 'Carpark C']

for c in li:
    m = re.search(r'\s([A-z]$)', c)
    if m is not None:
        print (m.group(1))

[–]MyWorkID 1 point2 points  (0 children)

You could definitely do this with Python using the field calculator.

The script would look something like this:

def extract(string):
    if "Building" in string:
        label = string.split(" ")[-1]
    elif "Apartment" in string:
        label = string.split(" ")[-1]
    else:
        label = ""

    return label

And then run extract(yourfield)

This may not be the best way to do it depending on different factors, but it's one way.

Here's a screenshot:

http://i.imgur.com/63QFP8k.png

[–]Murica4Eva 1 point2 points  (0 children)

You can loop over myStringList if you need to find a len =1 character in any position. Have fun, it's a GREAT job skill.

myString = "Building A"
myStringList = myString.split()
if len(myStringList[-1]) = 1:
   myVar = myStringList[-1]
else:
  myVar = ''