[Baypiggies] Pythonic way to iterate over two lists?

Jeremy Fishman jeremy.r.fishman at gmail.com
Tue Jun 7 07:27:14 CEST 2011


More information:

izip() is the iterative version of the core Python builtin zip(), which
returns a list.
    http://docs.python.org/library/functions.html#zip

In Python 3+ zip() returns an iterable object (it's a type)
    http://docs.python.org/release/3.2/library/functions.html#zip

<http://docs.python.org/release/3.2/library/functions.html#zip>Cheers,
Jeremy

On Mon, Jun 6, 2011 at 9:44 PM, Hy Carrinski <hcarrinski at gmail.com> wrote:

> The following will work.
> Does it fully solve your problem?
>
> from itertools import izip
>
> for (job, machine) in izip(jobs, machines):
>    do_job(job, machine)
>
> On Mon, Jun 6, 2011 at 9:37 PM, Ross Patterson <me at rpatterson.net> wrote:
> > I suspect you could use itertools.izip_longest:
> > http://docs.python.org/library/itertools.html#itertools.izip_longest
> > Ross
> > On Mon, Jun 6, 2011 at 9:32 PM, Casey Callendrello <c1 at caseyc.net>
> wrote:
> >>
> >> Hi there,
> >> I've got a simple problem that I've already solved effectively, but I
> >> can't help thinking that there must be a more "pythonic" way to do it.
> >> Especially because my solution uses a list index, which I *know* can't
> >> possibly be the Python way ;-).
> >>
> >> In any case, I have two lists: one of machines, and one of jobs. Either
> >> one can be of arbitrary length, including zero. I want to generate
> (machine,
> >> job) pairs where every machine gets at most one job, each job is only
> >> executed once, and as much work as possible is done. The actual index or
> >> order is irrelevant.
> >>
> >> The simple, C-inspired solution is:
> >>
> >> i = 0
> >> while i<len(jobs) and i<len(machines):
> >>    do_job(jobs[i], machines[i])
> >>    i += 1
> >>
> >> There has to be a cleaner way than that! Any suggestions?
> >>
> >> --Casey
> >>
> >>
> >>
> >> _______________________________________________
> >> Baypiggies mailing list
> >> Baypiggies at python.org
> >> To change your subscription options or unsubscribe:
> >> http://mail.python.org/mailman/listinfo/baypiggies
> >
> >
> > _______________________________________________
> > Baypiggies mailing list
> > Baypiggies at python.org
> > To change your subscription options or unsubscribe:
> > http://mail.python.org/mailman/listinfo/baypiggies
> >
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20110606/4a60566c/attachment.html>


More information about the Baypiggies mailing list