About Me

My photo
i'm a laugning jack-o'-lantern

Tuesday, July 19, 2011

One more tricky thing I like in python

For example, I've got the list of iterable objects:
>>> l = [[1,2,3], [4,5,6], [7,8]]
And I need to get linear list of all elements in those iterables. Here's the trick, related to the list comprehension, I've learned couple of weeks ago:
>>> ll = [i for j in l for i in j]
>>> print ll
... [1, 2, 3, 4, 5, 6, 7, 8]

This constuction may be found a bit complicated at the first glance, but if we'll look at it, we'll get that that's similar to the following code:
>>> for j in l:
>>>     for i in j:
>>>         yield i

Friday, June 24, 2011

zip / unzip

zip() is simple and tricky buit-in python function at the same time.
>>> zip(range(3), '123')
... [(0, '1'), (1, '2'), (2, '3')]
That's simple, heh. :)
The trickest part is when you've got the result of your zip(a, b), but you want to unzip it.
>>> a1 = (1, 2, 3)
>>> b1 = (4, 5, 6)
>>> c = zip(a1, c1)
>>> # woo-a la
>>> a2, b2 = zip(*c)
>>> print a1 == a2 and b1 == b2
... True

We've got the origial values. Congrats!
BTW, unzip is described in python documentation http://docs.python.org/library/functions.html#zip

Wednesday, January 12, 2011

psi+

I always used psi as jabber-client for linux, but there were some feelings that it needs something more for comfortable usage. And couple weeks ago I've found that there are some russian guys who work on psi+ (based on psi) that already has lot's of customizable things. I really love it.

In Debian (and may be Ubuntu, but I haven't checked) it's available in repos, so you could install it:
apt-get install psi-plus