Fix Python – Sort tuples based on second parameter

I have a list of tuples that look something like this:
(“Person 1”,10)
(“Person 2”,8)
(“Person 3”,12)
(“Person 4”,20)

What I want produced, is the list sorted in ascending order, by the second value of the tuple. So L[0] should be (“Person 2”, 8) after sorting.
How can I do this? Using Python 3.2.2 If that helps.
….

Fix Python – Custom Python list sorting

I was refactoring some old code of mine and came across of this:
alist.sort(cmp_items)

def cmp_items(a, b):
if a.foo > b.foo:
return 1
elif a.foo == b.foo:
return 0
else:
return -1

The code works (and I wrote it some 3 years ago!) but I cannot find this thing documented anywhere in the Python docs and everybod….

Fix Python – How do operator.itemgetter() and sort() work?

I have the following code:
# initialize
a = []

# create the table (name, age, job)
a.append([“Nick”, 30, “Doctor”])
a.append([“John”, 8, “Student”])
a.append([“Paul”, 22, “Car Dealer”])
a.append([“Mark”, 66, “Retired”])

# sort the table by age
import operator
a.sort(key=operator.itemgetter(1))

# print the table
print(a)

It creates a 4….

Fix Python – Making a python user-defined class sortable, hashable

What methods need to be overridden/implemented when making user-defined classes sortable and/or hashable in python?
What are the gotchas to watch out for?
I type dir({}) into my interpreter to get a list of methods on built-in dicts. Of those, I assume I need to some implement some subset of
[‘__cmp__’, ‘__eq__’, ‘__ge__’, ‘__gt__’, ‘__hash__’, ‘….

Fix Python – sort eigenvalues and associated eigenvectors after using numpy.linalg.eig in python

I’m using numpy.linalg.eig to obtain a list of eigenvalues and eigenvectors:
A = someMatrixArray
from numpy.linalg import eig as eigenValuesAndVectors

solution = eigenValuesAndVectors(A)

eigenValues = solution[0]
eigenVectors = solution[1]

I would like to sort my eigenvalues (e.g. from lowest to highest), in a way I know what is the associated ….

Fix Python – Shuffle two list at once with same order

I’m using the nltk library’s movie_reviews corpus which contains a large number of documents. My task is get predictive performance of these reviews with pre-processing of the data and without pre-processing. But there is problem, in lists documents and documents2 I have the same documents and I need shuffle them in order to keep same order in bo….

Fix Python – Is python’s sorted() function guaranteed to be stable?

The documentation doesn’t guarantee that. Is there any other place that it is documented?
I’m guessing it might be stable since the sort method on lists is guaranteed to be stable (Notes 9th point: “Starting with Python 2.3, the sort() method is guaranteed to be stable”), and sorted is functionally similar. However, I’m not able to find any defin….

Fix Python – How to use a custom comparison function in Python 3?

In Python 2.x, I could pass custom function to sorted and .sort functions
>>> x=[‘kar’,’htar’,’har’,’ar’]
>>>
>>> sorted(x)
[‘ar’, ‘har’, ‘htar’, ‘kar’]
>>>
>>> sorted(x,cmp=customsort)
[‘kar’, ‘htar’, ‘har’, ‘ar’]

Because, in My language, consonents are comes with this order
“k”,”kh”,….,”ht”,…”h”,…,”a”

But In Python 3.x, looks like I cou….

Fix Python – sorting by a custom list in pandas

After reading through: http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.DataFrame.sort.html
I still can’t seem to figure out how to sort a column by a custom list. Obviously, the default sort is alphabetical. I’ll give an example. Here is my (very abridged) dataframe:
Player Year Age Tm G
2967 C….