Ross Wan's World!

Python, Ajax, PHP and Linux.

zip() == unzip()

Posted by Ross Wan 于 2012/03/11

zip()是 Python 的内建函数,示例:

a = ('a', 'b', 'c', 'd')
b = (1,2,3,4)
zip(a,b)

返回是一个 iterator, list(zip(a,b)), 显示如下:

[(‘a’, 1), (‘b’, 2), (‘c’, 3), (‘d’, 4)]

但是, 要逆转怎么做呢?很简单:)

zip(*zip(a,b))

list(zip(*zip(a,b)) 显示如下:

[(‘a’, ‘b’, ‘c’, ‘d’), (1,2,3,4)]

Have fun!

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

 
%d 博主赞过: