473,804 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1291
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(myTup le):
if n in (0,2):
myList.append(i tem)

-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_childre n, spare)
Where list_of_childre n might "be a list with elements that are 4-
tuples or plain strings".

In other terms, if I have something like this:
('<tagA><tagB>b obloblaw</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_childre n, spare)
Where list_of_childre n might "be a list with elements that are 4-
tuples or plain strings".

In other terms, if I have something like this:
('<tagA><tagB>b obloblaw</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_childre n, spare)
Where list_of_childre n might "be a list with elements that are 4-
tuples or plain strings".

In other terms, if I have something like this:
('<tagA><tagB>b obloblaw</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.visitDe fault
fn(tag, attrs, children, spare)

for child in children:
if isinstance(chil d, tuple):
walktree(child, visitor)
else:
visitor.visitCo ntent(child)

class Visitor:
def visitDefault(se lf, t, a, c, s): pass
def visitContent(se lf, 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.net com.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.net com.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...@gma il.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
2765
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 a tuple? I understand that a tuple is immutable and a list is mutable but there has to be more to it than just that. Everything I tried with a list worked the same with a tuple. So, what's the difference and why choose one over the other? Jeff
3
2123
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 and lists is that tuples are immutable, so if this is correct, than list are a superset of tuples, meaning lists can do everything tuples can do and more. Is there any advantage for using tuples? Are they "faster"? Consume less memory? When is...
66
5042
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
3525
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 values side. IIRC, "apply" used to require that the second argument be a tuple; it now accepts sequences, and has been depreciated in favor of *args, which accepts not only sequences but iterators. Is there any place in the language that still...
5
2367
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 possible to make a pointer, which can point to all of these tuples?) #include <tr1/tuple>
10
5160
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 of tuples, but not extracting tuples based on their content. In my case, I have a list of 9 tuples. Each tuple has 30 items. The first two items are 3-character strings, the remaining 28 itmes are floats. I want to create a new list from...
12
4171
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 like this: ('eco', "(u'Roads',)", 0.073969887301348305) Pysqlite doesn't like the format of the middle term: pysqlite2.dbapi2.InterfaceError: Error binding parameter 1 - probably
122
5544
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 the Sequence Types page of the Library Reference (http://docs.python.org/lib/typesseq.html); it wasn't there. A search of the Library Reference's index seemed to confirm that the function did not exist. A little later I realized it might be called...
10
1003
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) : for j in xrange(1, 5, 1) :
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10315
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.