3 ms·
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
by d0mine 18y ago
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]
- MHollender 18y agoUm, exactly what words in "#LINQ in C# is cool #Python doesn't quite need it as it has generatior expressions, sum(), len(), sorted() and itertools.groupby() #so let's just wrap a nice SQL-like interface over them in order to make them more familiar" you have trouble understanding? I'm trying to wrap a unified and familiar SQL-like, LINQ-like interface over gen. expressions, sorted(), len() and suchlike and you remark that gen. expressions, sorted() and suchlike exist. Wow!