forked from rogerwcpt/python-linq-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinq-grouping.py
70 lines (47 loc) · 1.92 KB
/
linq-grouping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import shared
from itertools import groupby
from types import SimpleNamespace
def linq40():
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
# First create a record of numbers and their modulus of 5
number_remainders = map(lambda n: SimpleNamespace(Number=n,
Remainder=n % 5),
numbers)
number_remainders = (SimpleNamespace(Number=n, Remainder=n % 5) for n in numbers)
# Group By only works on sorted lists, so sort by both fields
sorted_by_reminder = sorted(number_remainders, key=lambda x: (x.Remainder, x.Number))
remainder_groups = groupby(sorted_by_reminder, key=lambda nr: nr.Remainder)
for key, items in remainder_groups:
print("Numbers with a remainder of %d when divided by 5:" % key)
for item in items:
print(item.Number)
def linq41():
words = ["blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese"]
first_letter_words = map(lambda w: SimpleNamespace(Letter=w[0],
Word=w),
words)
# Group By only works on sorted lists, so sort by both fields
sorted_letter_words = sorted(first_letter_words, key=lambda x: (x.Word, x.Letter))
letter_groups = groupby(sorted_letter_words, key=lambda nr: nr.Letter)
for key, items in letter_groups:
print("Words that start with the letter '%s':" % key)
for item in items:
print(item.Word)
def linq42():
products = shared.getProductList()
sorted_by_category = sorted(products, key=lambda p: p.Category)
order_groups = groupby(sorted_by_category, key=lambda p: p.Category)
for key, items in order_groups:
print("Products in the category '%s':" % key)
print(list(items))
def linq43():
pass
def linq44():
pass
def linq45():
pass
linq40()
# linq41()
# linq42()
# linq43()
# linq44()