@@ -79,12 +79,12 @@ The samples below mirrors the C# LINQ samples layout with the names of the top-l
79
79
|| ` Enumerable.Repeat ` | ` [x] * n ` * or* <br /> ` itertools.repeat(x, n) ` ||
80
80
| ** Quantifiers** | ` Any ` | ` any ` ||
81
81
|| ` All ` | ` all ` ||
82
- | ** Aggregate** | ` Count ` | ` length ` ||
83
- || ` Count(lamda) ` | ` where(lambda).length ` ||
84
- || ` Sum ` || Custom [ sum] ( #dart-utils-added-4 ) utility added |
85
- || ` Min ` || Custom [ min] ( #dart-utils-added-4 ) utility added |
86
- || ` Max ` || Custom [ max] ( #dart-utils-added-4 ) utility added |
87
- || ` Avg ` || Custom [ avg ] ( #dart-utils-added-4 ) utility added |
82
+ | ** Aggregate** | ` Count ` | ` length ` * or * < br /> ` sum(iterator) ` ||
83
+ || ` Count(lamda) ` | ` sum(iterator) ` ||
84
+ || ` Sum ` | sum| |
85
+ || ` Min ` | min| |
86
+ || ` Max ` | max| |
87
+ || ` Avg ` |||
88
88
|| ` Sum(lambda) ` || Custom [ sum] ( #dart-utils-added-4 ) utility added|
89
89
|| ` Min(lambda) ` || Custom [ min] ( #dart-utils-added-4 ) utility added|
90
90
|| ` Max(lambda) ` || Custom [ max] ( #dart-utils-added-4 ) utility added|
@@ -2718,15 +2718,15 @@ public void Linq73()
2718
2718
Console .WriteLine ($" There are {uniqueFactors } unique prime factors of 300." );
2719
2719
}
2720
2720
```
2721
- ``` dart
2722
- //dart
2723
- linq73(){
2724
- var factorsOf300 = [ 2, 2, 3, 5, 5 ];
2725
-
2726
- int uniqueFactors = factorsOf300.toSet().length;
2727
-
2728
- print("There are $uniqueFactors unique factors of 300.");
2729
- }
2721
+ ``` python
2722
+ # python
2723
+ def linq73 ():
2724
+ factors_of_300 = [2 , 2 , 3 , 5 , 5 ]
2725
+
2726
+ unique_factors = len ( set (factors_of_300))
2727
+
2728
+ print (" There are %d unique factors of 300." % unique_factors)
2729
+
2730
2730
```
2731
2731
#### Output
2732
2732
@@ -2744,15 +2744,14 @@ public void Linq74()
2744
2744
Console .WriteLine ($" There are {oddNumbers } odd numbers in the list." );
2745
2745
}
2746
2746
```
2747
- ``` dart
2748
- //dart
2749
- linq74(){
2750
- var numbers = [ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ];
2751
-
2752
- int oddNumbers = numbers.where((n) => n % 2 == 1).length;
2753
-
2754
- print("There are $oddNumbers odd numbers in the list.");
2755
- }
2747
+ ``` python
2748
+ # python
2749
+ def linq74 ():
2750
+ numbers = [5 , 4 , 1 , 3 , 9 , 8 , 6 , 7 , 2 , 0 ]
2751
+
2752
+ odd_numbers = sum (n % 2 == 1 for n in numbers)
2753
+
2754
+ print (" There are %d odd numbers in the list." % odd_numbers)
2756
2755
```
2757
2756
#### Output
2758
2757
@@ -2771,16 +2770,14 @@ public void Linq76()
2771
2770
ObjectDumper .Write (orderCounts );
2772
2771
}
2773
2772
```
2774
- ``` dart
2775
- //dart
2776
- linq76(){
2777
- var customers = customersList();
2778
-
2779
- var orderCounts = customers
2780
- .map((c) => { 'CustomerId': c.customerId, 'OrderCount': c.orders.length });
2773
+ ``` python
2774
+ # python
2775
+ def linq75 ():
2776
+ customers = shared.getCustomerList()
2781
2777
2782
- orderCounts.forEach(print);
2783
- }
2778
+ order_counts = map (lambda cust : SimpleNamespace(CustomerID = cust.CustomerID, OrderCount = len (cust.Orders)), customers)
2779
+
2780
+ shared.print_namespace(order_counts)
2784
2781
```
2785
2782
#### Output
2786
2783
@@ -2807,15 +2804,18 @@ public void Linq77()
2807
2804
ObjectDumper .Write (categoryCounts );
2808
2805
}
2809
2806
```
2810
- ``` dart
2811
- //dart
2807
+ ``` python
2808
+ # python
2812
2809
linq77(){
2813
- var products = productsList();
2814
-
2815
- var categoryCounts = group(products, by:(p) => p.category)
2816
- .map((g) => { 'Category': g.key, 'ProductCount': g.length });
2810
+ def linq77():
2811
+ products = shared.getProductList()
2812
+
2813
+ sorted_by_category = sorted (products, key = lambda p : p.Category)
2814
+ grouped_by_category = groupby(sorted_by_category, key = lambda p : p.Category)
2817
2815
2818
- categoryCounts.forEach(print);
2816
+ category_counts = map (lambda g : SimpleNamespace(Category = g[0 ], ProductCount = len (list (g[1 ]))), grouped_by_category)
2817
+
2818
+ shared.print_namespace(category_counts)
2819
2819
}
2820
2820
```
2821
2821
#### Output
@@ -2841,15 +2841,14 @@ public void Linq78()
2841
2841
Console .WriteLine ($" The sum of the numbers is {numSum }." );
2842
2842
}
2843
2843
```
2844
- ``` dart
2845
- //dart
2846
- linq78(){
2847
- var numbers = [ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ];
2848
-
2849
- var numSum = sum(numbers);
2850
-
2851
- print("The sum of the numbers is $numSum.");
2852
- }
2844
+ ``` python
2845
+ # python
2846
+ def linq78 ():
2847
+ numbers = [5 , 4 , 1 , 3 , 9 , 8 , 6 , 7 , 2 , 0 ]
2848
+
2849
+ num_sum = sum (numbers)
2850
+
2851
+ print (" The sum of the numbers is %d ." % num_sum)
2853
2852
```
2854
2853
#### Output
2855
2854
@@ -2867,15 +2866,14 @@ public void Linq79()
2867
2866
Console .WriteLine ($" There are a total of {totalChars } characters in these words." );
2868
2867
}
2869
2868
```
2870
- ``` dart
2871
- //dart
2872
- linq79(){
2873
- var words = [ "cherry", "apple", "blueberry" ];
2874
-
2875
- var totalChars = sum(words, (w) => w.length);
2876
-
2877
- print("There are a total of $totalChars characters in these words.");
2878
- }
2869
+ ``` python
2870
+ # python
2871
+ def linq79 ():
2872
+ words = [" cherry" , " apple" , " blueberry" ]
2873
+
2874
+ total_chars = sum (len (w) for w in words)
2875
+
2876
+ print (" There are a total of %d characters in these words." % total_chars)
2879
2877
```
2880
2878
#### Output
2881
2879
@@ -2895,16 +2893,17 @@ public void Linq80()
2895
2893
ObjectDumper .Write (categories );
2896
2894
}
2897
2895
```
2898
- ``` dart
2899
- //dart
2900
- linq80(){
2901
- var products = productsList();
2902
-
2903
- var categories = group(products, by:(p) => p.category)
2904
- .map((g) => { 'Category': g.key, 'TotalUnitsInStock': sum(g.values, (p) => p.unitsInStock) });
2905
-
2906
- categories.forEach(print);
2907
- }
2896
+ ``` python
2897
+ # python
2898
+ def linq80 ():
2899
+ products = shared.getProductList()
2900
+
2901
+ sorted_by_category = sorted (products, key = lambda p : p.Category)
2902
+ grouped_by_category = groupby(sorted_by_category, key = lambda p : p.Category)
2903
+
2904
+ category_counts = map (lambda g : SimpleNamespace(Category = g[0 ], TotalUnitsInStock = sum (p.UnitsInStock for p in g[1 ])), grouped_by_category)
2905
+
2906
+ shared.print_namespace(category_counts)
2908
2907
```
2909
2908
#### Output
2910
2909
@@ -2929,15 +2928,14 @@ public void Linq81()
2929
2928
Console .WriteLine ($" The minimum number is {minNum }." );
2930
2929
}
2931
2930
```
2932
- ``` dart
2933
- //dart
2934
- linq81(){
2935
- var numbers = [ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ];
2936
-
2937
- int minNum = min(numbers);
2938
-
2939
- print("The minimum number is $minNum.");
2940
- }
2931
+ ``` python
2932
+ # python
2933
+ def linq81 ():
2934
+ numbers = [5 , 4 , 1 , 3 , 9 , 8 , 6 , 7 , 2 , 0 ]
2935
+
2936
+ min_num = min (numbers)
2937
+
2938
+ print (" The minimum number is %d " % min_num);
2941
2939
```
2942
2940
#### Output
2943
2941
0 commit comments