-1

Let's say I want to sort rows and I want to resolve any ties with the next column, subsequent ties to with the next-next column etc.

In python words the equivalent of sorted(rows, key=itemgetter(1, 2, 3, 4, ...)).

I tried writing my own generator but sorted doesn't iterate over my generator as it does with the tuple itemgetter returns. Any advice?

Dim
  • 164
  • 7
  • 3
    Can you post what you tried with the results? – Burhan Khalid Jul 22 '12 at 10:55
  • 3
    If the rows are lists/tuples (as it seems from your post), then sorting according to column order is the default behavior in python. – Guy Adini Jul 22 '12 at 11:00
  • @Adini No actually they aren't, it's a die_roll() function which I want to be called again until the ties sorted() encounters are resolved. – Dim Jul 22 '12 at 11:16
  • `sorted` can't operate over a generator because a generator gets consumed after the first pass. The sorting algorithm used by `sorted` needs to make multiple passes over the data. It also needs to rearrange that data. It doesn't make sense to have a sorting algorithm that can only see one row at a time and cannot rearrange the data. If you want to sort the results of your generator, do `sorted(list(my_gen_expr))`. – Joel Cornett Jul 22 '12 at 11:17
  • @Cornett But my generator is infinite. Sth like itertools.repeat(my_function). I think the problem is sorted() needs to know the whole iterable "key=" holds for its sorting alg as you say – Dim Jul 22 '12 at 11:19
  • @SerraSouth: You are misusing `sorted`. Please state in your question the algorithm by which you plan on sorting your generator results. – Joel Cornett Jul 22 '12 at 11:56
  • @JoelCornett: `sorted` works on any iterable including a generator. There is no need to explicitly convert it to a list - that will be done by `sorted` internally. – interjay Jul 22 '12 at 11:58
  • @interjay: You're right, I misspoke. Either way though, I think that the OP is not quite explaining what he wants. – Joel Cornett Jul 22 '12 at 12:00
  • If you have an infinite amount of data, how are you expecting to sort it? There could always be a "smaller" element further down the line that hasn't been generated yet... – Karl Knechtel Jul 22 '12 at 13:30

2 Answers2

1

For the reasons noted in the comments, you cannot sort a list of things that hasn't been yet created. Generators exist to yield results when they are asked for so you can't sort a an iterable that hasn't been iterated (as with list(generator()).

To put in more ordinary terms, I'm thinking of ten names but am not telling you what they are yet, please sort them into alphabetical order. You should respond "how can I sort them when you haven't given them to me?" and you'd be correct: you can't.

msw
  • 42,753
  • 9
  • 87
  • 112
0

OK, here's what you say you want to do:

I want to sort rows and I want to resolve any ties with the next column, subsequent ties to with the next-next column etc.

Note, first, that the documentation for the key argument does the following:

key specifies a function of one argument that is used to extract a comparison key from each list element

So your itemgetter idea isn't quite right, since you want to move through the list only when a comparison is equal.

However, things are actually much easier than you think. Check out the Python docs (See also this SO question.):

Sequence types also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length. (For full details see Comparisons in the language reference.)

Which, I think, is exactly what you want if you just make sure that each row is an equal-length sequence (list or tuple).

(Aha, I just read the comment regarding the die-roll function producing the keys. Confusing -- not sure if the above is helpful in that case, but I'm not sure what you are asking actually makes sense...)

Community
  • 1
  • 1
Andrew Jaffe
  • 26,554
  • 4
  • 50
  • 59
  • My thing is: I have the rows list. I actually create the column values dynamically. In this sorted kicks. And that's probably because as I found in a R.Hettingers answer, sorted just maps rows to the key function decorating them, calls sort() method on decorated rows, and then undecorates and returns the rows. Sorry for not being clear from the beginning. – Dim Jul 22 '12 at 11:54