Recently,we begin our new project named Sparrow.It's a project manage and trace application.
We decide to find a easy to use and free license IDE for our team.After a several of days,we found a good tools open komodo .It's support a lot of existing dynamic language.such as python,ruby,lisp and several of dynamic language such as django. etc....Also it's support customizable feature ,such as customize command,customize menu etc.. We'll use this IDE in our Sparrow application.
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Monday, March 10, 2008
Friday, March 7, 2008
Lock step iteration in Python
Today I will log this big feature in python 2.x.That is lock set iteration.so what's that?let's assume,we have two list and for some reason we want to iteration them in the same time.In other language(eg.Java).we need a nesting loop.But in python .we can use map function,it's build-in.following code is a example.
>>a=(1,2,3)
>>b=(4,5,6)
>>for i in map(None,a,b): print i
The result is :
(1, 4)
(2, 5)
(3, 6)
Wow.It's cool ,right.now assume the length of each list is different.for example,we set them like this
>>a=(1,2,3)
>>b=(4,5,6,7,8)
The result is:
(1, 4)
(2, 5)
(3, 6)
(None, 7)
(None, 8)
As you can see,now we have two extra small list with the element None.Yes,Python auto append a None type in last list,because it is the first parameter in the map function.Until now,it looks good ,right ?we only write one line code ,then map function do two times loop.But it's not elegant.because :
>>a=(1,2,3)
>>b=(4,5,6)
>>for i in map(None,a,b): print i
The result is :
(1, 4)
(2, 5)
(3, 6)
Wow.It's cool ,right.now assume the length of each list is different.for example,we set them like this
>>a=(1,2,3)
>>b=(4,5,6,7,8)
The result is:
(1, 4)
(2, 5)
(3, 6)
(None, 7)
(None, 8)
As you can see,now we have two extra small list with the element None.Yes,Python auto append a None type in last list,because it is the first parameter in the map function.Until now,it looks good ,right ?we only write one line code ,then map function do two times loop.But it's not elegant.because :
- It is non-obvious to programmers without a functional
programming background.
- The use of the magic `None' first argument is non-obvious.
- It has arbitrary, often unintended, and inflexible semantics
when the lists are not of the same length: the shorter sequences
are padded with `None'.
Fortunately,we have another function,zip.yes it's good to use.let me give a example:
>>a=(1,2,3)
>>b=(4,5,6,7,8)
>>zip(a,b)
The result is:
(1, 4)
(2, 5)
(3, 6)
Yes,as you can see,no other two redundant list.it's more clearly.of course,if you think that
other twos is useful,you can also use the map function.But now we have another choose.
Subscribe to:
Posts (Atom)