Re: Quick nested loop syntax?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alex23

    Re: Quick nested loop syntax?

    On Nov 20, 3:48 am, Johannes Bauer <dfnsonfsdu...@ gmx.dewrote:
    a = { "a", "b", "c" }
    b = { 4, 9, 13}
    for (x, y) in someoperator(a, b):
            print(x, y)
    >
    which would print all tuples of
    "a", 4
    "a", 9
    "a", 13
    "b", 4
    "b", 9
    "b", 13
    "c", 4
    "c", 9
    "c", 13
    If you're using a version of Python before 2.6, you could also use a
    list comprehension:
    >>show = lambda *args: sys.stdout.writ e('%s\n' % args)
    >>[show((x,y)) for x in a for y in b]
    ('a', 4)
    ('a', 9)
    ('a', 13)
    ('b', 4)
    ('b', 9)
    ('b', 13)
    ('c', 4)
    ('c', 9)
    ('c', 13)
Working...