Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Python: Turning One List Into Another
Index -> Programming, Python -> Python Tutorials
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Sep 05, 2005 3:38 pm   Post subject: Python: Turning One List Into Another

Knowing that Python has become popular with at least some members of our community, I'd like to endeavor to explore the functional side of Python, so you can understand how to write more effective code.

The subject of this tutorial will be generating one list from another.

A naive approach would be:

Python:
foo = [1, 2, 3]
bar = []
for x in foo:
   bar.append(x + 1)


This simply increments each of the value in foo and stores the resulting numbers in bar.

This seems tedious, and that's because it is.

Let's look at using map. The map function takes a function and applies it to each element of a list, creating a new list to store the result.

Python:
foo = [1, 2, 3]
bar = map(lambda x: x + 1, foo)


Much better. A nice side-effect of this is that if we don't want to create a new variable, we can simply assign the result back to foo.

Python:
foo = [1, 2, 3]
foo = map(lambda x: x + 1, foo)


But this is still tedious.

Python:
lambda x: x + 1


You may not have realized it, but this is terribly redundant. In Python, the + operator is just syntactic sugar.

Python:
x + 1


Is just a different way of writing:

Python:
x.__add__(1)


Equivalent to this would be:

Python:
1 .__add__(x)


The extra space is added to avoid confusion. An integer constant followed by a dot is seen as a floating point literal.

Of course, all functions in Python are first clss entities, so:

Python:
1 .__add__


Is a function which can be passed around like any variable. When applied to an argument, it adds 1 to that argument.

Therefore, our map can look like:

Python:
foo = [1, 2, 3]
bar = map(1 .__add__, foo)


If we compare this to a pure functional language, we can see how it's not far off.

code:
foo = [1, 2, 3]
bar = map (1 +) foo
Sponsor
Sponsor
Sponsor
sponsor
Hikaru79




PostPosted: Mon Sep 05, 2005 5:53 pm   Post subject: (No subject)

Then there's the simple list comprehension:
Python:
>>> foo = [1,2,3]
>>> bar = [x+1 for x in foo]
>>> print bar
[2,3,4]


Python's great Very Happy
wtd




PostPosted: Mon Sep 05, 2005 6:02 pm   Post subject: (No subject)

The Rubyist in me demands the following demonstration before people become too enamored of Python. Wink

code:
foo = [1, 2, 3]
bar = foo.map { |x| x.succ }
shaon




PostPosted: Mon Jan 14, 2008 11:17 pm   Post subject: Re: Python: Turning One List Into Another

hmm... i wasnt sure how to insert the python text with background and all so ill just type how to work with lists...

1) copying one list into a brand new list with no connection to the old list:

>> a = [0,1,2,3,4]
>> b = a[0:]
>> print b
>> [0,1,2,3,4]
>> # if we change b[0] to -1
>> b[0] = -1
>> print b
>> [-1,1,2,3,4]
>> # "a" is unaffected by all changes in "b"

2) if say you wanted to keep a list connected to another list so that if something changes in one list it changes in the other:
>> a = [0,1,2,3,4]
>> b = a
>> print a
>> [0,1,2,3,4]
>> print b
>> [0,1,2,3,4]
>> lets change b[0] to -1 and look what happens to a...
>> b[0] = -1
>> print a
>> [-1,1,2,3,4] # a[0] has changed even though we made the changes in b. so....

If we want to make a brand new copy of a list, we use b = a[0:] where "b" is the new list and "a" is the old, but if we want to make a copy where modifications can be done to either list, then we use b=a where b is the new list and a is the old.

using lambada is over-doing python code Wink
shaon




PostPosted: Mon Jan 14, 2008 11:20 pm   Post subject: Re: Python: Turning One List Into Another

oooooo.... i think i just mis-interpreted the whole tutorial.... Sad not good...
Display posts from previous:   
   Index -> Programming, Python -> Python Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: