[Tutor] A couple beginner questions.
Ignacio Vazquez-Abrams
ignacio@openservices.net
Tue, 25 Sep 2001 02:03:44 -0400 (EDT)
On Mon, 24 Sep 2001, Eric Henry wrote:
> Hi there everyone. I decided work on a small project to sort of get my
> feet wet in python and programming in general. I've got a couple of
> quick questions for you all. First, is there a built in way to apply
> some operation to each item in a list? For example adding 2 to [1,2,3],
> and getting [3,4,5].
map(lambda x: x+2, [1, 2, 3])
> Second, I need to store some information from a table. Each row has 3
> values. Somewhat like this:
>
> [1, 3, 4]
> [2, 4, 5]
> [3, 1, 1]
>
> From what I understand, a dictionary would let me assign each of those
> sets of information a key and retrieve them, but what I need to do is
> give it say 3 in the second column(the numbers aren't duplicated
> anywhere in each column) and have it return one or four. I don't know
> how clear that all was, but hopefully someone has some idea what I'm
> talking aobut.
You weren't clear on which 1 or 4 you wanted...
---
t=[[1, 3, 4], [2, 4, 5], [3, 1, 1]]
for i in t:
if i[1]==3:
print i
idx=t[0].index(3)
for i in t[1:]:
print i[idx]
---
--
Ignacio Vazquez-Abrams <ignacio@openservices.net>