pylint woes

Steven D'Aprano steve at pearwood.info
Sun May 8 07:36:25 EDT 2016


On Sun, 8 May 2016 11:16 am, DFS wrote:

> address data is scraped from a website:
> 
> names = tree.xpath()
> addr  = tree.xpath()

Why are you scraping the data twice?

names = addr = tree.xpath()

or if you prefer the old-fashioned:

names = tree.xpath()
addr = names

but that raises the question, how can you describe the same set of data as
both "names" and "addr[esses]" and have them both be accurate?


> I want to store the data atomically, 

I'm not really sure what you mean by "atomically" here. I know what *I* mean
by "atomically", which is to describe an operation which either succeeds
entirely or fails. But I don't know what you mean by it.



> so I parse street, city, state, and 
> zip into their own lists.

None of which is atomic.

> "1250 Peachtree Rd, Atlanta, GA 30303
> 
> street = [s.split(',')[0] for s in addr]
> city   = [c.split(',')[1].strip() for c in addr]
> state  = [s[-8:][:2] for s in addr]
> zipcd  = [z[-5:] for z in addr]

At this point, instead of iterating over the same list four times, doing the
same thing over and over again, you should do things the old-fashioned way:

streets, cities, states, zipcodes = [], [], [], []
for word in addr:
    items = word.split(',')
    streets.append(items[0])
    cities.append(items[1].strip())
    states.append(word[-8:-2])
    zipcodes.append(word[-5:])
    

Oh, and use better names. "street" is a single street, not a list of
streets, note plural.



-- 
Steven




More information about the Python-list mailing list