2
"cells": [
3
{
4
"cell_type": "markdown",
5
"metadata": {},
6
"source": [
7
">作者 Chris Burns, Christophe Combelles, Emmanuelle Gouillart, Gaël Varoquaux"
8
]
9
},
10
{
11
"cell_type": "markdown",
12
"metadata": {},
13
"source": [
14
">**Python中的科学计算**\n",
15
">这里我们介绍Python语言。这里只会仅仅解决可以用于Numpy和Scipy的最低要求。想要更多的了解这门语言,请参考http://docs.python.org/tutorial 这个非常好的教程。也可以借助专门的图书,比如:http://diveintopython.org/.\n",
16
"\n",
17
"Python是一门**编程语言**,与C、Fortran、BASIC和PHP等等类似。Python的一些特性如下:\n",
18
"\n",
19
"- 一种*解释性*(不是编译)语言。与C或者Fortran等不同,Python代码在执行前不会编译。另外,Python可以**交互**使用:有许多的Python解释器,命令和脚本可以在其中执行。\n",
20
"- 在**开源**证书下发布的免费软件:Python可以免费使用和分发,即使用于商用。\n",
21
"- **多平台**:Python可以用于所有的主流操作系统,Windows、Linux/Unix、MacOS X, 甚至可能是你有手机操作系统等等。\n",
22
"- 可读性很强的语言,有清晰不罗嗦的语法\n",
23
"- 拥有大量高质量的包,可以应用于多种多样的应用,从网站框架到科学计算。\n",
24
"- 非常简单的接口与其他语言交互,特别是C和C++\n",
25
"- 稍后会介绍一些语言的其他特性。例如Python是面向对象的语言,包含动态类型(一个变量可以在程序过程中,可以包含不同的对象类型)。\n",
26
"\n",
27
"Python的特有特性的更多信息,请见:http://www.python.org/about/\n",
28
"\n",
29
"## 1.2.1 第一步\n",
30
"启动**IPython** Shell(一个增强的Python交互Shell):\n",
31
"\n",
32
"- 在Linux/Mac终端中输入“ipython”,或者在Windows cmd sheell,\n",
33
"- 或者从菜单启动程序,即在[Python(x,y)](http://www.pythonxy.com/)或[EPD](http://www.enthought.com/products/epd.php),如果你已经安装这些Python科学套装之一。\n",
34
"\n",
35
"如果你的电脑上还没有安装IPython,也可以选择其他Python shells,比如在终端中输入“Python”启动纯Python shell,或者Idle解释器。但是,我们建议使用IPython Shell,因为它增强特性,特别是对于科学计算。\n",
36
"\n",
37
"如果你已经启动了解释器,输入"
38
]
39
},
40
{
41
"cell_type": "code",
42
"execution_count": 2,
43
"metadata": {
44
"run_control": {
45
"frozen": false,
46
"read_only": false
47
}
48
},
49
"outputs": [
50
{
51
"name": "stdout",
52
"output_type": "stream",
53
"text": [
54
"Hello, world!\n"
55
]
56
}
57
],
58
"source": [
59
"print \"Hello, world!\""
60
]
61
},
62
{
63
"cell_type": "markdown",
64
"metadata": {},
65
"source": [
66
"接下来就会显示信息\"Hello, world!\"。你已经执行了你的第一条Python命令,恭喜!\n",
67
"\n",
68
"你自己开始吧,输入下列命令"
69
]
70
},
71
{
72
"cell_type": "code",
73
"execution_count": 1,
74
"metadata": {
75
"run_control": {
76
"frozen": false,
77
"read_only": false
78
}
79
},
80
"outputs": [
81
{
82
"data": {
83
"text/plain": [
84
"int"
85
]
86
},
87
"execution_count": 1,
88
"metadata": {},
89
"output_type": "execute_result"
90
}
91
],
92
"source": [
93
"a = 3\n",
94
"b = 2*a\n",
95
"type(b)"
96
]
97
},
98
{
99
"cell_type": "code",
100
"execution_count": 2,
101
"metadata": {
102
"run_control": {
103
"frozen": false,
104
"read_only": false
105
}
106
},
107
"outputs": [
108
{
109
"name": "stdout",
110
"output_type": "stream",
111
"text": [
112
"6\n"
113
]
114
}
115
],
116
"source": [
117
"print b"
118
]
119
},
120
{
121
"cell_type": "code",
122
"execution_count": 3,
123
"metadata": {
124
"run_control": {
125
"frozen": false,
126
"read_only": false
127
}
128
},
129
"outputs": [
130
{
131
"data": {
132
"text/plain": [
133
"18"
134
]
135
},
136
"execution_count": 3,
137
"metadata": {},
138
"output_type": "execute_result"
139
}
140
],
141
"source": [
142
"a*b"
143
]
144
},
145
{
146
"cell_type": "code",
147
"execution_count": 4,
148
"metadata": {
149
"run_control": {
150
"frozen": false,
151
"read_only": false
152
}
153
},
154
"outputs": [
155
{
156
"data": {
157
"text/plain": [
158
"str"
159
]
160
},
161
"execution_count": 4,
162
"metadata": {},
163
"output_type": "execute_result"
164
}
165
],
166
"source": [
167
"b = 'hello'\n",
168
"type(b)"
169
]
170
},
171
{
172
"cell_type": "code",
173
"execution_count": 5,
174
"metadata": {
175
"run_control": {
176
"frozen": false,
177
"read_only": false
178
}
179
},
180
"outputs": [
181
{
182
"data": {
183
"text/plain": [
184
"'hellohello'"
185
]
186
},
187
"execution_count": 5,
188
"metadata": {},
189
"output_type": "execute_result"
190
}
191
],
192
"source": [
193
"b + b"
194
]
195
},
196
{
197
"cell_type": "code",
198
"execution_count": 6,
199
"metadata": {
200
"run_control": {
201
"frozen": false,
202
"read_only": false
203
}
204
},
205
"outputs": [
206
{
207
"data": {
208
"text/plain": [
209
"'hellohello'"
210
]
211
},
212
"execution_count": 6,
213
"metadata": {},
214
"output_type": "execute_result"
215
}
216
],
217
"source": [
218
"2*b"
219
]
220
},
221
{
222
"cell_type": "markdown",
223
"metadata": {},
224
"source": [
225
"上面定义了*a*和*b*两个变量。注意这里在赋值前没有声明变量类型。相反,在C中,应该写为:\n",
226
"```C\n",
227
"int a=3;\n",
228
"```"
229
]
230
},
231
{
232
"cell_type": "markdown",
233
"metadata": {},
234
"source": [
235
"另外,变量的类型可以改变,在一个时间点它可以等于一个特定类型,在接下来的时间里,他可以等于另外的类型。*b*首先等于整数,但是当它被赋值为*\"hello\"*时他变成等于字符。在Python中,整数的运算符(b=2\\*a)原生支持的,一些字符上的操作符例如相加和相乘也是支持的,相当于串联和重复。\n",
236
"## 1.2.2 基础类型\n",
237
"### 1.2.2.1 数值类型\n",
238
"\n",
239
"Python支持如下的数值、标量类型:\n",
240
"\n",
241
"**整型:**"
242
]
243
},
244
{
245
"cell_type": "code",
246
"execution_count": 8,
247
"metadata": {
248
"run_control": {
249
"frozen": false,
250
"read_only": false
251
}
252
},
253
"outputs": [
254
{
255
"data": {
256
"text/plain": [
257
"2"
258
]
259
},
260
"execution_count": 8,
261
"metadata": {},
262
"output_type": "execute_result"
263
}
264
],
265
"source": [
266
"1 + 1"
267
]
268
},
269
{
270
"cell_type": "code",
271
"execution_count": 11,
272
"metadata": {
273
"run_control": {
274
"frozen": false,
275
"read_only": false
276
}
277
},
278
"outputs": [
279
{
280
"data": {
281
"text/plain": [
282
"int"
283
]
284
},
285
"execution_count": 11,
286
"metadata": {},
287
"output_type": "execute_result"
288
}
289
],
290
"source": [
291
"a = 4\n",
292
"type(a)"
293
]
294
},
295
{
296
"cell_type": "markdown",
297
"metadata": {},
298
"source": [
299
"**浮点型:**"
300
]
301
},
302
{
303
"cell_type": "code",
304
"execution_count": 12,
305
"metadata": {
306
"run_control": {
307
"frozen": false,
308
"read_only": false
309
}
310
},
311
"outputs": [
312
{
313
"data": {
314
"text/plain": [
315
"float"
316
]
317
},
318
"execution_count": 12,
319
"metadata": {},
320
"output_type": "execute_result"
321
}
322
],
323
"source": [
324
"c = 2.1\n",
325
"type(c)"
326
]
327
},
328
{
329
"cell_type": "markdown",
330
"metadata": {},
331
"source": [
332
"**复数:**"
333
]
334
},
335
{
336
"cell_type": "code",
337
"execution_count": 13,
338
"metadata": {
339
"run_control": {
340
"frozen": false,
341
"read_only": false
342
}
343
},
344
"outputs": [
345
{
346
"data": {
347
"text/plain": [
348
"1.5"
349
]
350
},
351
"execution_count": 13,
352
"metadata": {},
353
"output_type": "execute_result"
354
}
355
],
356
"source": [
357
"a = 1.5 + 0.5j\n",
358
"a.real"
359
]
360
},
361
{
362
"cell_type": "code",
363
"execution_count": 14,
364
"metadata": {
365
"run_control": {
366
"frozen": false,
367
"read_only": false
368
}
369
},
370
"outputs": [
371
{
372
"data": {
373
"text/plain": [
374
"0.5"
375
]
376
},
377
"execution_count": 14,
378
"metadata": {},
379
"output_type": "execute_result"
380
}
381
],
382
"source": [
383
"a.imag"
384
]
385
},
386
{
387
"cell_type": "code",
388
"execution_count": 15,
389
"metadata": {
390
"run_control": {
391
"frozen": false,
392
"read_only": false
393
}
394
},
395
"outputs": [
396
{
397
"data": {
398
"text/plain": [
399
"complex"
400
]
401
},
402
"execution_count": 15,
403
"metadata": {},
404
"output_type": "execute_result"
405
}
406
],
407
"source": [
408
"type(1. + 0j )"
409
]
410
},
411
{
412
"cell_type": "markdown",
413
"metadata": {},
414
"source": [
415
"**布尔:**"
416
]
417
},
418
{
419
"cell_type": "code",
420
"execution_count": 16,
421
"metadata": {
422
"run_control": {
423
"frozen": false,
424
"read_only": false
425
}
426
},
427
"outputs": [
428
{
429
"data": {
430
"text/plain": [
431
"False"
432
]
433
},
434
"execution_count": 16,
435
"metadata": {},
436
"output_type": "execute_result"
437
}
438
],
439
"source": [
440
"3 > 4"
441
]
442
},
443
{
444
"cell_type": "code",
445
"execution_count": 17,
446
"metadata": {
447
"run_control": {
448
"frozen": false,
449
"read_only": false
450
}
451
},
452
"outputs": [
453
{
454
"data": {
455
"text/plain": [
456
"False"
457
]
458
},
459
"execution_count": 17,
460
"metadata": {},
461
"output_type": "execute_result"
462
}
463
],
464
"source": [
465
"test = (3 > 4)\n",
466
"test"
467
]
468
},
469
{
470
"cell_type": "code",
471
"execution_count": 18,
472
"metadata": {
473
"run_control": {
474
"frozen": false,
475
"read_only": false
476
}
477
},
478
"outputs": [
479
{
480
"data": {
481
"text/plain": [
482
"bool"
483
]
484
},
485
"execution_count": 18,
486
"metadata": {},
487
"output_type": "execute_result"
488
}
489
],
490
"source": [
491
"type(test)"
492
]
493
},
494
{
495
"cell_type": "markdown",
496
"metadata": {},
497
"source": [
498
"因此,Python shell可以代替你的口袋计算器,因为基本的代数操作符 +、-、\\*、/、%(模)都已经原生实现了。"
499
]
500
},
501
{
502
"cell_type": "code",
503
"execution_count": 19,
504
"metadata": {
505
"run_control": {
506
"frozen": false,
507
"read_only": false
508
}
509
},
510
"outputs": [
511
{
512
"data": {
513
"text/plain": [
514
"21.0"
515
]
516
},
517
"execution_count": 19,
518
"metadata": {},
519
"output_type": "execute_result"
520
}
521
],
522
"source": [
523
"7 * 3."
524
]
525
},
526
{
527
"cell_type": "code",
528
"execution_count": 20,
529
"metadata": {
530
"run_control": {
531
"frozen": false,
532
"read_only": false
533
}
534
},
535
"outputs": [
536
{
537
"data": {
538
"text/plain": [
539
"1024"
540
]
541
},
542
"execution_count": 20,
543
"metadata": {},
544
"output_type": "execute_result"
545
}
546
],
547
"source": [
548
"2**10"
549
]
550
},
551
{
552
"cell_type": "code",
553
"execution_count": 21,
554
"metadata": {
555
"run_control": {
556
"frozen": false,
557
"read_only": false
558
}
559
},
560
"outputs": [
561
{
562
"data": {
563
"text/plain": [
564
"2"
565
]
566
},
567
"execution_count": 21,
568
"metadata": {},
569
"output_type": "execute_result"
570
}
571
],
572
"source": [
573
"8 % 3"
574
]
575
},
576
{
577
"cell_type": "markdown",
578
"metadata": {},
579
"source": [
580
"类型转化(投射):"
581
]
582
},
583
{
584
"cell_type": "code",
585
"execution_count": 22,
586
"metadata": {
587
"run_control": {
588
"frozen": false,
589
"read_only": false
590
}
591
},
592
"outputs": [
593
{
594
"data": {
595
"text/plain": [
596
"1.0"
597
]
598
},
599
"execution_count": 22,
600
"metadata": {},
601
"output_type": "execute_result"
602
}
603
],
604
"source": [
605
"float(1)"
606
]
607
},
608
{
609
"cell_type": "markdown",
610
"metadata": {},
611
"source": [
612
"---\n",
613
"**注意**:整数相除"
614
]
615
},
616
{
617
"cell_type": "code",
618
"execution_count": 23,
619
"metadata": {
620
"run_control": {
621
"frozen": false,
622
"read_only": false
623
}
624
},
625
"outputs": [
626
{
627
"data": {
628
"text/plain": [
629
"1"
630
]
631
},
632
"execution_count": 23,
633
"metadata": {},
634
"output_type": "execute_result"
635
}
636
],
637
"source": [
638
"3 / 2"
639
]
640
},
641
{
642
"cell_type": "markdown",
643
"metadata": {},
644
"source": [
645
"**技巧**:使用浮点:"
646
]
647
},
648
{
649
"cell_type": "code",
650
"execution_count": 24,
651
"metadata": {
652
"run_control": {
653
"frozen": false,
654
"read_only": false
655
}
656
},
657
"outputs": [
658
{
659
"data": {
660
"text/plain": [
661
"1.5"
662
]
663
},
664
"execution_count": 24,
665
"metadata": {},
666
"output_type": "execute_result"
667
}
668
],
669
"source": [
670
"3 / 2."
671
]
672
},
673
{
674
"cell_type": "code",
675
"execution_count": 25,
676
"metadata": {
677
"run_control": {
678
"frozen": false,
679
"read_only": false
680
}
681
},
682
"outputs": [
683
{
684
"data": {
685
"text/plain": [
686
"1"
687
]
688
},
689
"execution_count": 25,
690
"metadata": {},
691
"output_type": "execute_result"
692
}
693
],
694
"source": [
695
"a = 3\n",
696
"b = 2\n",
697
"a / b"
698
]
699
},
700
{
701
"cell_type": "code",
702
"execution_count": 26,
703
"metadata": {
704
"run_control": {
705
"frozen": false,
706
"read_only": false
707
}
708
},
709
"outputs": [
710
{
711
"data": {
712
"text/plain": [
713
"1.5"
714
]
715
},
716
"execution_count": 26,
717
"metadata": {},
718
"output_type": "execute_result"
719
}
720
],
721
"source": [
722
"a / float(b)"
723
]
724
},
725
{
726
"cell_type": "markdown",
727
"metadata": {},
728
"source": [
729
"如果你明确想要整除,请使用//:"
730
]
731
},
732
{
733
"cell_type": "code",
734
"execution_count": 27,
735
"metadata": {
736
"run_control": {
737
"frozen": false,
738
"read_only": false
739
}
740
},
741
"outputs": [
742
{
743
"data": {
744
"text/plain": [
745
"1.0"
746
]
747
},
748
"execution_count": 27,
749
"metadata": {},
750
"output_type": "execute_result"
751
}
752
],
753
"source": [
754
"3.0 // 2"
755
]
756
},
757
{
758
"cell_type": "markdown",
759
"metadata": {},
760
"source": [
761
"Python3改变了除运算符行为。细节请看[python3porting](http://python3porting.com/preparing.html#use-instead-of-when-dividing-integers)网站.\n",
762
"\n",
763
"---"
764
]
765
},
766
{
767
"cell_type": "markdown",
768
"metadata": {},
769
"source": [
770
"### 1.2.2.2 容器\n",
771
"Python提供了许多有效的容器类型,其中存储了对象集合。\n",
772
"#### 1.2.2.2.1 列表\n",
773
"列表是一个有序的对象集合,对象可以有多种类型。例如:"
774
]
775
},
776
{
777
"cell_type": "code",
778
"execution_count": 28,
779
"metadata": {
780
"run_control": {
781
"frozen": false,
782
"read_only": false
783
}
784
},
785
"outputs": [
786
{
787
"data": {
788
"text/plain": [
789
"list"
790
]
791
},
792
"execution_count": 28,
793
"metadata": {},
794
"output_type": "execute_result"
795
}
796
],
797
"source": [
798
"L = ['red', 'blue', 'green', 'black', 'white']\n",
799
"type(L)"
800
]
801
},
802
{
803
"cell_type": "markdown",
804
"metadata": {},
805
"source": [
806
"索引:访问包含在列表中的单个对象:"
807
]
808
},
809
{
810
"cell_type": "code",
811
"execution_count": 29,
812
"metadata": {
813
"run_control": {
814
"frozen": false,
815
"read_only": false
816
}
817
},
818
"outputs": [
819
{
820
"data": {
821
"text/plain": [
822
"'green'"
823
]
824
},
825
"execution_count": 29,
826
"metadata": {},
827
"output_type": "execute_result"
828
}
829
],
830
"source": [
831
"L[2]"
832
]
833
},
834
{
835
"cell_type": "markdown",
836
"metadata": {},
837
"source": [
838
"使用负索引,从结尾开始计数:"
839
]
840
},
841
{
842
"cell_type": "code",
843
"execution_count": 30,
844
"metadata": {
845
"run_control": {
846
"frozen": false,
847
"read_only": false
848
}
849
},
850
"outputs": [
851
{
852
"data": {
853
"text/plain": [
854
"'white'"
855
]
856
},
857
"execution_count": 30,
858
"metadata": {},
859
"output_type": "execute_result"
860
}
861
],
862
"source": [
863
"L[-1]"
864
]
865
},
866
{
867
"cell_type": "code",
868
"execution_count": 31,
869
"metadata": {
870
"run_control": {
871
"frozen": false,
872
"read_only": false
873
}
874
},
875
"outputs": [
876
{
877
"data": {
878
"text/plain": [
879
"'black'"
880
]
881
},
882
"execution_count": 31,
883
"metadata": {},
884
"output_type": "execute_result"
885
}
886
],
887
"source": [
888
"L[-2]"
889
]
890
},
891
{
892
"cell_type": "markdown",
893
"metadata": {},
894
"source": [
895
"**注意:索引从0开始**(和C中一样),而不是1(像在Fortran或Matlab)!\n",
896
"\n",
897
"切片:获得规律分布元素的子列表:"
898
]
899
},
900
{
901
"cell_type": "code",
902
"execution_count": 32,
903
"metadata": {
904
"run_control": {
905
"frozen": false,
906
"read_only": false
907
}
908
},
909
"outputs": [
910
{
911
"data": {
912
"text/plain": [
913
"['red', 'blue', 'green', 'black', 'white']"
914
]
915
},
916
"execution_count": 32,
917
"metadata": {},
918
"output_type": "execute_result"
919
}
920
],
921
"source": [
922
"L"
923
]
924
},
925
{
926
"cell_type": "code",
927
"execution_count": 33,
928
"metadata": {
929
"run_control": {
930
"frozen": false,
931
"read_only": false
932
}
933
},
934
"outputs": [
935
{
936
"data": {
937
"text/plain": [
938
"['green', 'black']"
939
]
940
},
941
"execution_count": 33,
942
"metadata": {},
943
"output_type": "execute_result"
944
}
945
],
946
"source": [
947
"L[2:4]"
948
]
949
},
950
{
951
"cell_type": "markdown",
952
"metadata": {},
953
"source": [
954
"**注意**:L[start:stop]包含索引start<= i < stop的元素(i的范围从start到stop-1)。因此,L[start:stop]包含(stop-start)个元素。\n",
955
"\n",
956
"**切片语法**:```L[start:stop:stride]```\n",
957
"\n",
958
"所有切片参数都是可选的:"
959
]
960
},
961
{
962
"cell_type": "code",
963
"execution_count": 34,
964
"metadata": {
965
"run_control": {
966
"frozen": false,
967
"read_only": false
968
}
969
},
970
"outputs": [
971
{
972
"data": {
973
"text/plain": [
974
"['red', 'blue', 'green', 'black', 'white']"
975
]
976
},
977
"execution_count": 34,
978
"metadata": {},
979
"output_type": "execute_result"
980
}
981
],
982
"source": [
983
"L"
984
]
985
},
986
{
987
"cell_type": "code",
988
"execution_count": 35,
989
"metadata": {
990
"run_control": {
991
"frozen": false,
992
"read_only": false
993
}
994
},
995
"outputs": [
996
{
997
"data": {
998
"text/plain": [
999
"['black', 'white']"
1000
]
1001
},
1002
"execution_count": 35,
1003
"metadata": {},
1004
"output_type": "execute_result"
1005
}
1006
],
1007
"source": [
1008
"L[3:]"
1009
]
1010
},
1011
{
1012
"cell_type": "code",
1013
"execution_count": 36,
1014
"metadata": {
1015
"run_control": {
1016
"frozen": false,
1017
"read_only": false
1018
}
1019
},
1020
"outputs": [
1021
{
1022
"data": {
1023
"text/plain": [
1024
"['red', 'blue', 'green']"
1025
]
1026
},
1027
"execution_count": 36,
1028
"metadata": {},
1029
"output_type": "execute_result"
1030
}
1031
],
1032
"source": [
1033
"L[:3]"
1034
]
1035
},
1036
{
1037
"cell_type": "markdown",
1038
"metadata": {},
1039
"source": [
1040
"列表是可变对象,可以被改变:"
1041
]
1042
},
1043
{
1044
"cell_type": "code",
1045
"execution_count": 38,
1046
"metadata": {
1047
"run_control": {
1048
"frozen": false,
1049
"read_only": false
1050
}
1051
},
1052
"outputs": [
1053
{
1054
"data": {
1055
"text/plain": [
1056
"['yellow', 'blue', 'green', 'black', 'white']"
1057
]
1058
},
1059
"execution_count": 38,
1060
"metadata": {},
1061
"output_type": "execute_result"
1062
}
1063
],
1064
"source": [
1065
"L[0] = 'yellow'\n",
1066
"L "
1067
]
1068
},
1069
{
1070
"cell_type": "code",
1071
"execution_count": 39,
1072
"metadata": {
1073
"run_control": {
1074
"frozen": false,
1075
"read_only": false
1076
}
1077
},
1078
"outputs": [
1079
{
1080
"data": {
1081
"text/plain": [
1082
"['yellow', 'blue', 'gray', 'purple', 'white']"
1083
]
1084
},
1085
"execution_count": 39,
1086
"metadata": {},
1087
"output_type": "execute_result"
1088
}
1089
],
1090
"source": [
1091
"L[2:4] = ['gray', 'purple']\n",
1092
"L"
1093
]
1094
},
1095
{
1096
"cell_type": "markdown",
1097
"metadata": {},
1098
"source": [
1099
"**注:**一个列表的元素可以有不同的类型:"
1100
]
1101
},
1102
{
1103
"cell_type": "code",
1104
"execution_count": 40,
1105
"metadata": {
1106
"run_control": {
1107
"frozen": false,
1108
"read_only": false
1109
}
1110
},
1111
"outputs": [
1112
{
1113
"data": {
1114
"text/plain": [
1115
"[3, -200, 'hello']"
1116
]
1117
},
1118
"execution_count": 40,
1119
"metadata": {},
1120
"output_type": "execute_result"
1121
}
1122
],
1123
"source": [
1124
"L = [3, -200, 'hello']\n",
1125
"L"
1126
]
1127
},
1128
{
1129
"cell_type": "code",
1130
"execution_count": 41,
1131
"metadata": {
1132
"run_control": {
1133
"frozen": false,
1134
"read_only": false
1135
}
1136
},
1137
"outputs": [
1138
{
1139
"data": {
1140
"text/plain": [
1141
"(-200, 'hello')"
1142
]
1143
},
1144
"execution_count": 41,
1145
"metadata": {},
1146
"output_type": "execute_result"
1147
}
1148
],
1149
"source": [
1150
"L[1], L[2]"
1151
]
1152
},
1153
{
1154
"cell_type": "markdown",
1155
"metadata": {},
1156
"source": [
1157
"对于一个所有类型都相同的数值数据集合,使用Numpy模块提供的数组类型通常更有效。Numpy数组是包含固定大小项目的内存组块。使用Numpy数组,元素上的操作可以非常快速,因为元素均匀分布在内存上并且更多的操作是通过特殊的C函数而不是Python循环。\n",
1158
"\n",
1159
"Python提供了一大组函数来修改或查询列表。这里是一些例子,更多内容,请见:http://docs.python.org/tutorial/datastructures.html#more-on-lists\n",
1160
"\n",
1161
"添加和删除元素:"
1162
]
1163
},
1164
{
1165
"cell_type": "code",
1166
"execution_count": 42,
1167
"metadata": {
1168
"run_control": {
1169
"frozen": false,
1170
"read_only": false
1171
}
1172
},
1173
"outputs": [
1174
{
1175
"data": {
1176
"text/plain": [
1177
"['red', 'blue', 'green', 'black', 'white', 'pink']"
1178
]
1179
},
1180
"execution_count": 42,
1181
"metadata": {},
1182
"output_type": "execute_result"
1183
}
1184
],
1185
"source": [
1186
"L = ['red', 'blue', 'green', 'black', 'white']\n",
1187
"L.append('pink')\n",
1188
"L"
1189
]
1190
},
1191
{
1192
"cell_type": "code",
1193
"execution_count": 43,
1194
"metadata": {
1195
"run_control": {
1196
"frozen": false,
1197
"read_only": false
1198
}
1199
},
1200
"outputs": [
1201
{
1202
"data": {
1203
"text/plain": [
1204
"'pink'"
1205
]
1206
},
1207
"execution_count": 43,
1208
"metadata": {},
1209
"output_type": "execute_result"
1210
}
1211
],
1212
"source": [
1213
"L.pop() # 删除并返回最后一个项目"
1214
]
1215
},
1216
{
1217
"cell_type": "code",
1218
"execution_count": 44,
1219
"metadata": {
1220
"run_control": {
1221
"frozen": false,
1222
"read_only": false
1223
}
1224
},
1225
"outputs": [
1226
{
1227
"data": {
1228
"text/plain": [
1229
"['red', 'blue', 'green', 'black', 'white']"
1230
]
1231
},
1232
"execution_count": 44,
1233
"metadata": {},
1234
"output_type": "execute_result"
1235
}
1236
],
1237
"source": [
1238
"L"
1239
]
1240
},
1241
{
1242
"cell_type": "code",
1243
"execution_count": 45,
1244
"metadata": {
1245
"run_control": {
1246
"frozen": false,
1247
"read_only": false
1248
}
1249
},
1250
"outputs": [],
1251
"source": [
1252
"L.extend(['pink', 'purple']) # 扩展列表L,原地\n",
1253
"L"
1254
]
1255
},
1256
{
1257
"cell_type": "code",
1258
"execution_count": 46,
1259
"metadata": {
1260
"run_control": {
1261
"frozen": false,
1262
"read_only": false
1263
}
1264
},
1265
"outputs": [
1266
{
1267
"data": {
1268
"text/plain": [
1269
"['red', 'blue', 'green', 'black', 'white']"
1270
]
1271
},
1272
"execution_count": 46,
1273
"metadata": {},
1274
"output_type": "execute_result"
1275
}
1276
],
1277
"source": [
1278
"L = L[:-2]\n",
1279
"L"
1280
]
1281
},
1282
{
1283
"cell_type": "markdown",
1284
"metadata": {},
1285
"source": [
1286
"反转:"
1287
]
1288
},
1289
{
1290
"cell_type": "code",
1291
"execution_count": 47,
1292
"metadata": {
1293
"run_control": {
1294
"frozen": false,
1295
"read_only": false
1296
}
1297
},
1298
"outputs": [
1299
{
1300
"data": {
1301
"text/plain": [
1302
"['white', 'black', 'green', 'blue', 'red']"
1303
]
1304
},
1305
"execution_count": 47,
1306
"metadata": {},
1307
"output_type": "execute_result"
1308
}
1309
],
1310
"source": [
1311
"r = L[::-1]\n",
1312
"r"
1313
]
1314
},
1315
{
1316
"cell_type": "code",
1317
"execution_count": 48,
1318
"metadata": {
1319
"run_control": {
1320
"frozen": false,
1321
"read_only": false
1322
}
1323
},
1324
"outputs": [
1325
{
1326
"data": {
1327
"text/plain": [
1328
"['red', 'blue', 'green', 'black', 'white']"
1329
]
1330
},
1331
"execution_count": 48,
1332
"metadata": {},
1333
"output_type": "execute_result"
1334
}
1335
],
1336
"source": [
1337
"r2 = list(L)\n",
1338
"r2"
1339
]
1340
},
1341
{
1342
"cell_type": "code",
1343
"execution_count": 49,
1344
"metadata": {
1345
"run_control": {
1346
"frozen": false,
1347
"read_only": false
1348
}
1349
},
1350
"outputs": [
1351
{
1352
"data": {
1353
"text/plain": [
1354
"['white', 'black', 'green', 'blue', 'red']"
1355
]
1356
},
1357
"execution_count": 49,
1358
"metadata": {},
1359
"output_type": "execute_result"
1360
}
1361
],
1362
"source": [
1363
"r2.reverse() # 原对象\n",
1364
"r2"
1365
]
1366
},
1367
{
1368
"cell_type": "markdown",
1369
"metadata": {},
1370
"source": [
1371
"串联和重复列表:"
1372
]
1373
},
1374
{
1375
"cell_type": "code",
1376
"execution_count": 50,
1377
"metadata": {
1378
"run_control": {
1379
"frozen": false,
1380
"read_only": false
1381
}
1382
},
1383
"outputs": [
1384
{
1385
"data": {
1386
"text/plain": [
1387
"['white',\n",
1388
" 'black',\n",
1389
" 'green',\n",
1390
" 'blue',\n",
1391
" 'red',\n",
1392
" 'red',\n",
1393
" 'blue',\n",
1394
" 'green',\n",
1395
" 'black',\n",
1396
" 'white']"
1397
]
1398
},
1399
"execution_count": 50,
1400
"metadata": {},
1401
"output_type": "execute_result"
1402
}
1403
],
1404
"source": [
1405
"r + L"
1406
]
1407
},
1408
{
1409
"cell_type": "code",
1410
"execution_count": 51,
1411
"metadata": {
1412
"run_control": {
1413
"frozen": false,
1414
"read_only": false
1415
}
1416
},
1417
"outputs": [
1418
{
1419
"data": {
1420
"text/plain": [
1421
"['white',\n",
1422
" 'black',\n",
1423
" 'green',\n",
1424
" 'blue',\n",
1425
" 'red',\n",
1426
" 'white',\n",
1427
" 'black',\n",
1428
" 'green',\n",
1429
" 'blue',\n",
1430
" 'red']"
1431
]
1432
},
1433
"execution_count": 51,
1434
"metadata": {},
1435
"output_type": "execute_result"
1436
}
1437
],
1438
"source": [
1439
"r * 2"
1440
]
1441
},
1442
{
1443
"cell_type": "markdown",
1444
"metadata": {},
1445
"source": [
1446
"排序:"
1447
]
1448
},
1449
{
1450
"cell_type": "code",
1451
"execution_count": 52,
1452
"metadata": {
1453
"run_control": {
1454
"frozen": false,
1455
"read_only": false
1456
}
1457
},
1458
"outputs": [
1459
{
1460
"data": {
1461
"text/plain": [
1462
"['black', 'blue', 'green', 'red', 'white']"
1463
]
1464
},
1465
"execution_count": 52,
1466
"metadata": {},
1467
"output_type": "execute_result"
1468
}
1469
],
1470
"source": [
1471
"sorted(r) # 新对象"
1472
]
1473
},
1474
{
1475
"cell_type": "code",
1476
"execution_count": 53,
1477
"metadata": {
1478
"run_control": {
1479
"frozen": false,
1480
"read_only": false
1481
}
1482
},
1483
"outputs": [
1484
{
1485
"data": {
1486
"text/plain": [
1487
"['white', 'black', 'green', 'blue', 'red']"
1488
]
1489
},
1490
"execution_count": 53,
1491
"metadata": {},
1492
"output_type": "execute_result"
1493
}
1494
],
1495
"source": [
1496
"r"
1497
]
1498
},
1499
{
1500
"cell_type": "code",
1501
"execution_count": 55,
1502
"metadata": {
1503
"run_control": {
1504
"frozen": false,
1505
"read_only": false
1506
}
1507
},
1508
"outputs": [
1509
{
1510
"data": {
1511
"text/plain": [
1512
"['black', 'blue', 'green', 'red', 'white']"
1513
]
1514
},
1515
"execution_count": 55,
1516
"metadata": {},
1517
"output_type": "execute_result"
1518
}
1519
],
1520
"source": [
1521
"r.sort() # 原对象\n",
1522
"r"
1523
]
1524
},
1525
{
1526
"cell_type": "markdown",
1527
"metadata": {},
1528
"source": [
1529
"---\n",
1530
"**方法和面向对象编程**\n",
1531
"\n",
1532
"符号r.method() (即 r.append(3) and L.pop()) 是我们第一个关于面向对象编程的例子(OOP)。作为列表,对象r有可以以这种方式调用的方法函数。对于这篇教程不需要关于面向对象编程的更多知识,只需要理解这种符号。\n",
1533
"\n",
1534
"---\n",
1535
"**发现方法**:\n",
1536
"\n",
1537
"提醒:在IPython中:tab完成 (按tab)\n",
1538
"\n",
1539
"```python\n",
1540
"In [28]: r.\n",
1541
"r.__add__ r.__iadd__ r.__setattr__\n",
1542
"r.__class__ r.__imul__ r.__setitem__\n",
1543
"r.__contains__ r.__init__ r.__setslice__\n",
1544
"r.__delattr__ r.__iter__ r.__sizeof__\n",
1545
"r.__delitem__ r.__le__ r.__str__\n",
1546
"r.__delslice__ r.__len__ r.__subclasshook__\n",
1547
"r.__doc__ r.__lt__ r.append\n",
1548
"r.__eq__ r.__mul__ r.count\n",
1549
"r.__format__ r.__ne__ r.extend\n",
1550
"r.__ge__ r.__new__ r.index\n",
1551
"r.__getattribute__ r.__reduce__ r.insert\n",
1552
"r.__getitem__ r.__reduce_ex__ r.pop\n",
1553
"r.__getslice__ r.__repr__ r.remove\n",
1554
"r.__gt__ r.__reversed__ r.reverse\n",
1555
"r.__hash__ r.__rmul__ r.sort\n",
1556
"```\n",
1557
"\n",
1558
"\n",
1559
"\n",
1560
"\n",
1561
"\n",
1562
"\n",
1563
"\n",
1564
"\n",
1565
"\n",
1566
"\n",
1567
"\n",
1568
"\n",
1569
"\n",
1570
"\n",
1571
"\n",
1572
"\n",
1573
"\n",
1574
"\n",
1575
"\n",
1576
"\n",
1577
"\n",
1578
"\n",
1579
"\n"
1580
]
1581
},
1582
{
1583
"cell_type": "markdown",
1584
"metadata": {},
1585
"source": [
1586
"#### 1.2.2.2.2 字符\n",
1587
"\n",
1588
"不同的字符语法(单引号、双引号或三个引号):"
1589
]
1590
},
1591
{
1592
"cell_type": "code",
1593
"execution_count": 58,
1594
"metadata": {
1595
"run_control": {
1596
"frozen": false,
1597
"read_only": false
1598
}
1599
},
1600
"outputs": [
1601
{
1602
"ename": "SyntaxError",
1603
"evalue": "invalid syntax (, line 7)",
1604
"output_type": "error",
1605
"traceback": [
1606
"\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m7\u001b[0m\n\u001b[0;31m 'Hi, what's up?'\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
1607
]
1608
}
1609
],
1610
"source": [
1611
"s = 'Hello, how are you?'\n",
1612
"s = \"Hi, what's up\"\n",
1613
"s = '''Hello, \n",
1614
" how are you''' # 三个引号可以允许字符跨行\n",
1615
"s = \"\"\"Hi,\n",
1616
"what's up?\"\"\"\n",
1617
"'Hi, what's up?'"
1618
]
1619
},
1620
{
1621
"cell_type": "markdown",
1622
"metadata": {},
1623
"source": [
1624
"如果在字符中要是使用引号,那么应该嵌套使用,或者使用\"\\\"进行转义,否则会报错。\n",
1625
"\n",
1626
"换行的符号为 \\n,tab符号是\\t。\n",
1627
"\n",
1628
"字符也是类似与列表的结合。因此,也可以使用相同的语法和规则索引和切片。\n",
1629
"\n",
1630
"索引:"
1631
]
1632
},
1633
{
1634
"cell_type": "code",
1635
"execution_count": 59,
1636
"metadata": {
1637
"run_control": {
1638
"frozen": false,
1639
"read_only": false
1640
}
1641
},
1642
"outputs": [
1643
{
1644
"data": {
1645
"text/plain": [
1646
"'h'"
1647
]
1648
},
1649
"execution_count": 59,
1650
"metadata": {},
1651
"output_type": "execute_result"
1652
}
1653
],
1654
"source": [
1655
"a = \"hello\"\n",
1656
"a[0]"
1657
]
1658
},
1659
{
1660
"cell_type": "code",
1661
"execution_count": 60,
1662
"metadata": {
1663
"run_control": {
1664
"frozen": false,
1665
"read_only": false
1666
}
1667
},
1668
"outputs": [
1669
{
1670
"data": {
1671
"text/plain": [
1672
"'e'"
1673
]
1674
},
1675
"execution_count": 60,
1676
"metadata": {},
1677
"output_type": "execute_result"
1678
}
1679
],
1680
"source": [
1681
"a[1]"
1682
]
1683
},
1684
{
1685
"cell_type": "code",
1686
"execution_count": 61,
1687
"metadata": {
1688
"run_control": {
1689
"frozen": false,
1690
"read_only": false
1691
}
1692
},
1693
"outputs": [
1694
{
1695
"data": {
1696
"text/plain": [
1697
"'o'"
1698
]
1699
},
1700
"execution_count": 61,
1701
"metadata": {},
1702
"output_type": "execute_result"
1703
}
1704
],
1705
"source": [
1706
"a[-1]"
1707
]
1708
},
1709
{
1710
"cell_type": "markdown",
1711
"metadata": {},
1712
"source": [
1713
"(记住负索引从右侧开始计数。)\n",
1714
"\n",
1715
"切片:"
1716
]
1717
},
1718
{
1719
"cell_type": "code",
1720
"execution_count": 64,
1721
"metadata": {
1722
"run_control": {
1723
"frozen": false,
1724
"read_only": false
1725
}
1726
},
1727
"outputs": [
1728
{
1729
"data": {
1730
"text/plain": [
1731
"'lo,'"
1732
]
1733
},
1734
"execution_count": 64,
1735
"metadata": {},
1736
"output_type": "execute_result"
1737
}
1738
],
1739
"source": [
1740
"a = \"hello, world!\"\n",
1741
"a[3:6] # 第三到第六个(不包含)元素:元素3、4、5"
1742
]
1743
},
1744
{
1745
"cell_type": "code",
1746
"execution_count": 65,
1747
"metadata": {
1748
"run_control": {
1749
"frozen": false,
1750
"read_only": false
1751
}
1752
},
1753
"outputs": [
1754
{
1755
"data": {
1756
"text/plain": [
1757
"'lo o'"
1758
]
1759
},
1760
"execution_count": 65,
1761
"metadata": {},
1762
"output_type": "execute_result"
1763
}
1764
],
1765
"source": [
1766
"a[2:10:2] # 语法:a[开始:结束:步幅]"
1767
]
1768
},
1769
{
1770
"cell_type": "code",
1771
"execution_count": 66,
1772
"metadata": {
1773
"run_control": {
1774
"frozen": false,
1775
"read_only": false
1776
}
1777
},
1778
"outputs": [
1779
{
1780
"data": {
1781
"text/plain": [
1782
"'hl r!'"
1783
]
1784
},
1785
"execution_count": 66,
1786
"metadata": {},
1787
"output_type": "execute_result"
1788
}
1789
],
1790
"source": [
1791
"a[::3] # 从开始到结尾,每隔3个字母"
1792
]
1793
},
1794
{
1795
"cell_type": "markdown",
1796
"metadata": {},
1797
"source": [
1798
"重音符号和特殊字符也可以被处理为Unicode字符(请见 http://docs.python.org/tutorial/introduction.html#unicode-strings)。\n",
1799
"\n",
1800
"字符是**不可变**对象,不可能修改内容。不过可以从原始的字符中创建一个新的字符。"
1801
]
1802
},
1803
{
1804
"cell_type": "code",
1805
"execution_count": 68,
1806
"metadata": {
1807
"run_control": {
1808
"frozen": false,
1809
"read_only": false
1810
}
1811
},
1812
"outputs": [
1813
{
1814
"ename": "TypeError",
1815
"evalue": "'str' object does not support item assignment",
1816
"output_type": "error",
1817
"traceback": [
1818
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
1819
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"hello, world!\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'z'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1820
"\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
1821
]
1822
}
1823
],
1824
"source": [
1825
"a = \"hello, world!\"\n",
1826
"a[2] = 'z'"
1827
]
1828
},
1829
{
1830
"cell_type": "code",
1831
"execution_count": 69,
1832
"metadata": {
1833
"run_control": {
1834
"frozen": false,
1835
"read_only": false
1836
}
1837
},
1838
"outputs": [
1839
{
1840
"data": {
1841
"text/plain": [
1842
"'hezlo, world!'"
1843
]
1844
},
1845
"execution_count": 69,
1846
"metadata": {},
1847
"output_type": "execute_result"
1848
}
1849
],
1850
"source": [
1851
"a.replace('l', 'z', 1)"
1852
]
1853
},
1854
{
1855
"cell_type": "code",
1856
"execution_count": 70,
1857
"metadata": {
1858
"run_control": {
1859
"frozen": false,
1860
"read_only": false
1861
}
1862
},
1863
"outputs": [
1864
{
1865
"data": {
1866
"text/plain": [
1867
"'hezzo, worzd!'"
1868
]
1869
},
1870
"execution_count": 70,
1871
"metadata": {},
1872
"output_type": "execute_result"
1873
}
1874
],
1875
"source": [
1876
"a.replace('l', 'z')"
1877
]
1878
},
1879
{
1880
"cell_type": "markdown",
1881
"metadata": {},
1882
"source": [
1883
"字符有许多有用的方法,比如上面的a.replace。回忆一下a.面向对象的符号,并且使用tab完成或者help(str)来搜索新的方法。\n",
1884
"\n",
1885
"**更多内容** Python提供了操作的字符的高级可能性,看一下模式或格式。感兴趣的读者请参考:http://docs.python.org/library/stdtypes.html#string-methods 和 http://docs.python.org/library/string.html#new-string-formatting。\n",
1886
"\n",
1887
"字符格式:"
1888
]
1889
},
1890
{
1891
"cell_type": "code",
1892
"execution_count": 71,
1893
"metadata": {
1894
"run_control": {
1895
"frozen": false,
1896
"read_only": false
1897
}
1898
},
1899
"outputs": [
1900
{
1901
"data": {
1902
"text/plain": [
1903
"'An integer: 1; a float: 0.100000; another string: string'"
1904
]
1905
},
1906
"execution_count": 71,
1907
"metadata": {},
1908
"output_type": "execute_result"
1909
}
1910
],
1911
"source": [
1912
"'An integer: %i; a float: %f; another string: %s' % (1, 0.1, 'string')"
1913
]
1914
},
1915
{
1916
"cell_type": "code",
1917
"execution_count": 72,
1918
"metadata": {
1919
"run_control": {
1920
"frozen": false,
1921
"read_only": false
1922
}
1923
},
1924
"outputs": [
1925
{
1926
"data": {
1927
"text/plain": [
1928
"'processing_of_dataset_102.txt'"
1929
]
1930
},
1931
"execution_count": 72,
1932
"metadata": {},
1933
"output_type": "execute_result"
1934
}
1935
],
1936
"source": [
1937
"i = 102\n",
1938
"filename = 'processing_of_dataset_%d.txt' % i\n",
1939
"filename"
1940
]
1941
},
1942
{
1943
"cell_type": "markdown",
1944
"metadata": {},
1945
"source": [
1946
"#### 1.2.2.2.3. Dictionaries"
1947
]
1948
},
1949
{
1950
"cell_type": "markdown",
1951
"metadata": {},
1952
"source": [
1953
"字典本质上是一个**映射键值**的高效表格。它是一个**无序**的容器"
1954
]
1955
},
1956
{
1957
"cell_type": "code",
1958
"execution_count": 74,
1959
"metadata": {
1960
"run_control": {
1961
"frozen": false,
1962
"read_only": false
1963
}
1964
},
1965
"outputs": [
1966
{
1967
"data": {
1968
"text/plain": [
1969
"{'emmanuelle': 5752, 'francis': 5915, 'sebastian': 5578}"
1970
]
1971
},
1972
"execution_count": 74,
1973
"metadata": {},
1974
"output_type": "execute_result"
1975
}
1976
],
1977
"source": [
1978
"tel = {'emmanuelle': 5752, 'sebastian': 5578}\n",
1979
"tel['francis'] = 5915\n",
1980
"tel"
1981
]
1982
},
1983
{
1984
"cell_type": "code",
1985
"execution_count": 75,
1986
"metadata": {
1987
"run_control": {
1988
"frozen": false,
1989
"read_only": false
1990
}
1991
},
1992
"outputs": [
1993
{
1994
"data": {
1995
"text/plain": [
1996
"5578"
1997
]
1998
},
1999
"execution_count": 75,
2000
"metadata": {},
2001
"output_type": "execute_result"
2002
}
2003
],
2004
"source": [
2005
"tel['sebastian']"
2006
]
2007
},
2008
{
2009
"cell_type": "code",
2010
"execution_count": 76,
2011
"metadata": {
2012
"run_control": {
2013
"frozen": false,
2014
"read_only": false
2015
}
2016
},
2017
"outputs": [
2018
{
2019
"data": {
2020
"text/plain": [
2021
"['sebastian', 'francis', 'emmanuelle']"
2022
]
2023
},
2024
"execution_count": 76,
2025
"metadata": {},
2026
"output_type": "execute_result"
2027
}
2028
],
2029
"source": [
2030
"tel.keys()"
2031
]
2032
},
2033
{
2034
"cell_type": "code",
2035
"execution_count": 77,
2036
"metadata": {
2037
"run_control": {
2038
"frozen": false,
2039
"read_only": false
2040
}
2041
},
2042
"outputs": [
2043
{
2044
"data": {
2045
"text/plain": [
2046
"[5578, 5915, 5752]"
2047
]
2048
},
2049
"execution_count": 77,
2050
"metadata": {},
2051
"output_type": "execute_result"
2052
}
2053
],
2054
"source": [
2055
"tel.values()"
2056
]
2057
},
2058
{
2059
"cell_type": "markdown",
2060
"metadata": {},
2061
"source": [
2062
"它可以方便的以名字(日期的字符和名称等)存储和获取值。更多信息见 http://docs.python.org/tutorial/datastructures.html#dictionaries。\n",
2063
"\n",
2064
"一个字典的键(代表值)可以有不同的类型:"
2065
]
2066
},
2067
{
2068
"cell_type": "code",
2069
"execution_count": 78,
2070
"metadata": {
2071
"run_control": {
2072
"frozen": false,
2073
"read_only": false
2074
}
2075
},
2076
"outputs": [
2077
{
2078
"data": {
2079
"text/plain": [
2080
"{3: 'hello', 'a': 1, 'b': 2}"
2081
]
2082
},
2083
"execution_count": 78,
2084
"metadata": {},
2085
"output_type": "execute_result"
2086
}
2087
],
2088
"source": [
2089
"d = {'a':1, 'b':2, 3:'hello'}\n",
2090
"d"
2091
]
2092
},
2093
{
2094
"cell_type": "markdown",
2095
"metadata": {},
2096
"source": [
2097
"#### 1.2.2.2.4. More container types\n",
2098
"\n",
2099
"**元组**\n",
2100
"\n",
2101
"元组本质上是不可变列表。元组的元素用括号包起来,或者只是用逗号分割:"
2102
]
2103
},
2104
{
2105
"cell_type": "code",
2106
"execution_count": 79,
2107
"metadata": {
2108
"run_control": {
2109
"frozen": false,
2110
"read_only": false
2111
}
2112
},
2113
"outputs": [
2114
{
2115
"data": {
2116
"text/plain": [
2117
"12345"
2118
]
2119
},
2120
"execution_count": 79,
2121
"metadata": {},
2122
"output_type": "execute_result"
2123
}
2124
],
2125
"source": [
2126
"t = 12345, 54321, 'hello!'\n",
2127
"t[0]"
2128
]
2129
},
2130
{
2131
"cell_type": "code",
2132
"execution_count": 80,
2133
"metadata": {
2134
"run_control": {
2135
"frozen": false,
2136
"read_only": false
2137
}
2138
},
2139
"outputs": [
2140
{
2141
"data": {
2142
"text/plain": [
2143
"(12345, 54321, 'hello!')"
2144
]
2145
},
2146
"execution_count": 80,
2147
"metadata": {},
2148
"output_type": "execute_result"
2149
}
2150
],
2151
"source": [
2152
"t"
2153
]
2154
},
2155
{
2156
"cell_type": "code",
2157
"execution_count": 81,
2158
"metadata": {
2159
"run_control": {
2160
"frozen": false,
2161
"read_only": false
2162
}
2163
},
2164
"outputs": [],
2165
"source": [
2166
"u = (0, 2)"
2167
]
2168
},
2169
{
2170
"cell_type": "markdown",
2171
"metadata": {},
2172
"source": [
2173
"**集合**:无序,惟一项目:"
2174
]
2175
},
2176
{
2177
"cell_type": "code",
2178
"execution_count": 82,
2179
"metadata": {
2180
"run_control": {
2181
"frozen": false,
2182
"read_only": false
2183
}
2184
},
2185
"outputs": [
2186
{
2187
"data": {
2188
"text/plain": [
2189
"{'a', 'b', 'c'}"
2190
]
2191
},
2192
"execution_count": 82,
2193
"metadata": {},
2194
"output_type": "execute_result"
2195
}
2196
],
2197
"source": [
2198
"s = set(('a', 'b', 'c', 'a'))\n",
2199
"s"
2200
]
2201
},
2202
{
2203
"cell_type": "code",
2204
"execution_count": 83,
2205
"metadata": {
2206
"run_control": {
2207
"frozen": false,
2208
"read_only": false
2209
}
2210
},
2211
"outputs": [
2212
{
2213
"data": {
2214
"text/plain": [
2215
"{'c'}"
2216
]
2217
},
2218
"execution_count": 83,
2219
"metadata": {},
2220
"output_type": "execute_result"
2221
}
2222
],
2223
"source": [
2224
"s.difference(('a', 'b'))"
2225
]
2226
},
2227
{
2228
"cell_type": "markdown",
2229
"metadata": {},
2230
"source": [
2231
"#### 1.2.2.3. 赋值运算"
2232
]
2233
},
2234
{
2235
"cell_type": "markdown",
2236
"metadata": {},
2237
"source": [
2238
"[Python类库参考:](http://docs.python.org/reference/simple_stmts.html#assignment-statements)\n",
2239
"\n",
2240
">赋值语句被用于(重)绑定名称与值,以及修改可变对象的项目或属性。\n",
2241
"\n",
2242
"简单来说,它这样工作(简单赋值):\n",
2243
"\n",
2244
"1. 右侧表达式被评估,创建或获得产生的对象\n",
2245
"\n",
2246
"2. 左侧的名字被赋值或绑定到右侧的对象\n",
2247
"\n",
2248
"需要注意的事情:\n",
2249
"\n",
2250
"- 单个对象可以有多个绑定的名称:"
2251
]
2252
},
2253
{
2254
"cell_type": "code",
2255
"execution_count": 84,
2256
"metadata": {
2257
"run_control": {
2258
"frozen": false,
2259
"read_only": false
2260
}
2261
},
2262
"outputs": [
2263
{
2264
"data": {
2265
"text/plain": [
2266
"[1, 2, 3]"
2267
]
2268
},
2269
"execution_count": 84,
2270
"metadata": {},
2271
"output_type": "execute_result"
2272
}
2273
],
2274
"source": [
2275
"a = [1, 2, 3]\n",
2276
"b = a\n",
2277
"a"
2278
]
2279
},
2280
{
2281
"cell_type": "code",
2282
"execution_count": 85,
2283
"metadata": {
2284
"run_control": {
2285
"frozen": false,
2286
"read_only": false
2287
}
2288
},
2289
"outputs": [
2290
{
2291
"data": {
2292
"text/plain": [
2293
"[1, 2, 3]"
2294
]
2295
},
2296
"execution_count": 85,
2297
"metadata": {},
2298
"output_type": "execute_result"
2299
}
2300
],
2301
"source": [
2302
"b"
2303
]
2304
},
2305
{
2306
"cell_type": "code",
2307
"execution_count": 86,
2308
"metadata": {
2309
"run_control": {
2310
"frozen": false,
2311
"read_only": false
2312
}
2313
},
2314
"outputs": [
2315
{
2316
"data": {
2317
"text/plain": [
2318
"True"
2319
]
2320
},
2321
"execution_count": 86,
2322
"metadata": {},
2323
"output_type": "execute_result"
2324
}
2325
],
2326
"source": [
2327
"a is b"
2328
]
2329
},
2330
{
2331
"cell_type": "code",
2332
"execution_count": 87,
2333
"metadata": {
2334
"run_control": {
2335
"frozen": false,
2336
"read_only": false
2337
}
2338
},
2339
"outputs": [
2340
{
2341
"data": {
2342
"text/plain": [
2343
"[1, 'hi!', 3]"
2344
]
2345
},
2346
"execution_count": 87,
2347
"metadata": {},
2348
"output_type": "execute_result"
2349
}
2350
],
2351
"source": [
2352
"b[1] = 'hi!'\n",
2353
"a"
2354
]
2355
},
2356
{
2357
"cell_type": "markdown",
2358
"metadata": {},
2359
"source": [
2360
"- 要在**原地**改变列表,请使用索引或切片:"
2361
]
2362
},
2363
{
2364
"cell_type": "code",
2365
"execution_count": 88,
2366
"metadata": {
2367
"run_control": {
2368
"frozen": false,
2369
"read_only": false
2370
}
2371
},
2372
"outputs": [
2373
{
2374
"data": {
2375
"text/plain": [
2376
"[1, 2, 3]"
2377
]
2378
},
2379
"execution_count": 88,
2380
"metadata": {},
2381
"output_type": "execute_result"
2382
}
2383
],
2384
"source": [
2385
"a = [1, 2, 3]\n",
2386
"a"
2387
]
2388
},
2389
{
2390
"cell_type": "code",
2391
"execution_count": 89,
2392
"metadata": {
2393
"run_control": {
2394
"frozen": false,
2395
"read_only": false
2396
}
2397
},
2398
"outputs": [
2399
{
2400
"data": {
2401
"text/plain": [
2402
"['a', 'b', 'c']"
2403
]
2404
},
2405
"execution_count": 89,
2406
"metadata": {},
2407
"output_type": "execute_result"
2408
}
2409
],
2410
"source": [
2411
"a = ['a', 'b', 'c'] # 创建另一个对象\n",
2412
"a"
2413
]
2414
},
2415
{
2416
"cell_type": "code",
2417
"execution_count": 90,
2418
"metadata": {
2419
"run_control": {
2420
"frozen": false,
2421
"read_only": false
2422
}
2423
},
2424
"outputs": [
2425
{
2426
"data": {
2427
"text/plain": [
2428
"4394695640"
2429
]
2430
},
2431
"execution_count": 90,
2432
"metadata": {},
2433
"output_type": "execute_result"
2434
}
2435
],
2436
"source": [
2437
"id(a)"
2438
]
2439
},
2440
{
2441
"cell_type": "code",
2442
"execution_count": 91,
2443
"metadata": {
2444
"run_control": {
2445
"frozen": false,
2446
"read_only": false
2447
}
2448
},
2449
"outputs": [
2450
{
2451
"data": {
2452
"text/plain": [
2453
"[1, 2, 3]"
2454
]
2455
},
2456
"execution_count": 91,
2457
"metadata": {},
2458
"output_type": "execute_result"
2459
}
2460
],
2461
"source": [
2462
"a[:] = [1, 2, 3] # 在原地修改对象\n",
2463
"a"
2464
]
2465
},
2466
{
2467
"cell_type": "code",
2468
"execution_count": 92,
2469
"metadata": {
2470
"run_control": {
2471
"frozen": false,
2472
"read_only": false
2473
}
2474
},
2475
"outputs": [
2476
{
2477
"data": {
2478
"text/plain": [
2479
"4394695640"
2480
]
2481
},
2482
"execution_count": 92,
2483
"metadata": {},
2484
"output_type": "execute_result"
2485
}
2486
],
2487
"source": [
2488
"id(a)"
2489
]
2490
},
2491
{
2492
"cell_type": "markdown",
2493
"metadata": {},
2494
"source": [
2495
"与上一个id相同,你的可能有所不同..."
2496
]
2497
},
2498
{
2499
"cell_type": "markdown",
2500
"metadata": {},
2501
"source": [
2502
"- 这里的关键观点是可变 vs. 不可变\n",
2503
"\n",
2504
" - 可变对象可以在原地修改\n",
2505
" - 不可变对象一旦被创建就不可修改\n",
2506
"\n",
2507
"**更多内容**在David M. Beazley的文章[Python中的类型和对象](http://www.informit.com/articles/article.aspx?p=453682)中也可以找到关于以上问题非常不错的详尽解释。\n",
2508
"## 1.2.3 流程控制\n",
2509
"\n",
2510
"控制代码执行顺序。\n",
2511
"\n",
2512
"### 1.2.3.1 if/elif/else"
2513
]
2514
},
2515
{
2516
"cell_type": "code",
2517
"execution_count": 93,
2518
"metadata": {
2519
"run_control": {
2520
"frozen": false,
2521
"read_only": false
2522
}
2523
},
2524
"outputs": [
2525
{
2526
"name": "stdout",
2527
"output_type": "stream",
2528
"text": [
2529
"Obvious!\n"
2530
]
2531
}
2532
],
2533
"source": [
2534
"if 2**2 == 4:\n",
2535
" print 'Obvious!'"
2536
]
2537
},
2538
{
2539
"cell_type": "markdown",
2540
"metadata": {},
2541
"source": [
2542
"**代码块用缩进限定**\n",
2543
"\n",
2544
"---\n",
2545
"\n",
2546
"**小技巧**:在你的Python解释器内输入下列行,并且注意保持缩进深度。IPython shell会在一行的 : 符号后自动增加缩进,如果要减少缩进,向左侧移动4个空格使用后退键。按两次回车键离开逻辑块。\n",
2547
"\n",
2548
"---"
2549
]
2550
},
2551
{
2552
"cell_type": "code",
2553
"execution_count": 96,
2554
"metadata": {
2555
"run_control": {
2556
"frozen": false,
2557
"read_only": false
2558
}
2559
},
2560
"outputs": [
2561
{
2562
"name": "stdout",
2563
"output_type": "stream",
2564
"text": [
2565
"A lot\n"
2566
]
2567
}
2568
],
2569
"source": [
2570
"a = 10\n",
2571
"if a == 1:\n",
2572
" print(1)\n",
2573
"elif a == 2:\n",
2574
" print(2)\n",
2575
"else:\n",
2576
" print('A lot')"
2577
]
2578
},
2579
{
2580
"cell_type": "markdown",
2581
"metadata": {},
2582
"source": [
2583
"在脚本中也是强制缩进的。作为练习,在condition.py脚本中以相同的缩进重新输入之前几行,并在IPython中用```run condition.py```执行脚本。"
2584
]
2585
},
2586
{
2587
"cell_type": "markdown",
2588
"metadata": {},
2589
"source": [
2590
"### 1.2.3.2 for/range\n",
2591
"\n",
2592
"在索引上迭代:"
2593
]
2594
},
2595
{
2596
"cell_type": "code",
2597
"execution_count": 97,
2598
"metadata": {
2599
"run_control": {
2600
"frozen": false,
2601
"read_only": false
2602
}
2603
},
2604
"outputs": [
2605
{
2606
"name": "stdout",
2607
"output_type": "stream",
2608
"text": [
2609
"0\n",
2610
"1\n",
2611
"2\n",
2612
"3\n"
2613
]
2614
}
2615
],
2616
"source": [
2617
"for i in range(4):\n",
2618
" print(i)"
2619
]
2620
},
2621
{
2622
"cell_type": "markdown",
2623
"metadata": {},
2624
"source": [
2625
"但是最经常使用,也更易读的是在值上迭代:"
2626
]
2627
},
2628
{
2629
"cell_type": "code",
2630
"execution_count": 98,
2631
"metadata": {
2632
"run_control": {
2633
"frozen": false,
2634
"read_only": false
2635
}
2636
},
2637
"outputs": [
2638
{
2639
"name": "stdout",
2640
"output_type": "stream",
2641
"text": [
2642
"Python is cool\n",
2643
"Python is powerful\n",
2644
"Python is readable\n"
2645
]
2646
}
2647
],
2648
"source": [
2649
"for word in ('cool', 'powerful', 'readable'):\n",
2650
" print('Python is %s' % word)"
2651
]
2652
},
2653
{
2654
"cell_type": "markdown",
2655
"metadata": {},
2656
"source": [
2657
"### 1.2.3.3 while/break/continue\n",
2658
"\n",
2659
"典型的C式While循环(Mandelbrot问题):"
2660
]
2661
},
2662
{
2663
"cell_type": "code",
2664
"execution_count": 13,
2665
"metadata": {
2666
"run_control": {
2667
"frozen": false,
2668
"read_only": false
2669
}
2670
},
2671
"outputs": [
2672
{
2673
"data": {
2674
"text/plain": [
2675
"(-134+352j)"
2676
]
2677
},
2678
"execution_count": 13,
2679
"metadata": {},
2680
"output_type": "execute_result"
2681
}
2682
],
2683
"source": [
2684
"z = 1 + 1j\n",
2685
"while abs(z) < 100: \n",
2686
" z = z**2 + 1\n",
2687
"z"
2688
]
2689
},
2690
{
2691
"cell_type": "markdown",
2692
"metadata": {},
2693
"source": [
2694
"**更高级的功能**\n",
2695
"\n",
2696
"break 跳出for/while循环:"
2697
]
2698
},
2699
{
2700
"cell_type": "code",
2701
"execution_count": 103,
2702
"metadata": {
2703
"run_control": {
2704
"frozen": false,
2705
"read_only": false
2706
}
2707
},
2708
"outputs": [
2709
{
2710
"name": "stdout",
2711
"output_type": "stream",
2712
"text": [
2713
"(1+2j)\n",
2714
"(-2+4j)\n",
2715
"(-11-16j)\n",
2716
"(-134+352j)\n"
2717
]
2718
}
2719
],
2720
"source": [
2721
"z = 1 + 1j\n",
2722
"while abs(z) < 100:\n",
2723
" if z.imag == 0:\n",
2724
" break\n",
2725
" z = z**2 + 1\n",
2726
" print z"
2727
]
2728
},
2729
{
2730
"cell_type": "markdown",
2731
"metadata": {},
2732
"source": [
2733
"continue 继续下一个循环迭代:"
2734
]
2735
},
2736
{
2737
"cell_type": "code",
2738
"execution_count": 101,
2739
"metadata": {
2740
"run_control": {
2741
"frozen": false,
2742
"read_only": false
2743
}
2744
},
2745
"outputs": [
2746
{
2747
"name": "stdout",
2748
"output_type": "stream",
2749
"text": [
2750
"1.0\n",
2751
"0.5\n",
2752
"0.25\n"
2753
]
2754
}
2755
],
2756
"source": [
2757
"a = [1, 0, 2, 4]\n",
2758
"for element in a:\n",
2759
" if element == 0:\n",
2760
" continue\n",
2761
" print 1. / element"
2762
]
2763
},
2764
{
2765
"cell_type": "markdown",
2766
"metadata": {},
2767
"source": [
2768
"### 1.2.3.4 条件表达式\n",
2769
"\n",
2770
"**if [OBJECT]:**\n",
2771
"\n",
2772
"评估为False:\n",
2773
" - 任何等于0的数字 (0、0.0、0+0j)\n",
2774
" - 空容器(列表、元组、集合、字典, ...)\n",
2775
" - False,None\n",
2776
"\n",
2777
"评估为True:\n",
2778
" - 任何其他的东西\n",
2779
" \n",
2780
"**a == b:**\n",
2781
"\n",
2782
"判断逻辑是否相等:"
2783
]
2784
},
2785
{
2786
"cell_type": "code",
2787
"execution_count": 1,
2788
"metadata": {
2789
"run_control": {
2790
"frozen": false,
2791
"read_only": false
2792
}
2793
},
2794
"outputs": [
2795
{
2796
"data": {
2797
"text/plain": [
2798
"True"
2799
]
2800
},
2801
"execution_count": 1,
2802
"metadata": {},
2803
"output_type": "execute_result"
2804
}
2805
],
2806
"source": [
2807
"1 == 1"
2808
]
2809
},
2810
{
2811
"cell_type": "markdown",
2812
"metadata": {},
2813
"source": [
2814
"**a is b:**\n",
2815
"\n",
2816
"测试同一性:两边是相同的对象:"
2817
]
2818
},
2819
{
2820
"cell_type": "code",
2821
"execution_count": 2,
2822
"metadata": {
2823
"run_control": {
2824
"frozen": false,
2825
"read_only": false
2826
}
2827
},
2828
"outputs": [
2829
{
2830
"data": {
2831
"text/plain": [
2832
"True"
2833
]
2834
},
2835
"execution_count": 2,
2836
"metadata": {},
2837
"output_type": "execute_result"
2838
}
2839
],
2840
"source": [
2841
"1 is 1"
2842
]
2843
},
2844
{
2845
"cell_type": "code",
2846
"execution_count": 3,
2847
"metadata": {
2848
"run_control": {
2849
"frozen": false,
2850
"read_only": false
2851
}
2852
},
2853
"outputs": [
2854
{
2855
"data": {
2856
"text/plain": [
2857
"True"
2858
]
2859
},
2860
"execution_count": 3,
2861
"metadata": {},
2862
"output_type": "execute_result"
2863
}
2864
],
2865
"source": [
2866
"a = 1\n",
2867
"b = 1\n",
2868
"a is b"
2869
]
2870
},
2871
{
2872
"cell_type": "markdown",
2873
"metadata": {},
2874
"source": [
2875
"**a in b:**\n",
2876
"\n",
2877
"对于任何集合b:b包含a"
2878
]
2879
},
2880
{
2881
"cell_type": "code",
2882
"execution_count": 11,
2883
"metadata": {
2884
"run_control": {
2885
"frozen": false,
2886
"read_only": false
2887
}
2888
},
2889
"outputs": [
2890
{
2891
"data": {
2892
"text/plain": [
2893
"True"
2894
]
2895
},
2896
"execution_count": 11,
2897
"metadata": {},
2898
"output_type": "execute_result"
2899
}
2900
],
2901
"source": [
2902
"b = [1, 2, 3]\n",
2903
"2 in b"
2904
]
2905
},
2906
{
2907
"cell_type": "code",
2908
"execution_count": 12,
2909
"metadata": {
2910
"run_control": {
2911
"frozen": false,
2912
"read_only": false
2913
}
2914
},
2915
"outputs": [
2916
{
2917
"data": {
2918
"text/plain": [
2919
"False"
2920
]
2921
},
2922
"execution_count": 12,
2923
"metadata": {},
2924
"output_type": "execute_result"
2925
}
2926
],
2927
"source": [
2928
"5 in b"
2929
]
2930
},
2931
{
2932
"cell_type": "markdown",
2933
"metadata": {},
2934
"source": [
2935
"如果b是字典,这个语法测试a是否是b的一个键。\n",
2936
"\n",
2937
"### 1.2.3.5. 高级循环\n",
2938
"\n",
2939
"#### 1.2.3.5.1 序列循环\n",
2940
"\n",
2941
"你可以在任何序列上进行循环(字符、列表、字典的键,文件的行...):"
2942
]
2943
},