On 5/7/2016 9:36 PM, Chris Angelico wrote:
On Sun, May 8, 2016 at 11:16 AM, DFS <nos...@dfs.com> wrote:
On 5/7/2016 1:01 PM, Chris Angelico wrote:
The suggestion from a human would be to use zip(), or possibly to
change your data structures.


Happens like this:

address data is scraped from a website:

names = tree.xpath()
addr  = tree.xpath()

I want to store the data atomically, so I parse street, city, state, and zip
into their own lists.

"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]

So you're iterating over addr lots of times, and building separate
lists. As an alternative, you could iterate over it *once*, and have a
single object representing an address.


I like the idea of one iteration, but how? (I'll be trying myself before I check back in)

Remember, it's required to split the data up, to give flexibility in sorting, searching, output, etc.

I saw a cool example where someone built a list and used it to do a bulk INSERT. That probably won't work well here, because one of the options I give the user is # of addresses to store. So I do invididual INSERTs using the 'for j in range()' method, which makes it easier to track how many addresses have been stored.


Why is it better to zip() them up and use:

for item1, item2, item3 in zip(list1, list2, list3):
     do something with the items

than


for j in range(len(list1)):
   do something with list1[j], list2[j], list3[j], etc.

Because 'j' is insignificant here, as is the length of the list.

Sorry, but I don't understand what you mean by insignificant. j keeps track of the position in the list - regardless of the length of the list.


What
you're doing is iterating over three parallel lists - not counting
numbers. Imagine that, instead of lists, you just have *sequences* -
ordered collections of things. You can follow a recipe without knowing
the numbers of the individual lines; you just need to know the
sequence. Here, iterate over this collection:

* Collect ingredients.
* Cream the butter and the sugar.
* Sift the salt into the flour.
* Fold the mixture into an origami crane.

These instructions work whether they're numbered or not.

Again, not following you.


The only reason

for j in range(len(list1)):
    do something with list1[j], list2[j], list3[j], etc.

or

for item1, item2, item3 in zip(list1, list2, list3):
    do something with the items

works is because each list has the same number of items.


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to