Skip to content

Commit c157734

Browse files
committed
chore: ch2 note - tuple attribution & copy/ deepcopy
1 parent 91fe373 commit c157734

File tree

1 file changed

+126
-12
lines changed

1 file changed

+126
-12
lines changed

02-array-seq/array-seq.ipynb

Lines changed: 126 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,45 @@
490490
"#### Example 2-7. Tuples used as records"
491491
]
492492
},
493+
{
494+
"cell_type": "markdown",
495+
"metadata": {},
496+
"source": [
497+
"`tuple` 只能接一個引數,並且**可以排序**"
498+
]
499+
},
493500
{
494501
"cell_type": "code",
495-
"execution_count": 14,
502+
"execution_count": 13,
503+
"metadata": {},
504+
"outputs": [
505+
{
506+
"name": "stdout",
507+
"output_type": "stream",
508+
"text": [
509+
"TypeError: tuple expected at most 1 argument, got 2\n",
510+
"AttributeError: 'tuple' object has no attribute 'sort'\n",
511+
"[-118.408056, 33.9425]\n"
512+
]
513+
}
514+
],
515+
"source": [
516+
"try:\n",
517+
" lax_coordinates = tuple(33.9425, -118.408056)\n",
518+
"except TypeError: \n",
519+
" print(\"TypeError: tuple expected at most 1 argument, got 2\")\n",
520+
"\n",
521+
"lax_coordinates = tuple((33.9425, -118.408056))\n",
522+
"try:\n",
523+
" lax_coordinates.sort()\n",
524+
"except AttributeError:\n",
525+
" print(\"AttributeError: 'tuple' object has no attribute 'sort'\")\n",
526+
"print(sorted(lax_coordinates))"
527+
]
528+
},
529+
{
530+
"cell_type": "code",
531+
"execution_count": 6,
496532
"metadata": {
497533
"collapsed": false,
498534
"pycharm": {
@@ -504,6 +540,7 @@
504540
"name": "stdout",
505541
"output_type": "stream",
506542
"text": [
543+
"<class 'tuple'>\n",
507544
"BRA/CE342567\n",
508545
"ESP/XDA205856\n",
509546
"USA/31195855\n"
@@ -512,6 +549,8 @@
512549
],
513550
"source": [
514551
"lax_coordinates = (33.9425, -118.408056)\n",
552+
"print(type(lax_coordinates))\n",
553+
"\n",
515554
"city, year, pop, chg, area = ('Tokyo', 2003, 32_450, 0.66, 8014)\n",
516555
"traveler_ids = [('USA', '31195855'), ('BRA', 'CE342567'), ('ESP', 'XDA205856')]\n",
517556
"\n",
@@ -521,7 +560,7 @@
521560
},
522561
{
523562
"cell_type": "code",
524-
"execution_count": 15,
563+
"execution_count": 2,
525564
"metadata": {
526565
"collapsed": false,
527566
"pycharm": {
@@ -558,7 +597,7 @@
558597
},
559598
{
560599
"cell_type": "code",
561-
"execution_count": 16,
600+
"execution_count": 7,
562601
"metadata": {
563602
"collapsed": false,
564603
"pycharm": {
@@ -572,7 +611,7 @@
572611
"True"
573612
]
574613
},
575-
"execution_count": 16,
614+
"execution_count": 7,
576615
"metadata": {},
577616
"output_type": "execute_result"
578617
}
@@ -585,7 +624,7 @@
585624
},
586625
{
587626
"cell_type": "code",
588-
"execution_count": 17,
627+
"execution_count": 8,
589628
"metadata": {
590629
"collapsed": false,
591630
"pycharm": {
@@ -599,7 +638,7 @@
599638
"False"
600639
]
601640
},
602-
"execution_count": 17,
641+
"execution_count": 8,
603642
"metadata": {},
604643
"output_type": "execute_result"
605644
}
@@ -611,7 +650,7 @@
611650
},
612651
{
613652
"cell_type": "code",
614-
"execution_count": 18,
653+
"execution_count": 9,
615654
"metadata": {
616655
"collapsed": false,
617656
"pycharm": {
@@ -625,7 +664,7 @@
625664
"(10, 'alpha', [1, 2, 99])"
626665
]
627666
},
628-
"execution_count": 18,
667+
"execution_count": 9,
629668
"metadata": {},
630669
"output_type": "execute_result"
631670
}
@@ -636,7 +675,7 @@
636675
},
637676
{
638677
"cell_type": "code",
639-
"execution_count": 19,
678+
"execution_count": 10,
640679
"metadata": {
641680
"collapsed": false,
642681
"pycharm": {
@@ -650,7 +689,7 @@
650689
"True"
651690
]
652691
},
653-
"execution_count": 19,
692+
"execution_count": 10,
654693
"metadata": {},
655694
"output_type": "execute_result"
656695
}
@@ -671,7 +710,7 @@
671710
},
672711
{
673712
"cell_type": "code",
674-
"execution_count": 20,
713+
"execution_count": 11,
675714
"metadata": {
676715
"collapsed": false,
677716
"pycharm": {
@@ -685,7 +724,7 @@
685724
"False"
686725
]
687726
},
688-
"execution_count": 20,
727+
"execution_count": 11,
689728
"metadata": {},
690729
"output_type": "execute_result"
691730
}
@@ -694,6 +733,81 @@
694733
"fixed(tm)"
695734
]
696735
},
736+
{
737+
"cell_type": "markdown",
738+
"metadata": {},
739+
"source": [
740+
"`tuple` 可以相加串接"
741+
]
742+
},
743+
{
744+
"cell_type": "code",
745+
"execution_count": null,
746+
"metadata": {},
747+
"outputs": [
748+
{
749+
"data": {
750+
"text/plain": [
751+
"(10, 'alpha', (1, 2), 10, 'alpha', [1, 2])"
752+
]
753+
},
754+
"execution_count": 15,
755+
"metadata": {},
756+
"output_type": "execute_result"
757+
}
758+
],
759+
"source": [
760+
"tf + tm"
761+
]
762+
},
763+
{
764+
"cell_type": "markdown",
765+
"metadata": {},
766+
"source": [
767+
"`tuple` **不能使用** `tuple.copy()`\n",
768+
"- `copy.copy()`(淺拷貝, shallow copy)\n",
769+
" - 只複製最外層的容器物件,裡面的巢狀物件(例如 list 裡的 list)只是參考(reference)。\n",
770+
" - 也就是說,內層的資料仍指向原本的記憶體位置,兩者共享。 \n",
771+
"- `copy.deepcopy()`(深拷貝, deep copy)\n",
772+
" - 遞迴地複製所有巢狀物件。\n",
773+
" - 原物件與拷貝物件完全獨立,改其中一個不會影響另一個。\n",
774+
"\n",
775+
"| 功能 | `copy.copy()`(淺拷貝) | `copy.deepcopy()`(深拷貝) |\n",
776+
"| -------- | ------------------ | ---------------------- |\n",
777+
"| 複製外層 | ✅ | ✅ |\n",
778+
"| 複製內層巢狀物件 | ❌(參照原物件) | ✅(新建物件) |\n",
779+
"| 速度與效能 | 較快 | 較慢(因為遞迴複製) |\n"
780+
]
781+
},
782+
{
783+
"cell_type": "code",
784+
"execution_count": null,
785+
"metadata": {},
786+
"outputs": [
787+
{
788+
"name": "stdout",
789+
"output_type": "stream",
790+
"text": [
791+
"'tuple' object has no attribute 'copy'\n",
792+
"tf_copy = (10, 'alpha', (1, 2))\n",
793+
"tf_deepcopy = (10, 'alpha', (1, 2))\n"
794+
]
795+
}
796+
],
797+
"source": [
798+
"try:\n",
799+
" tf_copy = tf.copy()\n",
800+
"except AttributeError: \n",
801+
" print(\"'tuple' object has no attribute 'copy'\")\n",
802+
"\n",
803+
"from copy import copy, deepcopy\n",
804+
"\n",
805+
"tf_copy = copy(tf)\n",
806+
"print(f\"tf_copy = {tf_copy}\")\n",
807+
"tf_deepcopy = deepcopy(tf)\n",
808+
"print(f\"tf_deepcopy = {tf_deepcopy}\")"
809+
]
810+
},
697811
{
698812
"cell_type": "markdown",
699813
"metadata": {

0 commit comments

Comments
 (0)