Referencing Items in a List of Tuples

R

rshepard

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
 
R

Rune Strand

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[0] == 'eco' etc.
 
J

John Machin

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
 
P

Paul Rubin

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


Preferable:

for i,m in enumerate(mainlist):
if m[0] == 'eco' etc.
 
D

Dennis Lee Bieber

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.
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)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
P

Paddy

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.
 
J

Jussi Salmela

Paddy kirjoitti:
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
 
R

rshepard

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
 
P

Paddy

Paddy kirjoitti:


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'] ]

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.
 
B

Bruno Desthuilliers

(e-mail address removed)-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 ?
 
R

rshepard

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
 
R

rshepard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top