473,322 Members | 1,736 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,322 software developers and data experts.

Access elements from nested tuples

Hi,

t = (1, (2, 3))

I am bit suprised, that I cannot access '3' via:
t[1].[1] # syntax error

But t[1].__getitem__(1) works like expected.

Why is that?

Regards
Georg Sauthoff
Sep 4 '06 #1
3 1907
t = (1, (2, 3))
>
I am bit suprised, that I cannot access '3' via:
t[1].[1] # syntax error

But t[1].__getitem__(1) works like expected.

Why is that?
What is "t"? It's a tuple. A tuple can be indexed, or you can
call its __getitem__ method. Thus, the one-th element of t is either

t[1]

or

t.__getitem__(1)

You're asking for the sub-element of that thing just returned.
Thus, you either need to use

t[1][1]

or

t[1]._getitem(1)

or

t.__getitem__(1)[1]

or

t.__getitem__(1).__getitem__(1)

Imagine that you used

x = t[1]

You wouldn't use

x.[1]

you'd use

x[1]

or

x.__getitem__(1)

Same thing. As simple as search-and-replace of "t[1]" with "x"

-tkc


Sep 4 '06 #2
jss
Hi Georg!

In [1]: t=(1,(2,3))
I am bit suprised, that I cannot access '3' via:
t[1].[1] # syntax error

But t[1].__getitem__(1) works like expected.
In [2]: k=t[1]

In [3]: k[1]
Out[3]: 3

In [4]: t[1][1]
Out[4]: 3

In [5]: k.__getitem__(1)
Out[5]: 3

In [6]: k.[1]
------------------------------------------------------------
File "<ipython console>", line 1
k.[1]
^
SyntaxError: invalid syntax

Note that:
In [7]: type(t[1])
Out[7]: <type 'tuple'>
just as t.

hth,
jss
Sep 4 '06 #3
On 2006-09-04, Tim Chase <py*********@tim.thechases.comwrote:

Hi,

[nested tuples]

thanks - I should not post before 8 am or 10 pm ...

Regards
Georg Sauthoff
Sep 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: omission9 | last post by:
I have a dictionary that looks like this MY_DICT=FOO I am having a problem updating this with a simple MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting into the inner...
8
by: Nickolay Kolev | last post by:
Hi all, I have a list whose length is a multiple of 3. I want to get a list of tuples each of which has 3 consecutive elements from the original list, thus packing the list into smaller...
5
by: Markus Ernst | last post by:
Hi I have a validation problem with a form and nested divs. I understand what the problem is, but I don't see how to fix it. This is my normal page structure, and it validates: <!DOCTYPE HTML...
5
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I'm trying to solve this problem: suppose I'm reading a csv file and want to create a tuple of all those rows and values, like...
5
by: ZikO | last post by:
Hi there. I have a problem. I have created nested classes but don't know how to access to inner classes. I know I can create objects: Hen Object; Hen::Nest ObjectNest; Hen::Nest::Egg...
1
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
1
by: Henrik Bechmann | last post by:
All, I'm trying to spoof Google's vertical tabs in a vertical menu structured with nested UL/LI elements. To do this, I need to find out where the anchor in the LI is, and then create an...
37
by: Prisoner at War | last post by:
Actually, it doesn't have to be a blockquote...but I'm at my wits' end: I want to make bold several lines of text which have a pair of <br /tags between them...seems like the <b></bdo not "carry...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.