Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Commit 304411e

Browse files
updating notes 02
1 parent a185df9 commit 304411e

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed

02-array-seq/02-notes.ipynb

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,232 @@
462462
"2. 用`_make()`通过接受一个可迭代对象来生成这个类的一个实例,它的作用跟`City(*delhi_data)`是一样的。\n",
463463
"3. `_asdict()`把具名元组以`collections.OrderedDict`的形式返回,我们可以利用它来把元组里的信息清晰的呈现出来。"
464464
]
465+
},
466+
{
467+
"cell_type": "markdown",
468+
"metadata": {},
469+
"source": [
470+
"### 2.3.5 作为不可变列表的元组\n",
471+
"\n",
472+
"除了和增减元素相关的方法之外,元组支持列表的其他所有方法。有一个例外是元组没有`__reversed__`方法 (书上描述:这个方法只是个优化) ,但是可以使用`reversed()`函数。\n",
473+
"\n",
474+
"注: `__reversed__` method returns an iterator of reversed items"
475+
]
476+
},
477+
{
478+
"cell_type": "code",
479+
"execution_count": 3,
480+
"metadata": {},
481+
"outputs": [
482+
{
483+
"name": "stdout",
484+
"output_type": "stream",
485+
"text": [
486+
"<reversed object at 0x00000291885FE9B0>\n",
487+
"(3, 2, 1)\n"
488+
]
489+
}
490+
],
491+
"source": [
492+
"a = (1,2,3)\n",
493+
"a = reversed(a)\n",
494+
"print(a)\n",
495+
"print(tuple(a))"
496+
]
497+
},
498+
{
499+
"cell_type": "code",
500+
"execution_count": 5,
501+
"metadata": {},
502+
"outputs": [
503+
{
504+
"name": "stdout",
505+
"output_type": "stream",
506+
"text": [
507+
"<list_reverseiterator object at 0x00000291885FF9A0>\n",
508+
"[3, 2, 1]\n"
509+
]
510+
}
511+
],
512+
"source": [
513+
"a = [1,2,3]\n",
514+
"a = reversed(a)\n",
515+
"print(a)\n",
516+
"print(list(a))"
517+
]
518+
},
519+
{
520+
"cell_type": "markdown",
521+
"metadata": {},
522+
"source": [
523+
"## 2.4 切片\n",
524+
"`a:b:c` 这种用法只能作为索引或者下标用在[]中来返回一个切片对象:`slice(a, b, c)` 。对`seq[start:stop:step]` 进行求值的时候,Python会调用`seq`. \n",
525+
"`__getitem__(slice(start, stop, step))`。\n",
526+
"\n",
527+
"(may refer to a video about slicing <a href = \"https://www.bilibili.com/video/BV1ki421v7Ao/?share_source=copy_web&vd_source=0f2805b5dbbfea4fe3a5cd27c708c741\">【Python】Slice:被低估的小技巧,减少重复工作量 </a>)\n",
528+
"\n",
529+
"### 2.4.3 多维切片和省略\n",
530+
"`[]` 运算符里还可以使用以逗号分开的多个索引或者是切片, numpy库就利用了这个特性。\n",
531+
"\n",
532+
"要正确处理这种`[]`运算符的话,对象的特殊方法`__getitem__`和`__setitem__`需要以元组的形式来接收`a[i, j]`中的索引。也就是说,如果要得到`a[i, j]`的值,Python会调用`a.__getitem__((i, j))`\n",
533+
"\n",
534+
"省略`...`是Ellipsis对象的别名,它可以表示任意多的冒号\n",
535+
"\n",
536+
"书本注:fun fact, `Ellipsis` object is a singleton object of `ellipsis` class (Ellipsis是一个内置实例). Yes, it is a class with lower letters. Similar to `bool` class with `True` and `False` instances.\n"
537+
]
538+
},
539+
{
540+
"cell_type": "code",
541+
"execution_count": 12,
542+
"metadata": {},
543+
"outputs": [
544+
{
545+
"name": "stdout",
546+
"output_type": "stream",
547+
"text": [
548+
"[[[ 1 2 3 4]\n",
549+
" [ 5 6 7 8]]\n",
550+
"\n",
551+
" [[12 34 56 78]\n",
552+
" [56 78 90 12]]]\n"
553+
]
554+
}
555+
],
556+
"source": [
557+
"import numpy as np\n",
558+
"\n",
559+
"a = np.array([[[[1,2,3,4],[5,6,7,8]],[[12,34,56,78],[56,78,90,12]]],[[[1,2,3,4],[5,6,7,8]],[[12,34,56,78],[56,78,90,12]]]])\n",
560+
"b = a[0, ...] # very interesting example\n",
561+
"print(b)"
562+
]
563+
},
564+
{
565+
"cell_type": "markdown",
566+
"metadata": {},
567+
"source": [
568+
"### 2.4.4 给切片赋值(有意思)\n",
569+
"如果把切片放在赋值语句的左边,或把它作为`del`操作的对象,我们就可以对序列进行**嫁接、切除或就地修改**操作。"
570+
]
571+
},
572+
{
573+
"cell_type": "code",
574+
"execution_count": 19,
575+
"metadata": {},
576+
"outputs": [
577+
{
578+
"name": "stdout",
579+
"output_type": "stream",
580+
"text": [
581+
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
582+
]
583+
}
584+
],
585+
"source": [
586+
"l = list(range(10))\n",
587+
"print(l)"
588+
]
589+
},
590+
{
591+
"cell_type": "code",
592+
"execution_count": 20,
593+
"metadata": {},
594+
"outputs": [
595+
{
596+
"data": {
597+
"text/plain": [
598+
"[0, 1, 20, 30, 5, 6, 7, 8, 9]"
599+
]
600+
},
601+
"execution_count": 20,
602+
"metadata": {},
603+
"output_type": "execute_result"
604+
}
605+
],
606+
"source": [
607+
"l[2:5] = [20,30]\n",
608+
"l"
609+
]
610+
},
611+
{
612+
"cell_type": "code",
613+
"execution_count": 21,
614+
"metadata": {},
615+
"outputs": [
616+
{
617+
"data": {
618+
"text/plain": [
619+
"[0, 1, 20, 30, 5, 8, 9]"
620+
]
621+
},
622+
"execution_count": 21,
623+
"metadata": {},
624+
"output_type": "execute_result"
625+
}
626+
],
627+
"source": [
628+
"del l[5:7]\n",
629+
"l"
630+
]
631+
},
632+
{
633+
"cell_type": "code",
634+
"execution_count": 22,
635+
"metadata": {},
636+
"outputs": [
637+
{
638+
"data": {
639+
"text/plain": [
640+
"[0, 1, 20, 11, 5, 22, 9]"
641+
]
642+
},
643+
"execution_count": 22,
644+
"metadata": {},
645+
"output_type": "execute_result"
646+
}
647+
],
648+
"source": [
649+
"l[3::2] = [11,22]\n",
650+
"l"
651+
]
652+
},
653+
{
654+
"cell_type": "code",
655+
"execution_count": 23,
656+
"metadata": {},
657+
"outputs": [
658+
{
659+
"data": {
660+
"text/plain": [
661+
"[0, 1, 100, 22, 9]"
662+
]
663+
},
664+
"execution_count": 23,
665+
"metadata": {},
666+
"output_type": "execute_result"
667+
}
668+
],
669+
"source": [
670+
"# l[2:5] = 100 error because 100 is not iterable\n",
671+
"l[2:5] = [100] \n",
672+
"l"
673+
]
674+
},
675+
{
676+
"cell_type": "markdown",
677+
"metadata": {},
678+
"source": [
679+
"如果赋值的对象是一个切片,那么赋值语句的右侧**必须是个可迭代对象**。即便只有单独一个值,也要把它转换成可迭代的序列。\n",
680+
"\n",
681+
"## 对序列使用`+`和`*`\n",
682+
"`+`和`*`都遵循这样的规律:不修改原有的操作对象,而是构建一个全新的序列。\n",
683+
"\n",
684+
"`*`操作符的一个潜在的缺点是,它会把一个单一的元素复制多次以构建新的列表。这意味着,如果这个元素是**可变的**,就可能导致意想不到的副作用。(萌新时期噩梦!)"
685+
]
686+
},
687+
{
688+
"cell_type": "markdown",
689+
"metadata": {},
690+
"source": []
465691
}
466692
],
467693
"metadata": {

0 commit comments

Comments
 (0)