tuples within tuples

J. Cliff Dyer jcd at sdf.lonestar.org
Sat Oct 27 22:47:17 CEST 2007


J. Clifford Dyer wrote:
> On Fri, Oct 26, 2007 at 06:59:51AM -0700, korovev76 at gmail.com 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.
>
>   
Also, as noted elsewhere in the string, your C is actually a list of
Cs.  So replace my else statement with

return [(child[0], reduceXML(child[2])) for child in node]

I think that'll do what you need.  However, I'm not sure why you need
to.  You're not getting any new information out of your data structure. 
Why not just ignore the attributes and the spare tuplet (or whatever you
call tuple-items) when you do the real processing next time through?

Cheers,
Cliff

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071027/7427ca2e/attachment.html>


More information about the Python-list mailing list