Adding an alternative solution to the 'extract rows with unequal values' exercise

This commit is contained in:
ibah 2016-12-01 13:42:52 +01:00
parent 37e7fe2563
commit 946647cb40
2 changed files with 12 additions and 0 deletions

View File

@ -2149,6 +2149,12 @@
"# Author: Robert Kern\n",
"\n",
"Z = np.random.randint(0,5,(10,3))\n",
"print(Z)\n",
"# solution for arrays of all dtypes (including string arrays and record arrays)\n",
"E = np.all(Z[:,1:] == Z[:,:-1], axis=1)\n",
"U = Z[~E]\n",
"print(U)\n",
"# soluiton for numerical arrays only, will work for any number of columns in Z\n",
"U = Z[Z.max(axis=1) != Z.min(axis=1),:]\n",
"print(U)"
]

View File

@ -1108,6 +1108,12 @@ print(rows)
# Author: Robert Kern
Z = np.random.randint(0,5,(10,3))
print(Z)
# solution for arrays of all dtypes (including string arrays and record arrays)
E = np.all(Z[:,1:] == Z[:,:-1], axis=1)
U = Z[~E]
print(U)
# soluiton for numerical arrays only, will work for any number of columns in Z
U = Z[Z.max(axis=1) != Z.min(axis=1),:]
print(U)
```