By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,550 Members | 2,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,550 IT Pros & Developers. It's quick & easy.

Referencing Items in a List of Tuples

P: n/a
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 each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.

If I try, for example, writing:

for item in mainlist:
if mainlist[item][0] == 'eco' and mainlist[item][1] == 'con':
ec.Append(mainlist[item][2:])

python doesn't like a non-numeric index.

I would really appreciate a pointer so I can learn how to manipulate lists
of tuples by referencing specific items in each tuple (string or float).

Rich
Feb 25 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Feb 25, 3:01 am, rshep...@nospam.appl-ecosys.com wrote:
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 each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.

If I try, for example, writing:

for item in mainlist:
if mainlist[item][0] == 'eco' and mainlist[item][1] == 'con':
ec.Append(mainlist[item][2:])

python doesn't like a non-numeric index.
try this instead:
for item in mainlist:
if item[0] == 'eco' and item[1] == 'con':
ec.append(item[2:])

if you want numeric adressing, try:
for i in range(len(mainlist)):
if mainlist[i][0] == 'eco' etc.

Feb 25 '07 #2

P: n/a
On Feb 25, 1:01 pm, rshep...@nospam.appl-ecosys.com wrote:
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 each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.

If I try, for example, writing:

for item in mainlist:
item is one of your 30-element tuples, ...

if mainlist[item][0] == 'eco' and mainlist[item][1] == 'con':
so do this:

if item[0] == 'eco' and item[1] == 'con':
ec.Append(mainlist[item][2:])
and

ec.append(item[2:]) # note append, not Append
>
python doesn't like a non-numeric index.
If nothing. Python doesn't like a non-numeric index, quite
irrespective of what you do :-)

I would really appreciate a pointer
Sorry, only nasty languages have pointers :-)
so I can learn how to manipulate lists
of tuples by referencing specific items in each tuple (string or float).
HTH,
John

Feb 25 '07 #3

P: n/a
"Rune Strand" <ru*********@gmail.comwrites:
if you want numeric adressing, try:
for i in range(len(mainlist)):
if mainlist[i][0] == 'eco' etc.
Preferable:

for i,m in enumerate(mainlist):
if m[0] == 'eco' etc.
Feb 25 '07 #4

P: n/a
On Feb 25, 2:01 am, rshep...@nospam.appl-ecosys.com wrote:
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 each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.

If I try, for example, writing:

for item in mainlist:
if mainlist[item][0] == 'eco' and mainlist[item][1] == 'con':
ec.Append(mainlist[item][2:])

python doesn't like a non-numeric index.

I would really appreciate a pointer so I can learn how to manipulate lists
of tuples by referencing specific items in each tuple (string or float).

Rich
You might also use list comprehensions to accumulate the values you
need:

ec = [ item[2:] for item in mainlist if item[:2] == ['eco','con'] ]

- Paddy.

Feb 25 '07 #5

P: n/a
Paddy kirjoitti:
On Feb 25, 2:01 am, rshep...@nospam.appl-ecosys.com wrote:
> 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 each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.

If I try, for example, writing:

for item in mainlist:
if mainlist[item][0] == 'eco' and mainlist[item][1] == 'con':
ec.Append(mainlist[item][2:])

python doesn't like a non-numeric index.

I would really appreciate a pointer so I can learn how to manipulate lists
of tuples by referencing specific items in each tuple (string or float).

Rich

You might also use list comprehensions to accumulate the values you
need:

ec = [ item[2:] for item in mainlist if item[:2] == ['eco','con'] ]

- Paddy.
I'm nitpicking, but the OP has a list of tuples:

ec = [ item[2:] for item in mainlist if item[:2] == ('eco','con') ]
Cheers,
Jussi
Feb 25 '07 #6

P: n/a
On 2007-02-25, Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
Item is ALREADY the "current" tuple...

for tpl in mainlist:
if tpl[0] == "eco" and tpl[1] == "con":
ec.Append(tpl[2:]) #presuming ec is NOT a list, as Append()
#is not a list method (append() is)
Of course! As a morning person I don't do as well at night. That's such a
silly error I'm embarrassed by having to have the obvious pointed out to me.
My apologies to all.

And, the .Append comes from list widgets in wxPython.

Thank you all for forcing my head straight again.

Rich
Feb 25 '07 #7

P: n/a
On Feb 25, 5:44 pm, Jussi Salmela <tiedon_j...@hotmail.comwrote:
Paddy kirjoitti:
On Feb 25, 2:01 am, rshep...@nospam.appl-ecosys.com wrote:
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 each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.
If I try, for example, writing:
for item in mainlist:
if mainlist[item][0] == 'eco' and mainlist[item][1] == 'con':
ec.Append(mainlist[item][2:])
python doesn't like a non-numeric index.
I would really appreciate a pointer so I can learn how to manipulate lists
of tuples by referencing specific items in each tuple (string or float).
Rich
You might also use list comprehensions to accumulate the values you
need:
ec = [ item[2:] for item in mainlist if item[:2] == ['eco','con'] ]
- Paddy.

I'm nitpicking, but the OP has a list of tuples:

ec = [ item[2:] for item in mainlist if item[:2] == ('eco','con') ]

Cheers,
Jussi
Of course. Ta.
- paddy.

Feb 25 '07 #8

P: n/a
rs******@nospam.appl-ecosys.com a écrit :
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.
t = "a", "b", "c"
assert t[0] == "a"
assert t[1] == "b"
assert t[2] == "c"

I find references to sorting a list of tuples, but not
extracting tuples based on their content.
loft = [(1, 2, 3), (1, 2, 4), (2, 3, 4)]
starting_with_one = [t for t in loft if t[0] == 1]
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 each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.

If I try, for example, writing:

for item in mainlist:
if mainlist[item][0] == 'eco' and mainlist[item][1] == 'con':
ec.Append(mainlist[item][2:])

python doesn't like a non-numeric index.
Hem... I'm afraid you don't understand Python's for loops. The above
should be:

ec = []
for item in mainlist:
if [item][0] == 'eco' and item[1] == 'con':
ec.append(item[2:])

which can also be written:
ec = [item[2:] for item in mainList \
if item[0] == 'eco' and item[1] == 'con']
I would really appreciate a pointer so I can learn how to manipulate lists
of tuples by referencing specific items in each tuple (string or float).
Did you actually tried reading some Python 101 material ?

Feb 25 '07 #9

P: n/a
On 2007-02-25, Paddy <pa*******@googlemail.comwrote:
You might also use list comprehensions to accumulate the values you
need:

ec = [ item[2:] for item in mainlist if item[:2] == ['eco','con'] ]
Thank you, Paddy. That's the syntax I couldn't work out myself.

Rich
Feb 26 '07 #10

P: n/a
On 2007-02-25, Jussi Salmela <ti*********@hotmail.comwrote:
I'm nitpicking, but the OP has a list of tuples:
ec = [ item[2:] for item in mainlist if item[:2] == ('eco','con') ]
Jussi,

An excellent nit to pick.

Thank you,

Rich
Feb 26 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.