[Tutor] New to list & first steps in Python

Vijay Kumar Bagavath Singh vijaykumar at linuxmail.org
Thu Jun 3 05:42:28 EDT 2004


The best thing about python is that you get to write very little code for even the most complicated thing. Check out this super compressed code, that acheives the same result?

from random import choice

fil = file("/tmp/ElisabethInsults.txt", "r")
adj1, adj2, nouns = zip(*[line.split() for line in fil])
print "Thou", choice(adj1), choice (adj2), choice(nouns) + "!"
fil.close()

To understand this program you will have to understand
1) List comprehension
2) Unpacking argument lists
3) Zip function
4) Multiple assignment

List comprehesion is a short hand for creating complicated lists. The following example illustrates list comprehension.
>>> [i*2 for i in range(0, 10)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

The arguments to a function can be passed on in a single tuple or list. The tuple/list has to be preceded by a * for the unpacking to happen. The following example illustrates unpacking of argument lists.
>>> range(0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> arg=(0,10)
>>> range(*arg)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The zip function(added in Python 2.0): This function returns a list of tuples, where the I-th tuple contains the I-th element from each of the argument sequences.
>>> numbers = [1, 2, 3, 4]
>>> alpha = ['a', 'b', 'c', 'd']
>>> zip(numbers, alpha)
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
>>> z = zip(numbers,alpha)
z = zip(numbers,alpha)

Using unpacking of argument lists in zip function results in inverse operation of the zip function.
>>> zip(*z)
[(1, 2, 3, 4), ('a', 'b', 'c', 'd')]

Following is an example of multiple assignment
>>> a, b, c = 1, 2, 3
>>> print a, b, c
1 2 3

With recent versions of Python(2.2 and above) you can use
file("filename", "r")
to open files instead of
open("filename", "r")
also "r" is the default mode so you could use
file("filename")

With recent versions of Python(2.2 and above) you can use
for line in fil
instead of
for line in fil.readlines()

Regards,
Vijay

> Date: Thu, 3 Jun 2004 03:51:05 +0200
> From: Chris F Waigl <cwaigl at free.fr>
> Subject: [Tutor] New to list & first steps in Python
> To: Python Tutor <tutor at python.org>
> Message-ID: <200406030351.05426.cwaigl at free.fr>
> Content-Type: text/plain;  charset="us-ascii"
> 
> I am thrilled to find that there is a list where beginners can find 
> guidance. Thanks for taking the time to help!
> 
> I'm interested in natural language processing, and chose Python as a 
> programming language. I'm working my way through various tutorials 
> right now, but I've only just begun. I've also changed over from W98 to 
> Debian GNU/Linux and am not yet familiar with the modules installed on 
> my box with Python 2.3. I use Idle, which I find very convenient. 
> 
> Right now I'm mostly interested in not adopting bad habits -- learning 
> "good" style from the very beginning. In a different life, I used C for 
> number-crunching type programming (solving equations and the like), but 
> I never particularly enjoyed it. 
> 
> Here's my first script not copied from a tutorial. It works, so the only 
> question is, should I have done something differently? (Well, it's so 
> short, there's not much to _do_.)
> 
> from random import *  # or "... import choice" ?
> adj1 = []
> adj2 = []
> nouns = []
> file = open("ElisabethInsults", "r")
> for line in file.readlines():
>     words = line.split()
>     adj1.append(words[0])
>     adj2.append(words[1])
>     nouns.append(words[2])
> print "Thou", choice(adj1), choice (adj2), choice(nouns) + "!"
> file.close()
> 
> ElisabethInsults is a file containing three rows, 2 x adjectives, 1 x 
> nouns, of derogatory words taken from Shakespeare. (If you're 
> interested, it's here: http://kastalia.free.fr/misc/ElisabethInsults )
> The script creates a random insult, e.g.
> "Thou clouted onion-eyed pigeon-egg!"
> 
> I'm planning to put together something like a very simple tokenizer, 
> something that reads in a text, counts sentences and words, handles all 
> the punctuation correctly, tells me something about word frequencies 
> and the presence or absence of grammatical structures I might be 
> interested in. I'm certainly going to run into a lot of problems soon.
> 
> Any comments and recommendations are warmly encouraged.
> 
> Best,
> Christine
> 
-- 
______________________________________________
Check out the latest SMS services @ http://www.linuxmail.org 
This allows you to send and receive SMS through your mailbox.


Powered by Outblaze



More information about the Tutor mailing list