Guest User

Untitled

a guest
Jan 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.79 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 3,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "1\n"
  13. ]
  14. }
  15. ],
  16. "source": [
  17. "a = {(1,2):1,(2,3):2}\n",
  18. "print(a[1,2])"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": 4,
  24. "metadata": {},
  25. "outputs": [
  26. {
  27. "ename": "KeyError",
  28. "evalue": "('a', 'b')",
  29. "output_type": "error",
  30. "traceback": [
  31. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  32. "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
  33. "\u001b[0;32m<ipython-input-4-0e60033381d8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'b'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'c'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'b'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  34. "\u001b[0;31mKeyError\u001b[0m: ('a', 'b')"
  35. ]
  36. }
  37. ],
  38. "source": [
  39. "a = {'a':1,'b':2,'c':3}\n",
  40. "print(a['a','b'])\n",
  41. "\n",
  42. "#error cause it aint a tuple"
  43. ]
  44. },
  45. {
  46. "cell_type": "code",
  47. "execution_count": 5,
  48. "metadata": {},
  49. "outputs": [
  50. {
  51. "name": "stdout",
  52. "output_type": "stream",
  53. "text": [
  54. "1\n"
  55. ]
  56. }
  57. ],
  58. "source": [
  59. "a = {'a':1,'b':2,'c':3}\n",
  60. "print(a['a'])\n",
  61. "\n"
  62. ]
  63. },
  64. {
  65. "cell_type": "code",
  66. "execution_count": 7,
  67. "metadata": {},
  68. "outputs": [
  69. {
  70. "name": "stdout",
  71. "output_type": "stream",
  72. "text": [
  73. "2\n"
  74. ]
  75. }
  76. ],
  77. "source": [
  78. "print(a['b'])"
  79. ]
  80. },
  81. {
  82. "cell_type": "code",
  83. "execution_count": 8,
  84. "metadata": {},
  85. "outputs": [],
  86. "source": [
  87. "arr = {\n",
  88. "}"
  89. ]
  90. },
  91. {
  92. "cell_type": "code",
  93. "execution_count": 9,
  94. "metadata": {},
  95. "outputs": [],
  96. "source": [
  97. "arr[1] = 1"
  98. ]
  99. },
  100. {
  101. "cell_type": "code",
  102. "execution_count": 10,
  103. "metadata": {},
  104. "outputs": [
  105. {
  106. "data": {
  107. "text/plain": [
  108. "{1: 1}"
  109. ]
  110. },
  111. "execution_count": 10,
  112. "metadata": {},
  113. "output_type": "execute_result"
  114. }
  115. ],
  116. "source": [
  117. "arr"
  118. ]
  119. },
  120. {
  121. "cell_type": "code",
  122. "execution_count": 11,
  123. "metadata": {},
  124. "outputs": [],
  125. "source": [
  126. "arr['1'] = 2"
  127. ]
  128. },
  129. {
  130. "cell_type": "code",
  131. "execution_count": 12,
  132. "metadata": {},
  133. "outputs": [
  134. {
  135. "data": {
  136. "text/plain": [
  137. "{1: 1, '1': 2}"
  138. ]
  139. },
  140. "execution_count": 12,
  141. "metadata": {},
  142. "output_type": "execute_result"
  143. }
  144. ],
  145. "source": [
  146. "arr"
  147. ]
  148. },
  149. {
  150. "cell_type": "code",
  151. "execution_count": 13,
  152. "metadata": {},
  153. "outputs": [],
  154. "source": [
  155. "arr[1] += 1"
  156. ]
  157. },
  158. {
  159. "cell_type": "code",
  160. "execution_count": 14,
  161. "metadata": {},
  162. "outputs": [
  163. {
  164. "data": {
  165. "text/plain": [
  166. "{1: 2, '1': 2}"
  167. ]
  168. },
  169. "execution_count": 14,
  170. "metadata": {},
  171. "output_type": "execute_result"
  172. }
  173. ],
  174. "source": [
  175. "arr"
  176. ]
  177. },
  178. {
  179. "cell_type": "code",
  180. "execution_count": 15,
  181. "metadata": {},
  182. "outputs": [
  183. {
  184. "data": {
  185. "text/plain": [
  186. "4"
  187. ]
  188. },
  189. "execution_count": 15,
  190. "metadata": {},
  191. "output_type": "execute_result"
  192. }
  193. ],
  194. "source": [
  195. "sum = 0\n",
  196. "for k in arr:\n",
  197. " sum += arr[k]\n",
  198. " \n",
  199. "sum"
  200. ]
  201. },
  202. {
  203. "cell_type": "code",
  204. "execution_count": 16,
  205. "metadata": {},
  206. "outputs": [],
  207. "source": [
  208. "my_dict = {}"
  209. ]
  210. },
  211. {
  212. "cell_type": "code",
  213. "execution_count": 17,
  214. "metadata": {},
  215. "outputs": [],
  216. "source": [
  217. "my_dict[1]=1\n",
  218. "my_dict['1'] = 2\n",
  219. "my_dict[1.0]=4\n",
  220. "\n"
  221. ]
  222. },
  223. {
  224. "cell_type": "code",
  225. "execution_count": 18,
  226. "metadata": {},
  227. "outputs": [
  228. {
  229. "data": {
  230. "text/plain": [
  231. "{1: 4, '1': 2}"
  232. ]
  233. },
  234. "execution_count": 18,
  235. "metadata": {},
  236. "output_type": "execute_result"
  237. }
  238. ],
  239. "source": [
  240. "my_dict\n"
  241. ]
  242. },
  243. {
  244. "cell_type": "code",
  245. "execution_count": 19,
  246. "metadata": {},
  247. "outputs": [
  248. {
  249. "data": {
  250. "text/plain": [
  251. "6"
  252. ]
  253. },
  254. "execution_count": 19,
  255. "metadata": {},
  256. "output_type": "execute_result"
  257. }
  258. ],
  259. "source": [
  260. "sum =0\n",
  261. "for k in my_dict:\n",
  262. " sum += my_dict[k]\n",
  263. " \n",
  264. "sum"
  265. ]
  266. },
  267. {
  268. "cell_type": "code",
  269. "execution_count": 20,
  270. "metadata": {},
  271. "outputs": [],
  272. "source": [
  273. "lol ={}"
  274. ]
  275. },
  276. {
  277. "cell_type": "code",
  278. "execution_count": 22,
  279. "metadata": {},
  280. "outputs": [
  281. {
  282. "data": {
  283. "text/plain": [
  284. "{(1, 2, 4): 8}"
  285. ]
  286. },
  287. "execution_count": 22,
  288. "metadata": {},
  289. "output_type": "execute_result"
  290. }
  291. ],
  292. "source": [
  293. "lol[(1,2,4)] = 8\n",
  294. "lol"
  295. ]
  296. },
  297. {
  298. "cell_type": "code",
  299. "execution_count": 23,
  300. "metadata": {},
  301. "outputs": [
  302. {
  303. "data": {
  304. "text/plain": [
  305. "{(1, 2, 4): 8, (4, 2, 1): 10}"
  306. ]
  307. },
  308. "execution_count": 23,
  309. "metadata": {},
  310. "output_type": "execute_result"
  311. }
  312. ],
  313. "source": [
  314. "lol[(4,2,1)] = 10\n",
  315. "lol"
  316. ]
  317. },
  318. {
  319. "cell_type": "code",
  320. "execution_count": 24,
  321. "metadata": {},
  322. "outputs": [
  323. {
  324. "data": {
  325. "text/plain": [
  326. "{(1, 2, 4): 8, (4, 2, 1): 10, (1, 2): 12}"
  327. ]
  328. },
  329. "execution_count": 24,
  330. "metadata": {},
  331. "output_type": "execute_result"
  332. }
  333. ],
  334. "source": [
  335. "lol[(1,2)] = 12\n",
  336. "lol"
  337. ]
  338. },
  339. {
  340. "cell_type": "code",
  341. "execution_count": 26,
  342. "metadata": {},
  343. "outputs": [
  344. {
  345. "data": {
  346. "text/plain": [
  347. "30"
  348. ]
  349. },
  350. "execution_count": 26,
  351. "metadata": {},
  352. "output_type": "execute_result"
  353. }
  354. ],
  355. "source": [
  356. "sum = 0\n",
  357. "for k in lol:\n",
  358. " sum += lol[k]\n",
  359. " \n",
  360. "sum\n"
  361. ]
  362. },
  363. {
  364. "cell_type": "code",
  365. "execution_count": 27,
  366. "metadata": {},
  367. "outputs": [
  368. {
  369. "data": {
  370. "text/plain": [
  371. "{(1, 2, 4): 8, (4, 2, 1): 10, (1, 2): 12}"
  372. ]
  373. },
  374. "execution_count": 27,
  375. "metadata": {},
  376. "output_type": "execute_result"
  377. }
  378. ],
  379. "source": [
  380. "lol"
  381. ]
  382. },
  383. {
  384. "cell_type": "code",
  385. "execution_count": null,
  386. "metadata": {},
  387. "outputs": [],
  388. "source": []
  389. },
  390. {
  391. "cell_type": "code",
  392. "execution_count": 30,
  393. "metadata": {},
  394. "outputs": [],
  395. "source": [
  396. "box = {}"
  397. ]
  398. },
  399. {
  400. "cell_type": "code",
  401. "execution_count": 31,
  402. "metadata": {},
  403. "outputs": [],
  404. "source": [
  405. "jars = {}"
  406. ]
  407. },
  408. {
  409. "cell_type": "code",
  410. "execution_count": 32,
  411. "metadata": {},
  412. "outputs": [],
  413. "source": [
  414. "crates = {\n",
  415. " \n",
  416. "}"
  417. ]
  418. },
  419. {
  420. "cell_type": "code",
  421. "execution_count": 33,
  422. "metadata": {},
  423. "outputs": [],
  424. "source": [
  425. "box['biscuit'] = 1\n",
  426. "box['cake'] = 3\n",
  427. "jars['jam']=4\n",
  428. "crates['box']=box\n",
  429. "crates['jars'] = jars\n"
  430. ]
  431. },
  432. {
  433. "cell_type": "code",
  434. "execution_count": 34,
  435. "metadata": {},
  436. "outputs": [
  437. {
  438. "data": {
  439. "text/plain": [
  440. "{'biscuit': 1, 'cake': 3}"
  441. ]
  442. },
  443. "execution_count": 34,
  444. "metadata": {},
  445. "output_type": "execute_result"
  446. }
  447. ],
  448. "source": [
  449. "box"
  450. ]
  451. },
  452. {
  453. "cell_type": "code",
  454. "execution_count": 36,
  455. "metadata": {},
  456. "outputs": [
  457. {
  458. "data": {
  459. "text/plain": [
  460. "{'jam': 4}"
  461. ]
  462. },
  463. "execution_count": 36,
  464. "metadata": {},
  465. "output_type": "execute_result"
  466. }
  467. ],
  468. "source": [
  469. "jars"
  470. ]
  471. },
  472. {
  473. "cell_type": "code",
  474. "execution_count": 37,
  475. "metadata": {},
  476. "outputs": [
  477. {
  478. "data": {
  479. "text/plain": [
  480. "{'box': {'biscuit': 1, 'cake': 3}, 'jars': {'jam': 4}}"
  481. ]
  482. },
  483. "execution_count": 37,
  484. "metadata": {},
  485. "output_type": "execute_result"
  486. }
  487. ],
  488. "source": [
  489. "crates"
  490. ]
  491. },
  492. {
  493. "cell_type": "raw",
  494. "metadata": {},
  495. "source": []
  496. },
  497. {
  498. "cell_type": "code",
  499. "execution_count": 38,
  500. "metadata": {},
  501. "outputs": [
  502. {
  503. "ename": "TypeError",
  504. "evalue": "unhashable type: 'dict'",
  505. "output_type": "error",
  506. "traceback": [
  507. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  508. "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
  509. "\u001b[0;32m<ipython-input-38-7a527bbca4ec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcrates\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mbox\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  510. "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'dict'"
  511. ]
  512. }
  513. ],
  514. "source": [
  515. "print(len(crates[box]))"
  516. ]
  517. },
  518. {
  519. "cell_type": "code",
  520. "execution_count": 39,
  521. "metadata": {},
  522. "outputs": [
  523. {
  524. "data": {
  525. "text/plain": [
  526. "1"
  527. ]
  528. },
  529. "execution_count": 39,
  530. "metadata": {},
  531. "output_type": "execute_result"
  532. }
  533. ],
  534. "source": [
  535. "len(jars)"
  536. ]
  537. },
  538. {
  539. "cell_type": "code",
  540. "execution_count": 42,
  541. "metadata": {},
  542. "outputs": [
  543. {
  544. "data": {
  545. "text/plain": [
  546. "2"
  547. ]
  548. },
  549. "execution_count": 42,
  550. "metadata": {},
  551. "output_type": "execute_result"
  552. }
  553. ],
  554. "source": [
  555. "len(crates)"
  556. ]
  557. },
  558. {
  559. "cell_type": "code",
  560. "execution_count": 43,
  561. "metadata": {},
  562. "outputs": [
  563. {
  564. "data": {
  565. "text/plain": [
  566. "2"
  567. ]
  568. },
  569. "execution_count": 43,
  570. "metadata": {},
  571. "output_type": "execute_result"
  572. }
  573. ],
  574. "source": [
  575. "len(box)"
  576. ]
  577. },
  578. {
  579. "cell_type": "code",
  580. "execution_count": 44,
  581. "metadata": {},
  582. "outputs": [],
  583. "source": [
  584. "p = {'c':97,'a':96,'b':98}"
  585. ]
  586. },
  587. {
  588. "cell_type": "code",
  589. "execution_count": 49,
  590. "metadata": {},
  591. "outputs": [
  592. {
  593. "name": "stdout",
  594. "output_type": "stream",
  595. "text": [
  596. "96\n",
  597. "98\n",
  598. "97\n"
  599. ]
  600. }
  601. ],
  602. "source": [
  603. "for l in sorted(p):\n",
  604. " print(p[l])\n",
  605. " \n",
  606. "#sorted works only for dictionaries\n"
  607. ]
  608. },
  609. {
  610. "cell_type": "code",
  611. "execution_count": 1,
  612. "metadata": {},
  613. "outputs": [],
  614. "source": [
  615. "x = [1,2,3,4]"
  616. ]
  617. },
  618. {
  619. "cell_type": "code",
  620. "execution_count": 2,
  621. "metadata": {},
  622. "outputs": [],
  623. "source": [
  624. "x[:0] = [-1, 0]"
  625. ]
  626. },
  627. {
  628. "cell_type": "code",
  629. "execution_count": 3,
  630. "metadata": {},
  631. "outputs": [
  632. {
  633. "data": {
  634. "text/plain": [
  635. "[-1, 0, 1, 2, 3, 4]"
  636. ]
  637. },
  638. "execution_count": 3,
  639. "metadata": {},
  640. "output_type": "execute_result"
  641. }
  642. ],
  643. "source": [
  644. "x"
  645. ]
  646. },
  647. {
  648. "cell_type": "code",
  649. "execution_count": 4,
  650. "metadata": {},
  651. "outputs": [],
  652. "source": [
  653. "x[1:-1] = []"
  654. ]
  655. },
  656. {
  657. "cell_type": "code",
  658. "execution_count": 5,
  659. "metadata": {},
  660. "outputs": [
  661. {
  662. "data": {
  663. "text/plain": [
  664. "[-1, 4]"
  665. ]
  666. },
  667. "execution_count": 5,
  668. "metadata": {},
  669. "output_type": "execute_result"
  670. }
  671. ],
  672. "source": [
  673. "x"
  674. ]
  675. },
  676. {
  677. "cell_type": "code",
  678. "execution_count": 6,
  679. "metadata": {},
  680. "outputs": [],
  681. "source": [
  682. "lst1=[1,2,[11,22]]"
  683. ]
  684. },
  685. {
  686. "cell_type": "code",
  687. "execution_count": 7,
  688. "metadata": {},
  689. "outputs": [
  690. {
  691. "data": {
  692. "text/plain": [
  693. "[1, 2, [11, 22]]"
  694. ]
  695. },
  696. "execution_count": 7,
  697. "metadata": {},
  698. "output_type": "execute_result"
  699. }
  700. ],
  701. "source": [
  702. "lst1\n"
  703. ]
  704. },
  705. {
  706. "cell_type": "code",
  707. "execution_count": null,
  708. "metadata": {},
  709. "outputs": [],
  710. "source": []
  711. }
  712. ],
  713. "metadata": {
  714. "kernelspec": {
  715. "display_name": "Python 3",
  716. "language": "python",
  717. "name": "python3"
  718. },
  719. "language_info": {
  720. "codemirror_mode": {
  721. "name": "ipython",
  722. "version": 3
  723. },
  724. "file_extension": ".py",
  725. "mimetype": "text/x-python",
  726. "name": "python",
  727. "nbconvert_exporter": "python",
  728. "pygments_lexer": "ipython3",
  729. "version": "3.6.5"
  730. }
  731. },
  732. "nbformat": 4,
  733. "nbformat_minor": 2
  734. }
Add Comment
Please, Sign In to add comment