Archive for July, 2009

Why you still need map and filter in Python

Monday, July 27th, 2009

This post is written for Python 2.5 (and possibly other Python 2.x series).

In most cases we can use list comprehension instead. Sometimes using map and filter is better for readability. But in rare cases, map and filter are more convenient.

Filter can produce string or tuple

List comprehension always produces a list. This is not nonsense. Filter will produce a string or tuple if the source sequence is a string or tuple. For example:

[x for x in (1,2,3) if x > 1]

returns [2,3].

But

filter((lambda x: x > 1), (1,2,3))

returns (2,3).

Therefore, if you want to filter a string tuple and retain its type in output, using filter is more convenient. Using list comprehension requires type conversion if you want to retain sequence types. For example, to get the same result above but using list comprehension instead of filter:

tuple([x for x in (1,2,3) if x > 1])

This works when you can predict that the source is a tuple (or a string). If not, the situation becomes complex. In that case, you have to write functions to determine the type of the source and convert them accordingly.

Map treats multi-sequences differently

Well, map cannot produce strings or tuples. I guess this is because sometimes the mapping function may become complex, in which cases, retaining the original sequence type becomes unsuitable.

But still, sometimes you may want to use map while dealing with multiple sequences. Both map and list comprehension can handle multiple sequences, but they treat sequences in different ways.

Example:

map(pow, [1,2,3],[2,3,4])

returns [1, 8, 81],

while the analogous list comprehension

[x**y for x in [1,2,3] for y in [2,3,4]]

returns:

[1, 1, 1, 4, 8, 16, 9, 27, 81]

See the difference? In such cases, map and list comprehension are not interchangeable. Well, this is arguable. The roughly equivalent list comprehension is

a=[1,2,3]
b=[2,3,4]
[a[i]**b[i] for i in range(len(a))]

I guess the map version is more clear. Besides, what if list b is longer than list a? The map function will (probably) raises an error, while list comprehension silently ignores the remaining items.

回首2001年

Monday, July 27th, 2009

現在一直見到的.info、.name都是在2001年開始使用的。

藍牙獲得了很大的發展,但還沒有大規模商用。事實上當時Wi-Fi和紅外被認為是藍牙的有力競爭對手。

鐵通誕生了。

pcHome.net是著名的下載站。

北大計劃在10年內研制出與世界先進水平相當的通用CPU。

火狐是浪潮家用PC的子品牌。

毛主席說:“人間正道是滄桑。”

identi.ca

Sunday, July 19th, 2009

I started to use the micro-blogging service identi.ca. It’s based on the Laconica free software, which supports the OpenMicroBlogging protocal, meaning that you can have friends on other microblogging services that can send/receive notices with you. You may think it the XMPP protocal for micro-blogging.

For me, micro-blogging is quicker, more informal, and more light-weighted than blog.

Mobidentica is the mobile identica/Laconica client I use. It’s a bit too simple. But the good news is that it’s free/open-source software and it’s installable on almost all mobile phones supporting J2ME. Good when you are away from your computer.

I’m on identi.ca.