Guest User

Untitled

a guest
May 28th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "List ,[-- 1D Datastructure for storing Objects], Operations"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "#### Creating Lists [] ####"
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": 1,
  20. "metadata": {
  21. "collapsed": true
  22. },
  23. "outputs": [],
  24. "source": [
  25. "#1a.\n",
  26. "list_num=[1,2,3]"
  27. ]
  28. },
  29. {
  30. "cell_type": "code",
  31. "execution_count": 2,
  32. "metadata": {},
  33. "outputs": [
  34. {
  35. "name": "stdout",
  36. "output_type": "stream",
  37. "text": [
  38. "[1, 2, 3]\n"
  39. ]
  40. }
  41. ],
  42. "source": [
  43. "print (list_num)"
  44. ]
  45. },
  46. {
  47. "cell_type": "code",
  48. "execution_count": 3,
  49. "metadata": {
  50. "collapsed": true
  51. },
  52. "outputs": [],
  53. "source": [
  54. "#1b.using range method\n",
  55. "list_range=list(range(0,5))"
  56. ]
  57. },
  58. {
  59. "cell_type": "code",
  60. "execution_count": 5,
  61. "metadata": {},
  62. "outputs": [
  63. {
  64. "name": "stdout",
  65. "output_type": "stream",
  66. "text": [
  67. "[0, 1, 2, 3, 4]\n"
  68. ]
  69. }
  70. ],
  71. "source": [
  72. "print(list_range)"
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": 6,
  78. "metadata": {
  79. "collapsed": true
  80. },
  81. "outputs": [],
  82. "source": [
  83. "#1c,using range method step argument\n",
  84. "list_range_step=list(range(0,5,2))"
  85. ]
  86. },
  87. {
  88. "cell_type": "code",
  89. "execution_count": 7,
  90. "metadata": {},
  91. "outputs": [
  92. {
  93. "data": {
  94. "text/plain": [
  95. "[0, 2, 4]"
  96. ]
  97. },
  98. "execution_count": 7,
  99. "metadata": {},
  100. "output_type": "execute_result"
  101. }
  102. ],
  103. "source": [
  104. "print(list_range_step)"
  105. ]
  106. },
  107. {
  108. "cell_type": "code",
  109. "execution_count": 9,
  110. "metadata": {},
  111. "outputs": [
  112. {
  113. "data": {
  114. "text/plain": [
  115. "[0, 1, 2, 3, 4]"
  116. ]
  117. },
  118. "execution_count": 9,
  119. "metadata": {},
  120. "output_type": "execute_result"
  121. }
  122. ],
  123. "source": [
  124. "#1d.List Comprehension, creating a list from another list by applying opertaions on elements of source List\n",
  125. "print(list_range) #src List"
  126. ]
  127. },
  128. {
  129. "cell_type": "code",
  130. "execution_count": 10,
  131. "metadata": {},
  132. "outputs": [
  133. {
  134. "name": "stdout",
  135. "output_type": "stream",
  136. "text": [
  137. "[0, 5, 10, 15, 20]\n"
  138. ]
  139. }
  140. ],
  141. "source": [
  142. "list_compre=[x*5 for x in list_range]\n",
  143. "print(list_compre)"
  144. ]
  145. },
  146. {
  147. "cell_type": "code",
  148. "execution_count": 16,
  149. "metadata": {},
  150. "outputs": [
  151. {
  152. "name": "stdout",
  153. "output_type": "stream",
  154. "text": [
  155. "[1, 2, 3, 4, 5]\n"
  156. ]
  157. }
  158. ],
  159. "source": [
  160. "#1e.Using map function, maipulate each item of a source list using a defined function and create a new list\n",
  161. "def addOne(x):\n",
  162. " return x+1;\n",
  163. "\n",
  164. "range_list_plus_one=list(map(addOne,list_range))\n",
  165. "print(range_list_plus_one)"
  166. ]
  167. },
  168. {
  169. "cell_type": "code",
  170. "execution_count": 19,
  171. "metadata": {},
  172. "outputs": [
  173. {
  174. "name": "stdout",
  175. "output_type": "stream",
  176. "text": [
  177. "[0, 2, 4]\n"
  178. ]
  179. }
  180. ],
  181. "source": [
  182. "#1f.using filter function,filter out elements and create a new list from source list\n",
  183. "def evenMembers(x):\n",
  184. " if x % 2==0:\n",
  185. " return True\n",
  186. " else:\n",
  187. " return False\n",
  188. " \n",
  189. "filtered_out_list=list(filter(evenMembers,list_range))\n",
  190. "print(filtered_out_list)"
  191. ]
  192. },
  193. {
  194. "cell_type": "code",
  195. "execution_count": 31,
  196. "metadata": {},
  197. "outputs": [
  198. {
  199. "name": "stdout",
  200. "output_type": "stream",
  201. "text": [
  202. "[0, 2, 4]\n"
  203. ]
  204. }
  205. ],
  206. "source": [
  207. "#1g.Lambda expression , anonymous function , used for brevity , you can implement the above evenMembers function inline \n",
  208. "filtered_out_list_using_lambda=list(filter(lambda x:x%2==0,list_range))\n",
  209. "print(filtered_out_list_using_lambda)"
  210. ]
  211. },
  212. {
  213. "cell_type": "markdown",
  214. "metadata": {},
  215. "source": [
  216. "#### Adding Element to List ####"
  217. ]
  218. },
  219. {
  220. "cell_type": "code",
  221. "execution_count": 32,
  222. "metadata": {},
  223. "outputs": [
  224. {
  225. "name": "stdout",
  226. "output_type": "stream",
  227. "text": [
  228. "[0, 2, 4, 9]\n"
  229. ]
  230. }
  231. ],
  232. "source": [
  233. "filtered_out_list_using_lambda.append(9)\n",
  234. "print(filtered_out_list_using_lambda)"
  235. ]
  236. },
  237. {
  238. "cell_type": "markdown",
  239. "metadata": {},
  240. "source": [
  241. "#### Updating element of a list ####"
  242. ]
  243. },
  244. {
  245. "cell_type": "code",
  246. "execution_count": 33,
  247. "metadata": {},
  248. "outputs": [
  249. {
  250. "name": "stdout",
  251. "output_type": "stream",
  252. "text": [
  253. "[0, 2, 4, 90]\n"
  254. ]
  255. }
  256. ],
  257. "source": [
  258. "filtered_out_list_using_lambda[3]=90 #3 is the index not element value\n",
  259. "print(filtered_out_list_using_lambda)"
  260. ]
  261. },
  262. {
  263. "cell_type": "markdown",
  264. "metadata": {},
  265. "source": [
  266. "#### Removing element from a List ####"
  267. ]
  268. },
  269. {
  270. "cell_type": "code",
  271. "execution_count": 34,
  272. "metadata": {},
  273. "outputs": [
  274. {
  275. "name": "stdout",
  276. "output_type": "stream",
  277. "text": [
  278. "[0, 2, 4]\n"
  279. ]
  280. }
  281. ],
  282. "source": [
  283. "filtered_out_list_using_lambda.remove(90) # removes the first instance of 90\n",
  284. "print(filtered_out_list_using_lambda)"
  285. ]
  286. },
  287. {
  288. "cell_type": "markdown",
  289. "metadata": {},
  290. "source": [
  291. "#### Selecting a sublist : notation ####"
  292. ]
  293. },
  294. {
  295. "cell_type": "code",
  296. "execution_count": 35,
  297. "metadata": {},
  298. "outputs": [
  299. {
  300. "name": "stdout",
  301. "output_type": "stream",
  302. "text": [
  303. "[0, 5, 10, 15, 20]\n"
  304. ]
  305. }
  306. ],
  307. "source": [
  308. "print(list_compre)"
  309. ]
  310. },
  311. {
  312. "cell_type": "code",
  313. "execution_count": 36,
  314. "metadata": {},
  315. "outputs": [
  316. {
  317. "name": "stdout",
  318. "output_type": "stream",
  319. "text": [
  320. "[10, 15]\n"
  321. ]
  322. }
  323. ],
  324. "source": [
  325. "subList=list_compre[2:4] # start from 2nd index till 3rd\n",
  326. "print(subList)"
  327. ]
  328. },
  329. {
  330. "cell_type": "markdown",
  331. "metadata": {},
  332. "source": [
  333. "#### Nested List ####"
  334. ]
  335. },
  336. {
  337. "cell_type": "code",
  338. "execution_count": 38,
  339. "metadata": {},
  340. "outputs": [
  341. {
  342. "name": "stdout",
  343. "output_type": "stream",
  344. "text": [
  345. "[1, 2, 3, 4, ['a', 'b', 'c', 'd']]\n"
  346. ]
  347. }
  348. ],
  349. "source": [
  350. "nested_alpha_numeric_list = [1,2,3,4,['a','b','c','d']]\n",
  351. "print(nested_alpha_numeric_list)"
  352. ]
  353. },
  354. {
  355. "cell_type": "markdown",
  356. "metadata": {},
  357. "source": [
  358. "#### Accessing particular element in a nested List using [][] notations ####"
  359. ]
  360. },
  361. {
  362. "cell_type": "code",
  363. "execution_count": 39,
  364. "metadata": {},
  365. "outputs": [
  366. {
  367. "name": "stdout",
  368. "output_type": "stream",
  369. "text": [
  370. "b\n"
  371. ]
  372. }
  373. ],
  374. "source": [
  375. "print(nested_alpha_numeric_list[4][1]) ##accesses 'b' of nested list"
  376. ]
  377. },
  378. {
  379. "cell_type": "markdown",
  380. "metadata": {},
  381. "source": [
  382. "#### Iterating a List item by item ####"
  383. ]
  384. },
  385. {
  386. "cell_type": "code",
  387. "execution_count": 41,
  388. "metadata": {},
  389. "outputs": [
  390. {
  391. "name": "stdout",
  392. "output_type": "stream",
  393. "text": [
  394. "0\n",
  395. "5\n",
  396. "10\n",
  397. "15\n",
  398. "20\n"
  399. ]
  400. }
  401. ],
  402. "source": [
  403. "for item in list_compre:\n",
  404. " print(item)"
  405. ]
  406. },
  407. {
  408. "cell_type": "code",
  409. "execution_count": null,
  410. "metadata": {
  411. "collapsed": true
  412. },
  413. "outputs": [],
  414. "source": []
  415. }
  416. ],
  417. "metadata": {
  418. "kernelspec": {
  419. "display_name": "Python 3",
  420. "language": "python",
  421. "name": "python3"
  422. },
  423. "language_info": {
  424. "codemirror_mode": {
  425. "name": "ipython",
  426. "version": 3
  427. },
  428. "file_extension": ".py",
  429. "mimetype": "text/x-python",
  430. "name": "python",
  431. "nbconvert_exporter": "python",
  432. "pygments_lexer": "ipython3",
  433. "version": "3.6.3"
  434. }
  435. },
  436. "nbformat": 4,
  437. "nbformat_minor": 2
  438. }
Add Comment
Please, Sign In to add comment