471,350 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

tuples within tuples

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 '07 #1
9 1206
ko*******@gmail.com 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
Oct 26 '07 #2

[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 '07 #3
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
Oct 26 '07 #4
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 '07 #5
ko*******@gmail.com 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.

Oct 26 '07 #6
On 26 Ott, 19:23, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
(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 '07 #7
ko*******@gmail.com wrote:
On 26 Ott, 19:23, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
(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

Oct 26 '07 #8
On Fri, 26 Oct 2007 14:26:24 -0600, Michael L Torrie wrote:
ko*******@gmail.com 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
Oct 26 '07 #9
On 26 Ott, 23:33, Stargaming <stargam...@gmail.comwrote:
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 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by Jeff Wagner | last post: by
66 posts views Thread by Darren Dale | last post: by
66 posts views Thread by Mike Meyer | last post: by
10 posts views Thread by rshepard | last post: by
12 posts views Thread by rshepard | last post: by
10 posts views Thread by victor.herasme | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.