473,394 Members | 1,734 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,394 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 1273
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) :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.