Answer To: Objectives · Demonstrate use of map and filter commands · Practice list comprehensions and lambda...
Sampad Swarup answered on Nov 03 2021
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. List, Mapping, Filtering, and Reducing Comprehensions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def compute(x): #maping logic\n",
" if type(x) == type(''): # checking type string\n",
" return x[::-1] # returning the reversed string\n",
" else:\n",
" if type(x) == type(1.0) or type(x) == type(1): # checking for int and float values\n",
" return x**2\n",
" else:\n",
" return None\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"B:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['puc', '123', 144, None, None]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def map_compute(my_list):\n",
" return list(map(compute, my_list)) # maping the list. Then listing and returning the mapped value.\n",
"\n",
"map_compute(['cup', '321', 12, ['x'], True])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"C:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['puc', '123', 4.0, None, 16]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def filter_list(x): #filtering logic\n",
" if type(x) == type(1): #checking type int\n",
" if x < 5:\n",
" return False\n",
" else:\n",
" return True\n",
" else:\n",
" return True\n",
"\n",
"def filter_compute(my_list):\n",
" my_list = map_compute(my_list) # using the map_compute fuction\n",
" my_list = filter(filter_list,my_list) # filtering the list using filtering logic\n",
" return list(my_list)\n",
"\n",
"filter_compute(['cup', '321', 2, 2.0, ['x'], 4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"D:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(['puc', '123', 144, 4, None, None], ['puc', '123', 144, None, None])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def lambda_compute(my_list):\n",
" #using lambda to raplicate the map operation\n",
" my_list_map = list(map(lambda x : x[::-1] if type(x) == type('') else (x**2 if type(x) == type(1.0) or type(x) == type(1) else None), my_list))\n",
" #using lambda to raplicate the filter operation\n",
" #here I'm filtering the my_list_map for better demonstration\n",
" my_list_filter = list(filter(lambda x : False if type(x) == type(1) and x < 5 else True, my_list_map))\n",
" return my_list_map, my_list_filter\n",
"\n",
"lambda_compute(['cup', '321', 12, 2, ['x'], True])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Generating Random Data and Filtering That Data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shape of ar::(1000,)\n"
]
}
],
"source": [
"import numpy as np\n",
"np.random.seed(25)\n",
"\n",
"ar = np.random.randn(1000)\n",
"ar = ar * 100 \n",
"ar = ar.astype('int8')\n",
"\n",
"print(f'Shape of ar::{ar.shape}')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"After reshape function shape of ar::(200, 5)\n"
]
}
],
"source": [
"def reshape(my_array):\n",
" my_array = np.array(my_array) #making sure that parsed array is a numpy array\n",
" if len(my_array) == 1000:\n",
" return my_array.reshape(200, 5)#reshapeing the array\n",
" else:\n",
" print('Enter array of len 1000.')\n",
"ar = reshape(ar)\n",
"print(f'After reshape function shape of ar::{ar.shape}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"B:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([102,\n",
" 86,\n",
" 123,\n",
" 120,\n",
" 99,\n",
" 59,\n",
" 73,\n",
" 63,\n",
" 68,\n",
" 10,\n",
" 117,\n",
" 118,\n",
" 94,\n",
" 57,\n",
" 112,\n",
" 49,\n",
" 119,\n",
" 104,\n",
" 121,\n",
" 19,\n",
" 102,\n",
" 56,\n",
" 122,\n",
" 17,\n",
" 52,\n",
" 20,\n",
" 54,\n",
" 101,\n",
" 43,\n",
" -16,\n",
" 112,\n",
" 93,\n",
" 64,\n",
" 59,\n",
" 62,\n",
" 92,\n",
" 63,\n",
" 101,\n",
" 90,\n",
" 120,\n",
" 111,\n",
" 97,\n",
" 64,\n",
" 40,\n",
" 98,\n",
" 106,\n",
" 96,\n",
" 80,\n",
" 71,\n",
" 59,\n",
" 73,\n",
" 24,\n",
" 57,\n",
" 68,\n",
" 65,\n",
" 120,\n",
" 117,\n",
" 94,\n",
" 102,\n",
" 28,\n",
" 2,\n",
" 93,\n",
" 66,\n",
" 58,\n",
" 111,\n",
" 83,\n",
" 126,\n",
" 17,\n",
" 111,\n",
" 111,\n",
" 46,\n",
" 70,\n",
" 121,\n",
" 124,\n",
" 89,\n",
" 98,\n",
" 43,\n",
" 98,\n",
" 106,\n",
" 94,\n",
" 100,\n",
" 78,\n",
" 78,\n",
" 45,\n",
" ...