tuples within tuples

Discussion in 'Python' started by korovev76@gmail.com, Oct 26, 2007.

  1. Guest

    Hello everybody.

    I'm wondering how to iterate over a tuple like this
    [A,B,C,D]
    while saving A and C in a list.

    My problem is that C sometimes is a tuple of the same structure
    itself...


    thanks!
    korovev
    , Oct 26, 2007
    #1
    1. Advertising

  2. Larry Bates Guest

    wrote:
    > Hello everybody.
    >
    > I'm wondering how to iterate over a tuple like this
    > [A,B,C,D]
    > while saving A and C in a list.
    >
    > My problem is that C sometimes is a tuple of the same structure
    > itself...
    >
    >
    > thanks!
    > korovev
    >

    First of all [A,B,C,D] is a list not a tuple. (A,B,C,D) is a tuple.

    Without a better example or explanation of what you are trying to do it is
    difficult, but I'll give it a try:

    myTuple=(A,B,C,D)
    for n, item enumerate(myTuple):
    if n in (0,2):
    myList.append(item)

    -Larry
    Larry Bates, Oct 26, 2007
    #2
    1. Advertising

  3. Guest

    [cut]
    >
    > Without a better example or explanation of what you are trying to do it is
    > difficult


    You're right.
    Actually i'm parsing an xml file using pyrxp, which returns something
    like this:
    (tagName, attributes, list_of_children, spare)
    Where list_of_children might "be a list with elements that are 4-
    tuples or plain strings".

    In other terms, if I have something like this:
    ('<tagA><tagB>bobloblaw</tagB></tagA>')
    it's parsed like this:
    ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)...

    Fact is that my xml is much more deep... and I'm not sure how to
    resolve it


    thanx
    , Oct 26, 2007
    #3
  4. On Fri, 26 Oct 2007 05:54:24 -0700, korovev76 wrote:

    > [cut]
    >>
    >> Without a better example or explanation of what you are trying to do it is
    >> difficult

    >
    > You're right.
    > Actually i'm parsing an xml file using pyrxp, which returns something
    > like this:
    > (tagName, attributes, list_of_children, spare)
    > Where list_of_children might "be a list with elements that are 4-
    > tuples or plain strings".
    >
    > In other terms, if I have something like this:
    > ('<tagA><tagB>bobloblaw</tagB></tagA>')
    > it's parsed like this:
    > ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)...
    >
    > Fact is that my xml is much more deep... and I'm not sure how to
    > resolve it


    Resolve *what*? The problem isn't clear yet; at least to me. Above you
    say what you get. What exactly do you want? Examples please.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Oct 26, 2007
    #4
  5. Guest


    > Resolve *what*? The problem isn't clear yet; at least to me. Above you
    > say what you get. What exactly do you want? Examples please.
    >



    Sorry for my poor english, but I meant: how can I obtain a list of A
    and C starting from something like this?

    (A,B,C,D)
    that could be
    ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)
    but also
    ('tagA', None, description, None)
    when I don't know if C is a tuple or not?

    I guess that, at least, within the cicle I may test if C is a tuple
    or not.. And then apply the same cicle for C... and so on

    Am i right?


    ciao
    korovev






    ciao
    korovev
    , Oct 26, 2007
    #5
  6. Duncan Booth Guest

    wrote:

    >
    > [cut]
    >>
    >> Without a better example or explanation of what you are trying to do
    >> it is difficult

    >
    > You're right.
    > Actually i'm parsing an xml file using pyrxp, which returns something
    > like this:
    > (tagName, attributes, list_of_children, spare)
    > Where list_of_children might "be a list with elements that are 4-
    > tuples or plain strings".
    >
    > In other terms, if I have something like this:
    > ('<tagA><tagB>bobloblaw</tagB></tagA>')
    > it's parsed like this:
    > ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)...
    >
    > Fact is that my xml is much more deep... and I'm not sure how to
    > resolve it
    >

    Probably you want some sort of visitor pattern.

    e.g. (warning untested pseudo code ahead)

    def walkTree(tree, visitor):
    tag, attrs, children, spare = tree
    fn = getattr(visitor, 'visit_'+tag, None)
    if not fn: fn = visitor.visitDefault
    fn(tag, attrs, children, spare)

    for child in children:
    if isinstance(child, tuple):
    walktree(child, visitor)
    else:
    visitor.visitContent(child)

    class Visitor:
    def visitDefault(self, t, a, c, s): pass
    def visitContent(self, c): pass

    .... then when you want to use it you subclass Visitor adding appropriate
    visit_tagA, visit_tabB methods for the tags which interest you. You walk
    the tree, and store whatever you want to save in your visitor subclass
    instance.
    Duncan Booth, Oct 26, 2007
    #6
  7. On Fri, Oct 26, 2007 at 06:59:51AM -0700, wrote regarding Re: tuples within tuples:
    >
    > > Resolve *what*? The problem isn't clear yet; at least to me. Above you
    > > say what you get. What exactly do you want? Examples please.
    > >

    >
    >
    > Sorry for my poor english, but I meant: how can I obtain a list of A
    > and C starting from something like this?
    >
    > (A,B,C,D)
    > that could be
    > ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)
    > but also
    > ('tagA', None, description, None)
    > when I don't know if C is a tuple or not?
    >
    > I guess that, at least, within the cicle I may test if C is a tuple
    > or not.. And then apply the same cicle for C... and so on
    >
    > Am i right?
    >
    >
    > ciao
    > korovev


    So, to clarify the piece that you still haven't explicitly said, you want to keep the tag name and children of every element in your XML document. (which is different from keeping (A, C) from tuple (A, B, C, D), because you want to modify C as well, and it's decendants.)

    As a first solution, how about:

    def reduceXML(node):
    if type(node) == str:
    return node
    else:
    return (node[0], reduceXML(node[2])

    N.B. Beware of stack limitations.
    J. Clifford Dyer, Oct 26, 2007
    #7
  8. On Fri, 26 Oct 2007 06:59:51 -0700, declaimed the
    following in comp.lang.python:

    >
    > (A,B,C,D)
    > that could be
    > ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)
    > but also
    > ('tagA', None, description, None)
    > when I don't know if C is a tuple or not?
    >
    > I guess that, at least, within the cicle I may test if C is a tuple
    > or not.. And then apply the same cicle for C... and so on
    >

    "C" isn't a tuple in your example either. It is a one-element list
    (the single element INSIDE the list is a tuple whose third element is a
    list containing a non-terminated string -- so the entire structure is
    invalid)
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Oct 26, 2007
    #8
  9. Guest

    On 26 Ott, 19:23, Dennis Lee Bieber <> wrote:
    > > (A,B,C,D)
    > > that could be
    > > ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)

    >
    > "C" isn't a tuple in your example either. It is a one-element list
    > (the single element INSIDE the list is a tuple whose third element is a
    > list containing a non-terminated string -- so the entire structure is
    > invalid)
    >


    i'm not sure what u mean with "the entire structure is invalid"...
    that's exactly what I got while parsing...
    , Oct 26, 2007
    #9
  10. wrote:
    > On 26 Ott, 19:23, Dennis Lee Bieber <> wrote:
    > > > (A,B,C,D)
    >>> that could be
    >>> ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)

    >> "C" isn't a tuple in your example either. It is a one-element list
    >> (the single element INSIDE the list is a tuple whose third element is a
    >> list containing a non-terminated string -- so the entire structure is
    >> invalid)
    >>

    >
    > i'm not sure what u mean with "the entire structure is invalid"...
    > that's exactly what I got while parsing...


    Your structure is correct. Dennis just didn't read all the matching
    parens and brackets properly.

    >
    >



    --
    Michael Torrie
    Assistant CSR, System Administrator
    Chemistry and Biochemistry Department
    Brigham Young University
    Provo, UT 84602
    +1.801.422.5771
    Michael L Torrie, Oct 26, 2007
    #10
  11. Stargaming Guest

    On Fri, 26 Oct 2007 14:26:24 -0600, Michael L Torrie wrote:

    > wrote:

    [snip]
    >>>> ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)

    ^
    Syntax error behind ``'bobloblaw``.

    >>> "C" isn't a tuple in your example either. It is a one-element
    >>> list
    >>> (the single element INSIDE the list is a tuple whose third element is
    >>> a list containing a non-terminated string -- so the entire structure
    >>> is invalid)
    >>>
    >>>

    >> i'm not sure what u mean with "the entire structure is invalid"...
    >> that's exactly what I got while parsing...

    >
    > Your structure is correct. Dennis just didn't read all the matching
    > parens and brackets properly.


    He certainly is -- *you* are misreading *him*. The nit he's picking is
    the non-terminated string (quotation mark/apostrophe missing).

    Nit-picking'ly,
    Stargaming
    Stargaming, Oct 26, 2007
    #11
  12. Guest

    On 26 Ott, 23:33, Stargaming <> wrote:
    > He certainly is -- *you* are misreading *him*. The nit he's picking

    is
    > the non-terminated string (quotation mark/apostrophe missing).
    >

    right, now i got it!

    beside this, i'm trying to use the reduceXML function proposed by
    Larry.. but I found out that sometimes pyrxp parses the newline too...
    By now I guess it's not its fault, but it's becuase of the way the xml
    file is written
    , Oct 27, 2007
    #12
  13. On Fri, 26 Oct 2007 12:58:39 -0700, declaimed the
    following in comp.lang.python:

    >
    > i'm not sure what u mean with "the entire structure is invalid"...
    > that's exactly what I got while parsing...
    >


    >>> data = ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)

    Traceback ( File "<interactive input>", line 1
    data = ('tagA', None, [('tagB', None, ['bobloblaw], None)], None)
    ^
    SyntaxError: EOL while scanning single-quoted string
    >>>

    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Oct 27, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Nogradi
    Replies:
    3
    Views:
    349
    Dennis Lee Bieber
    Nov 10, 2006
  2. Replies:
    5
    Views:
    552
    Thomas J. Gritzan
    Oct 6, 2006
  3. xera121
    Replies:
    8
    Views:
    721
    lolmc
    Sep 30, 2009
  4. Replies:
    19
    Views:
    226
    Nobody
    Feb 26, 2013
  5. Jon Reyes
    Replies:
    18
    Views:
    228
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page