Skip to content

Commit c702440

Browse files
committed
adde linq-conversin and linq-operators for python
1 parent 3d507a1 commit c702440

File tree

3 files changed

+285
-151
lines changed

3 files changed

+285
-151
lines changed

README.md

Lines changed: 134 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ The samples below mirrors the C# LINQ samples layout with the names of the top-l
6262
||`ThenBy`|`sequence.sort(key=lambda (key1, key2))` *or* <br/> `sorted(sequence, key=lambda (key1, key))`||
6363
||`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))` |
6464
||`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`||
6767
||`Union`|`union`||
6868
||`Interect`|`intersection`||
6969
||`Except`|`difference`||
7070
|**Conversion**|`ToArray`|`toList`||
7171
||`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|
7474
|**Element**|`First`|`first`||
7575
||`First(lambda)`|`firstWhere(lambda)`||
7676
||`FirstOrDefault`|`firstWhere(lambda, default)`|
@@ -1923,16 +1923,14 @@ public void Linq46()
19231923
uniqueFactors.ForEach(Console.WriteLine);
19241924
}
19251925
```
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]
19321930

1933-
print("Prime factors of 300:");
1934-
uniqueFactors.forEach(print);
1935-
}
1931+
unique_factors = set(factors_of300)
1932+
1933+
shared.printN(unique_factors)
19361934
```
19371935
#### Output
19381936

@@ -1956,18 +1954,15 @@ public void Linq47()
19561954
categoryNames.ForEach(Console.WriteLine);
19571955
}
19581956
```
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)
19711966
```
19721967
#### Output
19731968

@@ -1995,17 +1990,16 @@ public void Linq48()
19951990
uniqueNumbers.ForEach(Console.WriteLine);
19961991
}
19971992
```
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)
20092003
```
20102004
#### Output
20112005

@@ -2040,22 +2034,19 @@ public void Linq49()
20402034
uniqueFirstChars.ForEach(Console.WriteLine);
20412035
}
20422036
```
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)
20592050
```
20602051
#### Output
20612052

@@ -2099,17 +2090,16 @@ public void Linq50()
20992090
commonNumbers.ForEach(Console.WriteLine);
21002091
}
21012092
```
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)
21132103
```
21142104
#### Output
21152105

@@ -2130,28 +2120,25 @@ public void Linq51()
21302120
var customerFirstChars = customers
21312121
.Select(c => c.CompanyName[0]);
21322122

2133-
var commonFirstChars = productFirstChars.Intersect(customerFirstChars);
2123+
var commonFirstChars = productFirstChars.Intersect(customerFirstChars)
21342124

21352125
Console.WriteLine("Common first letters from Product names and Customer names:");
21362126
commonFirstChars.ForEach(Console.WriteLine);
21372127
}
21382128
```
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)
21552142
```
21562143
#### Output
21572144

@@ -2190,17 +2177,16 @@ public void Linq52()
21902177
aOnlyNumbers.ForEach(Console.WriteLine);
21912178
}
21922179
```
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)
22042190
```
22052191
#### Output
22062192

@@ -2230,22 +2216,19 @@ public void Linq53()
22302216
productOnlyFirstChars.ForEach(Console.WriteLine);
22312217
}
22322218
```
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)
22492232
```
22502233
#### Output
22512234

@@ -2289,20 +2272,21 @@ public void Linq54()
22892272
}
22902273
}
22912274
```
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 ]
22962279

2297-
var sortedDoubles = order(doubles).reversed;
2280+
sorted_doubles = sorted(doubles, reverse=True)
22982281

2299-
var doublesArray = sortedDoubles.toList(growable:false);
2282+
soubles_array = list(sorted_doubles)
23002283

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
23062290
```
23072291
#### Output
23082292

@@ -2326,18 +2310,17 @@ public void Linq55()
23262310
wordList.ForEach(Console.WriteLine);
23272311
}
23282312
```
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)
23412324
```
23422325
#### Output
23432326

@@ -2363,17 +2346,18 @@ public void Linq56()
23632346
Console.WriteLine("Bob's score: {0}", scoreRecordsDict["Bob"]);
23642347
}
23652348
```
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"])
23772361
```
23782362
#### Output
23792363

@@ -2392,16 +2376,15 @@ public void Linq57()
23922376
doubles.ForEach(Console.WriteLine);
23932377
}
23942378
```
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)
24052388
```
24062389
#### Output
24072390

0 commit comments

Comments
 (0)