Recent Notes

Displaying keyword search results 1 - 10
Created by Dr. Xi on April 18, 2011 12:10:37    Last update: April 18, 2011 12:14:24
Python defines a long list of special methods to customize class behavior. This is a short list for the Basic Customizations . Method Description object.__new__(cls[, ...]) Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. __new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[,...]) Called when the instance is created. The arguments are those passed to the class constructor expression. If a base class has an __init__() method, the derived class’s __init__()...
Created by mak on March 02, 2011 15:50:56    Last update: March 02, 2011 15:50:56
Python list comprehension with if-else condition in general: new_list =[ (F, T) [boolean test] for x in old_lis... #!/usr/local/bin/python l = [ 1, 2, 3, 4 ] ...
Created by Dr. Xi on September 10, 2010 22:53:42    Last update: September 10, 2010 22:54:06
The sort operation for a Python list sorts a list in place . It takes three optional arguments to control the comparisons: s.sort([cmp[, key[, reverse]]]) cmp specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()) . The default value is None . key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower . The default value is None . reverse is a boolean value. If set to True , then the list elements are sorted as if each comparison were reversed. Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC...
Created by Dr. Xi on September 10, 2010 16:30:16    Last update: September 10, 2010 16:30:16
Perl uses the pop function to remove an item from the end of an array, and the shift function to remove an item from the beginning of an array. Python does not have a shift function, but its pop function takes a optional parameter. To remove an item from the beginning of a list , simply pop(0) . >>> a = [] >>> a.append(1) >>> a.append(2) ...
Created by Dr. Xi on November 15, 2009 21:56:19    Last update: November 15, 2009 21:57:59
Python doesn't seem to have a built-in function to find an object in a list that satisfies a specified criterion, for example, making a function f to return True . To find jack among people : jack = None for p in people: if p.first_... This is not a lot of code, but it's not terse enough as you would expect of Python. Some suggestions are offered here: http://dev.ionous.net/2009/01/python-find-item-in-list.html http://tomayko.com/writings/cleanest-python-find-in-list-function
Created by Dr. Xi on November 15, 2009 21:44:23    Last update: November 15, 2009 21:44:23
Python lists are enclosed in square brackets. When a list of objects are enclosed in parentheses it is a tuple (an immutable list). List comprehensions are enclosed in square brackets. But when you change the square brackets to parentheses, you get a generator not a tuple ! >>> l = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> t = (1,...
Created by Dr. Xi on November 09, 2008 23:17:13    Last update: June 24, 2009 19:26:38
# The one liner frequency_dict.keys().sort() d...
Created by Dr. Xi on June 06, 2009 20:05:49    Last update: June 06, 2009 20:06:32
Use os.listdir to list the contents of a directory: import os # list the contents of mydir. Cur...
Created by Dr. Xi on May 02, 2009 22:18:48    Last update: May 02, 2009 22:19:11
Obtain a slice with the step parameter: new_list = old_list[start:stop:step] >>> a ['a', 1, 'b', 2, 'c', 3] >>> a[::2] ...
Created by Dr. Xi on May 02, 2009 21:25:35    Last update: May 02, 2009 22:07:40
Use the built-in dict function: >>> dict([(1,2), (3,4), (5,6)]) {1: 2, 3: 4, 5:... Or, if you have a list with even number of elements and want to use the odd numbered elements for key and even numbered elements for value: >>> a = [ 'a', 1, 'b', 2, 'c', 3 ] >>> dict([(a...
Previous  1 2 Next