@@ -62,15 +62,15 @@ The samples below mirrors the C# LINQ samples layout with the names of the top-l
62
62
|| ` ThenBy ` | ` sequence.sort(key=lambda (key1, key2)) ` * or* <br /> ` sorted(sequence, key=lambda (key1, key)) ` ||
63
63
|| ` ThenByDescending ` | ` sequence.sort(key=lambda (key1, -key2)) ` * or* <br /> ` sorted(sequence, key=lambda (key1, -key2)) ` <br /> * or use a 2 pass sort, starting with least significant* <br /> ` ordered = sorted(unordered, key=lambda (key2)) ` <br /> ` ordered = sorted(ordered, key=lambda (key1)) ` |
64
64
|| ` Reverse ` | ` sequence.reverse() ` * or* ` reversed(sequence) ` ||
65
- | ** Grouping** | ` GroupBy ` || ` from itertools import groupby ` | Only works on sorted lists |
66
- | ** Sets** | ` Distinct ` | ` toSet ` ||
65
+ |** Grouping** |` GroupBy ` |` groupby ` |` from itertools import groupby ` < br />Grouping works on sorted sequences
66
+ | ** Sets** | ` Distinct ` | ` set ` ||
67
67
|| ` Union ` | ` union ` ||
68
68
|| ` Interect ` | ` intersection ` ||
69
69
|| ` Except ` | ` difference ` ||
70
70
| ** Conversion** | ` ToArray ` | ` toList ` ||
71
71
|| ` ToList ` | ` toList ` ||
72
- || ` ToDictionary ` || Custom [ toMap ] ( #dart-utils-added-2 ) utility added |
73
- || ` OfType ` || Custom [ ofType ] ( #dart-utils-added-2 ) utility added |
72
+ || ` ToDictionary ` | ` dict ` | Often used in conjuction with ` zip ` |
73
+ || ` OfType ` | ` 'ilter ` using ` isinstance ` as predicate |
74
74
| ** Element** | ` First ` | ` first ` ||
75
75
|| ` First(lambda) ` | ` firstWhere(lambda) ` ||
76
76
|| ` FirstOrDefault ` | ` firstWhere(lambda, default) ` |
@@ -1923,16 +1923,14 @@ public void Linq46()
1923
1923
uniqueFactors .ForEach (Console .WriteLine );
1924
1924
}
1925
1925
```
1926
- ``` dart
1927
- //dart
1928
- linq46(){
1929
- var factorsOf300 = [ 2, 2, 3, 5, 5 ];
1930
-
1931
- var uniqueFactors = factorsOf300.toSet();
1926
+ ``` python
1927
+ # python
1928
+ def linq46 ():
1929
+ factors_of300 = [2 , 2 , 3 , 5 , 5 ]
1932
1930
1933
- print("Prime factors of 300:");
1934
- uniqueFactors.forEach(print);
1935
- }
1931
+ unique_factors = set (factors_of300)
1932
+
1933
+ shared.printN(unique_factors)
1936
1934
```
1937
1935
#### Output
1938
1936
@@ -1956,18 +1954,15 @@ public void Linq47()
1956
1954
categoryNames .ForEach (Console .WriteLine );
1957
1955
}
1958
1956
```
1959
- ``` dart
1960
- //dart
1961
- linq47(){
1962
- var products = productsList();
1963
-
1964
- var categoryNames = products
1965
- .map((p) => p.category)
1966
- .toSet();
1967
-
1968
- print("Category names:");
1969
- categoryNames.forEach(print);
1970
- }
1957
+ ``` python
1958
+ # python
1959
+ def linq47 ():
1960
+ products = shared.getProductList()
1961
+
1962
+ category_names = set (map (lambda p : p.Category, products))
1963
+
1964
+ print (" Category names:" )
1965
+ shared.printS(category_names)
1971
1966
```
1972
1967
#### Output
1973
1968
@@ -1995,17 +1990,16 @@ public void Linq48()
1995
1990
uniqueNumbers .ForEach (Console .WriteLine );
1996
1991
}
1997
1992
```
1998
- ``` dart
1999
- //dart
2000
- linq48(){
2001
- var numbersA = [ 0, 2, 4, 5, 6, 8, 9 ];
2002
- var numbersB = [ 1, 3, 5, 7, 8 ];
2003
-
2004
- var uniqueNumbers = numbersA.toSet().union(numbersB.toSet());
2005
-
2006
- print("Unique numbers from both arrays:");
2007
- uniqueNumbers.forEach(print);
2008
- }
1993
+ ``` python
1994
+ # python
1995
+ def linq48 ():
1996
+ numbers_a = [0 , 2 , 4 , 5 , 6 , 8 , 9 ]
1997
+ numbers_b = [1 , 3 , 5 , 7 , 8 ]
1998
+
1999
+ unique_numbers = set (numbers_a + numbers_b)
2000
+
2001
+ print (" Unique numbers from both arrays:" )
2002
+ shared.printN(unique_numbers)
2009
2003
```
2010
2004
#### Output
2011
2005
@@ -2040,22 +2034,19 @@ public void Linq49()
2040
2034
uniqueFirstChars .ForEach (Console .WriteLine );
2041
2035
}
2042
2036
```
2043
- ``` dart
2044
- //dart
2045
- linq49(){
2046
- var products = productsList();
2047
- var customers = customersList();
2048
-
2049
- var productFirstChars = products
2050
- .map((p) => p.productName[0]);
2051
- var customerFirstChars = customers
2052
- .map((c) => c.companyName[0]);
2053
-
2054
- var uniqueFirstChars = productFirstChars.toSet().union(customerFirstChars.toSet());
2055
-
2056
- print("Unique first letters from Product names and Customer names:");
2057
- uniqueFirstChars.forEach(print);
2058
- }
2037
+ ``` python
2038
+ # python
2039
+ def linq49 ():
2040
+ products = shared.getProductList()
2041
+ customers = shared.getCustomerList()
2042
+
2043
+ product_first_chars = map (lambda p : p.ProductName[0 ], products)
2044
+ customer_first_chars = map (lambda c : c.CompanyName[0 ], customers)
2045
+
2046
+ unique_first_chars = set (product_first_chars).union(set (customer_first_chars))
2047
+
2048
+ print (" Unique first letters from Product names and Customer names:" )
2049
+ shared.printS(unique_first_chars)
2059
2050
```
2060
2051
#### Output
2061
2052
@@ -2099,17 +2090,16 @@ public void Linq50()
2099
2090
commonNumbers .ForEach (Console .WriteLine );
2100
2091
}
2101
2092
```
2102
- ``` dart
2103
- //dart
2104
- linq50(){
2105
- var numbersA = [ 0, 2, 4, 5, 6, 8, 9 ];
2106
- var numbersB = [ 1, 3, 5, 7, 8 ];
2107
-
2108
- var commonNumbers = numbersA.toSet().intersection(numbersB.toSet());
2109
-
2110
- print("Common numbers shared by both arrays:");
2111
- commonNumbers.forEach(print);
2112
- }
2093
+ ``` python
2094
+ # python
2095
+ def linq50 ():
2096
+ numbers_a = [0 , 2 , 4 , 5 , 6 , 8 , 9 ]
2097
+ numbers_b = [1 , 3 , 5 , 7 , 8 ]
2098
+
2099
+ common_numbers = set (numbers_a).intersection((set (numbers_b)))
2100
+
2101
+ print (" Common numbers shared by both arrays:" )
2102
+ shared.printN(common_numbers)
2113
2103
```
2114
2104
#### Output
2115
2105
@@ -2130,28 +2120,25 @@ public void Linq51()
2130
2120
var customerFirstChars = customers
2131
2121
.Select (c => c .CompanyName [0 ]);
2132
2122
2133
- var commonFirstChars = productFirstChars .Intersect (customerFirstChars );
2123
+ var commonFirstChars = productFirstChars .Intersect (customerFirstChars )
2134
2124
2135
2125
Console .WriteLine (" Common first letters from Product names and Customer names:" );
2136
2126
commonFirstChars .ForEach (Console .WriteLine );
2137
2127
}
2138
2128
```
2139
- ``` dart
2140
- //dart
2141
- linq51(){
2142
- var products = productsList();
2143
- var customers = customersList();
2144
-
2145
- var productFirstChars = products
2146
- .map((p) => p.productName[0]);
2147
- var customerFirstChars = customers
2148
- .map((c) => c.companyName[0]);
2149
-
2150
- var commonFirstChars = productFirstChars.toSet().intersection(customerFirstChars.toSet());
2151
-
2152
- print("Common first letters from Product names and Customer names:");
2153
- commonFirstChars.forEach(print);
2154
- }
2129
+ ``` python
2130
+ # python
2131
+ def linq51 ():
2132
+ products = shared.getProductList()
2133
+ customers = shared.getCustomerList()
2134
+
2135
+ product_first_chars = map (lambda p : p.ProductName[0 ], products)
2136
+ customer_first_chars = map (lambda c : c.CompanyName[0 ], customers)
2137
+
2138
+ unique_first_chars = set (product_first_chars).intersection(set (customer_first_chars))
2139
+
2140
+ print (" Common first letters from Product names and Customer names:" )
2141
+ shared.printS(unique_first_chars)
2155
2142
```
2156
2143
#### Output
2157
2144
@@ -2190,17 +2177,16 @@ public void Linq52()
2190
2177
aOnlyNumbers .ForEach (Console .WriteLine );
2191
2178
}
2192
2179
```
2193
- ``` dart
2194
- //dart
2195
- linq52(){
2196
- var numbersA = [ 0, 2, 4, 5, 6, 8, 9 ];
2197
- var numbersB = [ 1, 3, 5, 7, 8 ];
2198
-
2199
- var aOnlyNumbers = numbersA.toSet().difference(numbersB.toSet());
2200
-
2201
- print("Numbers in first array but not second array:");
2202
- aOnlyNumbers.forEach(print);
2203
- }
2180
+ ``` python
2181
+ # python
2182
+ def linq52 ():
2183
+ numbers_a = [0 , 2 , 4 , 5 , 6 , 8 , 9 ]
2184
+ numbers_b = [1 , 3 , 5 , 7 , 8 ]
2185
+
2186
+ a_only_numbers = set (numbers_a).difference((set (numbers_b)))
2187
+
2188
+ print (" Numbers in first array but not second array:" )
2189
+ shared.printN(a_only_numbers)
2204
2190
```
2205
2191
#### Output
2206
2192
@@ -2230,22 +2216,19 @@ public void Linq53()
2230
2216
productOnlyFirstChars .ForEach (Console .WriteLine );
2231
2217
}
2232
2218
```
2233
- ``` dart
2234
- //dart
2235
- linq53(){
2236
- var products = productsList();
2237
- var customers = customersList();
2238
-
2239
- var productFirstChars = products
2240
- .map((p) => p.productName[0]);
2241
- var customerFirstChars = customers
2242
- .map((c) => c.companyName[0]);
2243
-
2244
- var productOnlyFirstChars = productFirstChars.toSet().difference(customerFirstChars.toSet());
2245
-
2246
- print("First letters from Product names, but not from Customer names:");
2247
- productOnlyFirstChars.forEach(print);
2248
- }
2219
+ ``` python
2220
+ # python
2221
+ def linq53 ():
2222
+ products = shared.getProductList()
2223
+ customers = shared.getCustomerList()
2224
+
2225
+ product_first_chars = map (lambda p : p.ProductName[0 ], products)
2226
+ customer_first_chars = map (lambda c : c.CompanyName[0 ], customers)
2227
+
2228
+ unique_first_chars = set (product_first_chars).difference(set (customer_first_chars))
2229
+
2230
+ print (" First letters from Product names, but not from Customer names:" )
2231
+ shared.printS(unique_first_chars)
2249
2232
```
2250
2233
#### Output
2251
2234
@@ -2289,20 +2272,21 @@ public void Linq54()
2289
2272
}
2290
2273
}
2291
2274
```
2292
- ``` dart
2293
- //dart
2294
- linq54(){
2295
- var doubles = [ 1.7, 2.3, 1.9, 4.1, 2.9 ];
2275
+ ``` python
2276
+ # python
2277
+ def linq54 ():
2278
+ doubles = [ 1.7 , 2.3 , 1.9 , 4.1 , 2.9 ]
2296
2279
2297
- var sortedDoubles = order (doubles).reversed;
2280
+ sorted_doubles = sorted (doubles, reverse = True )
2298
2281
2299
- var doublesArray = sortedDoubles.toList(growable:false);
2282
+ soubles_array = list (sorted_doubles)
2300
2283
2301
- print("Every other double from highest to lowest:");
2302
- for (int d = 0; d < doublesArray.length; d += 2){
2303
- print(doublesArray[d]);
2304
- }
2305
- }
2284
+ print (" Every other double from highest to lowest:" )
2285
+ # shared.printN(soublesArray)
2286
+ d = 0
2287
+ while d < len (soubles_array):
2288
+ print (soubles_array[d])
2289
+ d += 2
2306
2290
```
2307
2291
#### Output
2308
2292
@@ -2326,18 +2310,17 @@ public void Linq55()
2326
2310
wordList .ForEach (Console .WriteLine );
2327
2311
}
2328
2312
```
2329
- ``` dart
2330
- //dart
2331
- linq55(){
2332
- var words = [ "cherry", "apple", "blueberry" ];
2333
-
2334
- var sortedWords = orderBy(words);
2335
-
2336
- var wordList = sortedWords.toList();
2337
-
2338
- print("The sorted word list:");
2339
- wordList.forEach(print);
2340
- }
2313
+ ``` python
2314
+ # python
2315
+ def linq55 ():
2316
+ words = [" cherry" , " apple" , " blueberry" ]
2317
+
2318
+ sorted_words = sorted (words)
2319
+
2320
+ word_list = list (sorted_words)
2321
+
2322
+ print (" The sorted word list:" )
2323
+ shared.printN(word_list)
2341
2324
```
2342
2325
#### Output
2343
2326
@@ -2363,17 +2346,18 @@ public void Linq56()
2363
2346
Console .WriteLine (" Bob's score: {0}" , scoreRecordsDict [" Bob" ]);
2364
2347
}
2365
2348
```
2366
- ``` dart
2367
- //dart
2368
- linq56(){
2369
- var scoreRecords = [{ 'Name': "Alice", 'Score': 50},
2370
- { 'Name': "Bob" , 'Score': 40},
2371
- { 'Name': "Cathy", 'Score': 45}];
2372
-
2373
- var scoreRecordsDict = toMap(scoreRecords, (sr) => sr['Name']);
2374
-
2375
- print("Bob's score: ${scoreRecordsDict['Bob']}");
2376
- }
2349
+ ``` python
2350
+ # python
2351
+ def linq56 ():
2352
+ score_records = [{' Name' : " Alice" , ' Score' : 50 },
2353
+ {' Name' : " Bob" , ' Score' : 40 },
2354
+ {' Name' : " Cathy" , ' Score' : 45 }]
2355
+
2356
+ index = map (lambda s : s[" Name" ], score_records)
2357
+
2358
+ score_records_dict = dict (zip (index, score_records))
2359
+
2360
+ print (" Bob's score: %s " % score_records_dict[" Bob" ])
2377
2361
```
2378
2362
#### Output
2379
2363
@@ -2392,16 +2376,15 @@ public void Linq57()
2392
2376
doubles .ForEach (Console .WriteLine );
2393
2377
}
2394
2378
```
2395
- ``` dart
2396
- //dart
2397
- linq57(){
2398
- var numbers = [ null, 1.0, "two", 3, "four", 5, "six", 7.0 ];
2399
-
2400
- var doubles = ofType(numbers, double);
2401
-
2402
- print("Numbers stored as doubles:");
2403
- doubles.forEach(print);
2404
- }
2379
+ ``` python
2380
+ # python
2381
+ def linq57 ():
2382
+ numbers = [None , 1.0 , " two" , 3 , " four" , 5 , " six" , 7.0 ]
2383
+
2384
+ floats = filter (lambda n : isinstance (n, float ), numbers)
2385
+
2386
+ print (" Numbers stored as floats:" )
2387
+ shared.printN(floats)
2405
2388
```
2406
2389
#### Output
2407
2390
0 commit comments