8

Given a Python string like this:

location_in = 'London, Greater London, England, United Kingdom'

I would like to convert it into a list like this:

location_out = ['London, Greater London, England, United Kingdom',
                'Greater London, England, United Kingdom',
                'England, United Kingdom',
                'United Kingdom']

In other words, given a comma separated string (location_in), I would like to copy it to a list (location_out) and gradually break it down by removing the first word/phrase each time.

I'm a Python newbie. Any ideas on a good way to write this? Thanks.

8
  • Uh.. Isn't locaton_out just [location_in]? You need to clarify further. Commented May 13, 2011 at 23:06
  • 4
    don't. I don't know what you are doing but this probably the wrong approach. Commented May 13, 2011 at 23:15
  • Some great answers below. Thanks everyone for your help! Commented May 14, 2011 at 8:29
  • 1
    @WinstonEwert If you don't know what he is doing, how do you know it is the wrong approach? Commented Jan 8, 2013 at 17:37
  • @BenMordecai, the requested output is strange, leading me to think there is probably a better approach to whatever he was trying to do. Commented Jan 8, 2013 at 18:41

4 Answers 4

25
location_in  = 'London, Greater London, England, United Kingdom'
locations    = location_in.split(', ')
location_out = [', '.join(locations[n:]) for n in range(len(locations))]
Sign up to request clarification or add additional context in comments.

Comments

1

Here's a working one:

location_in = 'London, Greater London, England, United Kingdom'
loci = location_is.spilt(', ') # ['London', 'Greater London',..]
location_out = []
while loci:
  location_out.append(", ".join(loci))
  loci = loci[1:] # cut off the first element
# done
print location_out

Comments

1

Plenty of ways to do this, but here's one:

def splot(data):
  while True:
    yield data
    pre,sep,data=data.partition(', ')
    if not sep:  # no more parts
      return

location_in = 'London, Greater London, England, United Kingdom'
location_out = list(splot(location_in))

A more perverse solution:

def stringsplot(data):
  start=-2               # because the separator is 2 characters
  while start!=-1:       # while find did find
    start+=2             # skip the separator
    yield data[start:]
    start=data.find(', ',start)

Comments

0
>>> location_in = 'London, Greater London, England, United Kingdom'
>>> location_out = []
>>> loc_l = location_in.split(", ")
>>> while loc_l:
...     location_out.append(", ".join(loc_l))
...     del loc_l[0]
... 
>>> location_out
['London, Greater London, England, United Kingdom', 
 'Greater London, England, United Kingdom', 
 'England, United Kingdom', 
 'United Kingdom']
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.