Monday, January 17, 2011

Some Python Tips

=========================
Some Python Tips
=========================

Print the list
~~~~~~~~~~~~~~~~~
- we should print list like this
>>> print "%s" % '\n '.join(sys.path)

Floating Division
~~~~~~~~~~~~~~~~~
>>> from __future__ import division
>>> 5/2
2.5

Say I want to map and filter a list at the same time. In other words, I'd like to see the square of each element in the list where said element is under 4. Once more, the Python neophyte way:

# 's

1numbers = [1,2,3,4,5]
2squares = []
3for number in numbers:
4    if number < 4:
5        squares.append(number*number)
6# squares is now [1,4,9]

The code is starting to expand in the horizontal direction now! Alas, what could we possibly do to simplify the code? We could try using map and filter, but I don't have a good feeling about this...

# 's

1numbers = [1,2,3,4,5]
2squares = map(lambda x: x*x, filter(lambda x: x < 4, numbers))
3# squares is now [1,4,9]

While map and filter were ugly before, now they're just unreadable. Obviously this isn't a good idea. Once more, list comprehensions save the day:

# 's

1numbers = [1,2,3,4,5]
2squares = [number*number for number in numbers if number < 4]
3# square is now [1,4,9]

This is a bit longer than the earlier list comprehension examples, but in my opinion still very readable. It's definitely better than a for loop or using map and filter.

As you can see, a list comprehension filters then maps. If you absoulutely need to map then filter, things can get more complicated. You might even have to use nested list comprehensions, the map and filterfor loop, depending on what is cleanest. That discussion, though, is outside the scope of this article. commands, or a regular old

Syntax: List Comprehensions and Generator Expressions

A list comprehension has the syntax: [ element for variable(s) in list if condition ]

A generator expression has the syntax: ( element for variable(s) in list if condition )

list anything that can be treated as a list or iterator
variable(s) variable or variables to assign the current list element to, just like in a regular for loop
condition an inline python expression. Scope again includes local scope and variable(s). If this evaluates to true, item will be included in result.
element an inline python expression. Scope includes the local scope and variable(s). This is the actual element that will be included in the result.

The for variable(s) in list bit can be repeated indefinitely.

Using the Lambda Function

~~~~~~~~~~~~~~~~~~~~~

Syntax: Lambda Functions

A lambda function has the syntax: lambda variable(s) : expression

variable(s) a comma-separated list variable or variables that the function can receive. You can't use keywords, and you don't want these to be in parentheses (a mistake I started making for a couple of months and wondered why my lambdas never worked).
expression an inline python expression. Scope includes local scope and variable(s). This is what the function returns

No comments:

Post a Comment