Skip to content

Commit f4d6512

Browse files
authored
Merge pull request rougier#34 from ibah/master
generalizing the solution to the 'extract rows with unequal values'
2 parents 27179d3 + 946647c commit f4d6512

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

100 Numpy exercises.ipynb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,9 +2157,13 @@
21572157
"# Author: Robert Kern\n",
21582158
"\n",
21592159
"Z = np.random.randint(0,5,(10,3))\n",
2160-
"E = np.logical_and.reduce(Z[:,1:] == Z[:,:-1], axis=1)\n",
2161-
"U = Z[~E]\n",
21622160
"print(Z)\n",
2161+
"# solution for arrays of all dtypes (including string arrays and record arrays)\n",
2162+
"E = np.all(Z[:,1:] == Z[:,:-1], axis=1)\n",
2163+
"U = Z[~E]\n",
2164+
"print(U)\n",
2165+
"# soluiton for numerical arrays only, will work for any number of columns in Z\n",
2166+
"U = Z[Z.max(axis=1) != Z.min(axis=1),:]\n",
21632167
"print(U)"
21642168
]
21652169
},

100 Numpy exercises.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,9 +1119,13 @@ print(rows)
11191119
# Author: Robert Kern
11201120

11211121
Z = np.random.randint(0,5,(10,3))
1122-
E = np.logical_and.reduce(Z[:,1:] == Z[:,:-1], axis=1)
1123-
U = Z[~E]
11241122
print(Z)
1123+
# solution for arrays of all dtypes (including string arrays and record arrays)
1124+
E = np.all(Z[:,1:] == Z[:,:-1], axis=1)
1125+
U = Z[~E]
1126+
print(U)
1127+
# soluiton for numerical arrays only, will work for any number of columns in Z
1128+
U = Z[Z.max(axis=1) != Z.min(axis=1),:]
11251129
print(U)
11261130
```
11271131

0 commit comments

Comments
 (0)