Re: Enumerating ordered expat attributes with tuplets?

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

    Re: Enumerating ordered expat attributes with tuplets?

    On Sep 11, 4:48 pm, Manuel Ebert <maeb...@uos.de wrote:
    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1
    >
    Ah, well. Don't know whether it meets your aesthetic standards, but:
     >>my_list = ['tree', 'hug', 'flower', 'hug', 'bear', 'run']
     >>my_list[0:len(a):2]
    ['tree', 'flower', 'bear']
     >>my_list[1:len(a):2]
    ['hug', 'hug', 'run']
    >
    and hence
    >
     >>zip(my_list[0:len(a):2], my_list[1:len(a):2])
    [('tree', 'hug'), ('flower', 'hug'), ('bear', 'run')]
    >
    and furthermore
    >
     >>for a, b in zip(my_list[0:len(a):2], my_list[1:len(a):2]):
    ...     print a, b
    ...
    tree hug
    flower hug
    bear run
    >
    or the slightly less obfuscated:
    >
     >>for index in range(0, len(my_list), 2):
    ...     print my_list[index], my_list[index + 1]
    ...    
    tree hug
    flower hug
    bear run
    >
    [snip]
    I don't know what the "len(a)" is, but the end position defaults to
    the end:
    >>zip(my_list[0::2], my_list[1::2])
    [('tree', 'hug'), ('flower', 'hug'), ('bear', 'run')]
Working...