News for hackers. Stuff that matters.
Re:Ask HN: is it a good idea in Python? If yes, how to make ... by d0mine on Tuesday February 10, 2009 @01:47PM (#475657)
Your examples rewritten in a different style:

  table = ((8, 2, 9), (4, 5, 6), (2, 1, 9))
  my_sum = sum(row[0] for row in table if row[2] == 9) 
  # assert my_sum == 10

  my_lst = [row[0] for row in table if row[2] == 9]
  # all row[2] == 9 therefore OrderBy is a no-op in your example 
  # assert my_lst == [8, 2]
Modified example:

  myLst = From(table) \
        .Where(lambda column: column[2]==9) \
        .OrderBy(lambda column: column[1])  \
        .Select(lambda column: column[0])

  my_lst = sorted((row[1], row[0]) for row in table if row[2] == 9)
  my_lst = map(itemgetter(1), my_lst)
  # assert my_lst == [2, 8]
Or to preserve order in case some row[1] are equal:

  my_lst = [row for row in table if row[2] == 9]
  my_lst.sort(key=itemgetter(1)) # requires "lambda"
  my_lst = [row[0] for row in my_lst]
  # assert my_lst == [2, 8]

There are no comments on this comment yet.