473,287 Members | 3,181 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 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 1269
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
3
by: Thorsten Kampe | last post by:
I found out that I am rarely using tuples and almost always lists because of the more flexible usability of lists (methods, etc.) To my knowledge, the only fundamental difference between tuples...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
5
by: fff_afafaf | last post by:
Do you know is it possible to put different kinds of tuples to one container? E.g. to a vector? (The lengths of the tuples are different, and also the types in the tuples are different.. -Is it...
10
by: rshepard | last post by:
While working with lists of tuples is probably very common, none of my five Python books or a Google search tell me how to refer to specific items in each tuple. I find references to sorting a list...
12
by: rshepard | last post by:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding the solution in the docs I have here. Data are assembled for writing to a database table. A representative tuple looks...
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
10
by: victor.herasme | last post by:
Hi Everyone, i have another question. What if i wanted to make n tuples, each with a list of coordinates. For example : coords = list() for h in xrange(1,11,1): for i in xrange(1, 5, 1) :...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.