Skip to content

Instantly share code, notes, and snippets.

@martin-rdz
Created December 29, 2020 13:42
Show Gist options
  • Save martin-rdz/8828da6506b7063132b6aeeacd51fb15 to your computer and use it in GitHub Desktop.
Save martin-rdz/8828da6506b7063132b6aeeacd51fb15 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"a1 = np.array([1,2,3,5,7,9,11])\n",
"a2 = np.array([1,3,5,6,7,9,10,11])"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a1 - a2 {2} True\n",
"a2 - a1 {10, 6} True\n"
]
}
],
"source": [
"print('a1 - a2 ', set(a1) - set(a2), bool(set(a1) - set(a2)))\n",
"print('a2 - a1 ', set(a2) - set(a1), bool(set(a2) - set(a1)))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 3, 5, 7, 9, 11}"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set(a1) & set(a2)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"too many a1 (elements) [2]\n",
"too many a1 (indices) [1]\n",
"new a1 [ 1 3 5 7 9 11]\n",
"----------------------------------------------------\n",
"too many a2 (elements) [ 6 10]\n",
"too many a2 (indices) [3 6]\n",
"new a2 [ 1 3 5 7 9 11]\n"
]
}
],
"source": [
"too_many_a1 = np.sort(np.array(list(set(a1) - set(a2))))\n",
"print('too many a1 (elements) ', too_many_a1)\n",
"ind_a1 = np.searchsorted(a1, too_many_a1)\n",
"print('too many a1 (indices) ', ind_a1)\n",
"new_a1 = np.delete(a1, ind_a1)\n",
"print('new a1 ', new_a1)\n",
"\n",
"print('----------------------------------------------------')\n",
"too_many_a2 = np.sort(np.array(list(set(a2) - set(a1))))\n",
"print('too many a2 (elements) ', too_many_a2)\n",
"ind_a2 = np.searchsorted(a2, too_many_a2)\n",
"print('too many a2 (indices) ', ind_a2)\n",
"new_a2 = np.delete(a2, ind_a2)\n",
"print('new a2 ', new_a2)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/martin/.local/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.\n",
" \"\"\"Entry point for launching an IPython kernel.\n"
]
},
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-27-693973849e33>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma1\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0ma2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"assert np.all(a1 == a2)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"assert np.all(new_a1 == new_a2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment