[Tutor] newbie with some basic questions

alan.gauld@bt.com alan.gauld@bt.com
Wed, 1 May 2002 17:39:59 +0100


Sorry if this is answered I'm playing catch up on 
the digests(again!)

> im a total newbie to both programming and python (well i can write a 
> decent shell script...).

So I'll assume you are on *nix...

> just a few questions, most important one first:
> 1.whats better for a newbie like myself, learning python or 
> core python programming? or any other recommendations?

If you can do non trivial shell scripts then either is fine.
If you are only doing basic shell scripts then I'd go with 
Core Python or maybe quick Python. (Or even my book! :-)

> 2.how can i make input take only one character and not requrie a CR?

You can't. You need curses.getch()

> 3.i have a simple list of lists for example: people = 
> [["A","john","12345"],["B","joe","1231234"],["X","sally","1234"]]
> and i need to do something like:
> some_variable = X
> new_variable = people[*][0] 
> that is, find the corresponding list with the list that has [0] = X
> 
> i could use a dictionary like {"X":["sally","1234"]} but is there a 
> better way to do it?

You know too much for my book, I'd go with Learning Python.... :-)

A Dictionary looks like the best way (what do you mean "better"?
That implies you don't think the dictionary approach is good...)

The alternative is to traverse the list looking for X in 
the first position:

for L in people:
   if L[0] == some_variable: 
      break # L is your sublist

Or in one line:

sublist = filter(lambda L, v=some_variable: L[0] == v, people)

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld