-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.xml
2050 lines (1891 loc) · 208 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>Hello World</title>
<url>/2024/06/26/hello-world/</url>
<content><![CDATA[<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>
<p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>
]]></content>
</entry>
<entry>
<title>'chenge-first-blog'</title>
<url>/2024/06/26/chenge-first-blog/</url>
<content><![CDATA[]]></content>
</entry>
<entry>
<title>InterProScan</title>
<url>/2024/06/28/InterProScan/</url>
<content><![CDATA[<h1 id="conda安装"><a href="#conda安装" class="headerlink" title="conda安装"></a>conda安装</h1><h2 id="安装:"><a href="#安装:" class="headerlink" title="安装:"></a>安装:</h2><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">conda install -c bioconda interproscan</span><br><span class="line"><span class="comment">#https://anaconda.org/bioconda/interproscan</span></span><br></pre></td></tr></table></figure>
<h2 id="下载、检查md5文件并解压数据库"><a href="#下载、检查md5文件并解压数据库" class="headerlink" title="下载、检查md5文件并解压数据库"></a>下载、检查md5文件并解压数据库</h2><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment"># get the md5 of the databases</span></span><br><span class="line">wget https://ftp.ebi.ac.uk/pub/databases/interpro/iprscan/5/5.62-94.0/interproscan-5.62-94.0-64-bit.tar.gz.md5</span><br><span class="line"><span class="comment"># get the databases (with core because much faster to download)</span></span><br><span class="line">wget https://ftp.ebi.ac.uk/pub/databases/interpro/iprscan/5/5.62-94.0/interproscan-5.62-94.0-64-bit.tar.gz</span><br><span class="line"><span class="comment"># checksum</span></span><br><span class="line"><span class="built_in">md5sum</span> -c interproscan-5.62-94.0-64-bit.tar.gz.md5</span><br><span class="line"><span class="comment"># untar gz</span></span><br><span class="line">tar xvzf interproscan-5.62-94.0-64-bit.tar.gz</span><br></pre></td></tr></table></figure>
<h2 id="更改数据库"><a href="#更改数据库" class="headerlink" title="更改数据库"></a>更改数据库</h2><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment"># remove old DB 根据conda安装路径的数据库删除</span></span><br><span class="line"><span class="built_in">rm</span> -rf /home/yangzy/miniconda3/share/InterProScan/data/</span><br><span class="line"><span class="comment"># copy past the new db 把</span></span><br><span class="line">cp-r /home/yangzy/miniconda3/envs/interproscan/interproscan-5.62-94.0/data /home/yangzy/miniconda3/share/InterProScan/</span><br><span class="line"><span class="comment">#将wget到的数据库复制过去</span></span><br></pre></td></tr></table></figure>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">./interproscan.sh -i test_proteins.fasta -f tsv</span><br></pre></td></tr></table></figure>
<p>如果没有报错,则表示InterProScan能正常运行了<br>一般常用的参数有这些:<br> -appl,–applications 用于指定使用Interpro中哪些数据库,默认全部数据库<br> -b,–output-file-base 用于指定输出文件的路径or文件夹,默认是输入文件的路径<br> -f,–formats 用于指定输出文件的后缀,蛋白序列默认输出TSV, XML and GFF3<br> -i,–input 输入文件,一般要为fasta格式,不要带有其他特殊符号</p>
<h1 id="TBtools"><a href="#TBtools" class="headerlink" title="TBtools"></a>TBtools</h1><p>使用TBtools中的Batch Protein Annotation in InterPro插件<br>参考:<a href="https://mp.weixin.qq.com/s/4tIlpUekwHRlPe4yySFnFA">https://mp.weixin.qq.com/s/4tIlpUekwHRlPe4yySFnFA</a></p>
<p>上传1w条序列文件后,在分析到两千多条时显示超时错误:</p>
<p>咨询插件创作者得到反馈:<br>不建议一次1w 太多了 对interprot网站造成一定负担 可以少量多次 比如1000条分10次 另外一次量过多也容易产生ip被封的风险</p>
]]></content>
<tags>
<tag>软件</tag>
</tags>
</entry>
<entry>
<title>Python入门基础课</title>
<url>/2024/07/02/Python%E5%85%A5%E9%97%A8%E5%9F%BA%E7%A1%80%E8%AF%BE/</url>
<content><![CDATA[<h1 id="Python基础语法学习"><a href="#Python基础语法学习" class="headerlink" title="Python基础语法学习"></a>Python基础语法学习</h1><h2 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h2><ul>
<li><p>注释:</p>
<p> 使用 #表示注释行,一般井号在行首,也可以在行的中间,那么#后面的为注释信息</p>
<p> 带有 #号的行python解释器会忽略</p>
</li>
<li><p>缩进</p>
<p> 使用缩进表示层次关系,或者区分不同的代码块,不像perl语言用{} </p>
<p> 约定使用4个空格缩进</p>
</li>
<li><p>续行</p>
<p> 在行尾使用 \</p>
<p> 如果使用各种括号,认为括号内是一个整体,括号内部跨行不用 \</p>
</li>
</ul>
<p>Python示例代码:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">'''</span><br><span class="line">Description: This script is used to onvert U in fasta sequence file to T</span><br><span class="line">Date: 2019</span><br><span class="line">Auther: omicsgene</span><br><span class="line">'''</span><br><span class="line"></span><br><span class="line">from Bio.Seq import Seq</span><br><span class="line">from Bio import SeqIO</span><br><span class="line">from Bio.Alphabet import IUPAC</span><br><span class="line">from Bio.SeqRecord import SeqRecord</span><br><span class="line">import sys, os, argparse, os.path,re,math,time</span><br><span class="line">#参数设置</span><br><span class="line">parser = argparse.ArgumentParser(description='This script is \</span><br><span class="line"> used to onvert U in fasta sequence file to T')</span><br><span class="line">parser.add_argument('-f','--fasta',help='Please fasta file',required=True)</span><br><span class="line">parser.add_argument('-o','--out_dir',help='Please input complete out_put directory path',</span><br><span class="line"> default = os.getcwd(),</span><br><span class="line"> required=False)</span><br><span class="line"></span><br><span class="line">parser.add_argument('-n','--name',default ='demo_seq',required=False,</span><br><span class="line"> help='Please specify the output, demo_seq')</span><br><span class="line">################################################################################</span><br><span class="line">#读入参数,初始化路径</span><br><span class="line">args = parser.parse_args()</span><br><span class="line">dout=''</span><br><span class="line">if os.path.exists(args.out_dir):</span><br><span class="line"> dout=os.path.abspath(args.out_dir)</span><br><span class="line">else:</span><br><span class="line"> os.mkdir(args.out_dir)</span><br><span class="line"> dout=os.path.abspath(args.out_dir)</span><br><span class="line"></span><br><span class="line">output_handle = open(dout+'/'+args.name+'.fa', "w")</span><br><span class="line">#循环处理序列将U转换成T,然后输出</span><br><span class="line">for rec in SeqIO.parse(args.fasta, "fasta"):</span><br><span class="line"> seq=rec.seq</span><br><span class="line"> seq=str(seq.upper())</span><br><span class="line"> seq=seq.replace("U", "T")</span><br><span class="line"> seq_r = SeqRecord(Seq(seq,alphabet = IUPAC.IUPACAmbiguousDNA()), </span><br><span class="line"> id=rec.id,</span><br><span class="line"> description=rec.description)</span><br><span class="line"> SeqIO.write(seq_r, output_handle, "fasta")</span><br><span class="line">output_handle.close()</span><br></pre></td></tr></table></figure>
<h2 id="Python-标准数据类型和变量"><a href="#Python-标准数据类型和变量" class="headerlink" title="Python 标准数据类型和变量"></a>Python 标准数据类型和变量</h2><h4 id="数字:"><a href="#数字:" class="headerlink" title="数字:"></a>数字:</h4><ul>
<li>整数 integer: 11,223,-23 </li>
<li>布尔值 bool :True False</li>
<li>浮点数 float: 1.2 、 1.46 、 1.44e10 、 -1.6e-66</li>
<li>复数: 1+2j</li>
</ul>
<h4 id="字符串-string:"><a href="#字符串-string:" class="headerlink" title="字符串 string:"></a>字符串 string:</h4><ul>
<li>使用 ‘ “ 单引号或者双引号引用的字符</li>
<li>‘’’ 和 “”” 可以跨行,可以在其中任意的使用单双引号</li>
</ul>
<h4 id="空值"><a href="#空值" class="headerlink" title="空值"></a>空值</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">None</span><br></pre></td></tr></table></figure>
<p>空值是Python里一个特殊的值,用None表示。None不能理解为0,因为0是有意义的,而None是一个特殊的空值。</p>
<h4 id="变量:"><a href="#变量:" class="headerlink" title="变量:"></a>变量:</h4><p>变量:</p>
<p>和初中代数的方程变量是一致的,只是在计算机程序中,变量不仅可以是数字,还可以是任意数据类型。</p>
<p>变量名:</p>
<ol>
<li>必须是大小写英文字母、数字和 _ 的组合,且不能用数字开头。例如,可将变量命名为message_1,但不能将其命名为1_message。</li>
<li>变量名不能包含空格,但可使用下划线来分隔其中的单词。例如,变量名greeting_message可行,但变量名greeting message会引发错误。</li>
<li>不要将Python关键字和函数名用作变量名,即不要使用Python保留用于特殊用途的单词,如print。</li>
<li>变量名应既简短又具有描述性,让人一看就知道里面代表的是什么数据。例如,name比n好,student_name比s_n好,name_length比length_of_persons_name好。</li>
<li>慎用小写字母l和大写字母O,因给他们可能被人错看成数字1和0;</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">a = 123 # a是整数</span><br><span class="line">print(a)</span><br><span class="line">a = 'ABC' # a变为字符串</span><br><span class="line">print(a)</span><br></pre></td></tr></table></figure>
<h4 id="变量的理解"><a href="#变量的理解" class="headerlink" title="变量的理解"></a>变量的理解</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">a = 'abc'</span><br><span class="line">b = a</span><br><span class="line">a = 'xyz'</span><br><span class="line">print(b)</span><br></pre></td></tr></table></figure>
<p>1: 执行a = ‘abc’,解释器创建了字符串’abc’和变量a,并把a指向’abc’:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">graph LR</span><br><span class="line">a-->abc</span><br></pre></td></tr></table></figure>
<p>2:执行b = a,解释器创建了变量b,并把b指向a指向的字符串’abc’:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">graph LR</span><br><span class="line">a-->abc</span><br><span class="line">b-->abc</span><br></pre></td></tr></table></figure>
<p>3:执行a = ‘xyz’,解释器创建了字符串’xyz’,并把a的指向改为’xyz’,但b并没有更改:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">graph LR</span><br><span class="line">a-->xyz</span><br><span class="line">b-->abc</span><br></pre></td></tr></table></figure>
<h2 id="Python-标准数据的操作"><a href="#Python-标准数据的操作" class="headerlink" title="Python 标准数据的操作"></a>Python 标准数据的操作</h2><h4 id="数字操作运算"><a href="#数字操作运算" class="headerlink" title="数字操作运算"></a>数字操作运算</h4><ul>
<li>加减乘除 + - * /</li>
<li>求余运算 % 求幂运算 **</li>
<li>自然除/ 结果是浮点数,整除//。注意:在2.x中/和//都是整除。</li>
</ul>
<p>注意:字符串可以相加和相乘</p>
<h4 id="字符串中特殊字符的转义:"><a href="#字符串中特殊字符的转义:" class="headerlink" title="字符串中特殊字符的转义:\"></a>字符串中特殊字符的转义:\</h4><ul>
<li>例如要在字符串中输入一个换行符: <figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">"hello python\n"</span><br></pre></td></tr></table></figure></li>
<li>其他常见特殊意义的转义:<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">\\ \t \r \n \' \"</span><br></pre></td></tr></table></figure></li>
<li>字符前缀 r</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">print(r'my name is \n \"lucy\"')</span><br></pre></td></tr></table></figure>
<p>练习:<br>打印以下变量</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">x = 122223</span><br><span class="line">y = 45116e-789</span><br><span class="line">a1 = 'Hello, world'</span><br><span class="line">a2 = 'Hello, \'Lily\''</span><br><span class="line">a3 = r'Hello, "Mike"'</span><br><span class="line">a4 = r'''Hello,</span><br><span class="line">omicsclass!'''</span><br></pre></td></tr></table></figure>
<h4 id="字符串格式化输出"><a href="#字符串格式化输出" class="headerlink" title="字符串格式化输出"></a>字符串格式化输出</h4><ul>
<li>%运算符格式化字符串</li>
<li>%s表示用字符串替换,%d表示用整数替换,有几个%?占位符,后面就跟几个变量或者值,顺序要对应好。如果只有一个%?,括号可以省略。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">print('Age: %s. Gender: %s' % (25, True))</span><br><span class="line">print('growth rate: %d %%' % 7)</span><br><span class="line">print('%2d-%02d' % (3, 1))</span><br><span class="line">print('%.2f' % 3.1415926)</span><br></pre></td></tr></table></figure>
<h4 id="关于字符串操作的一些方法"><a href="#关于字符串操作的一些方法" class="headerlink" title="关于字符串操作的一些方法"></a>关于字符串操作的一些方法</h4><p>==重点掌握:split,strip,join==</p>
<p>方法的调用用 . 操作符</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>strip、rstrip、lstrip</td>
<td>去除空白符。相当于对各个元素执行x.strip()</td>
</tr>
<tr>
<td>split</td>
<td>通过指定的分隔符将字符串拆分为一组子串</td>
</tr>
<tr>
<td>join</td>
<td>将字符串用作连接其他字符串序列的分隔符</td>
</tr>
<tr>
<td>count</td>
<td>返回子串在字符串中出现次数</td>
</tr>
<tr>
<td>endswith、startswith</td>
<td>如果字符串以某个后缀结尾(或开头),则返回True</td>
</tr>
<tr>
<td>lower、upper</td>
<td>分别将字母字符转换为小写或大写</td>
</tr>
<tr>
<td>replace</td>
<td>用另一个字符串替换指定子串</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>ljust、rjust</td>
<td>用空格(或其他字符)填充字符串的空白侧以返回符合最低宽度的字符串</td>
</tr>
<tr>
<td>index</td>
<td>如果在字符串中找到子串,则返回子串第一个字符所在的位置。如果没有找到,则引发ValueError</td>
</tr>
<tr>
<td>find</td>
<td>如果在字符串中找到子串,则返回第一个发现的子串第一个字符所在的位置。如果没有找到,则返回-1</td>
</tr>
<tr>
<td>rfind</td>
<td>如果在字符串中找到子串,则返回最后一个发现的子串的第一个字符所在的位置。如果没有找到,则返回-1</td>
</tr>
</tbody></table>
<p>示例代码:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">s = 'A,T,C,G,A\n'</span><br><span class="line">print(s)</span><br><span class="line">print(s,end="")</span><br><span class="line">print(s.strip())</span><br><span class="line"></span><br><span class="line">s = 'A,T,C,G,A'</span><br><span class="line">s.split(',')</span><br><span class="line"></span><br><span class="line">"\t".join(["A","T","T"])</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">s = 'A,T,C,G,A'</span><br><span class="line">s.replace('A','T')</span><br><span class="line">s.count(',')</span><br><span class="line">s.index('c')</span><br><span class="line">s.find('c')</span><br><span class="line">s.endwith("A")</span><br><span class="line">s.lower()</span><br><span class="line"></span><br><span class="line">#函数</span><br><span class="line">len(s)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>注意:</strong> 调用函数或者方法,一定要跟()</p>
<p>==<strong>小知识</strong>==<br>Python中函数和方法的区别:</p>
<p>在Python中,<strong>函数</strong>(function) 和<strong>方法</strong>(method) 是有区别的:</p>
<ul>
<li><p>所处的位置不同:函数是直接写在文件中而不是class中,方法是只能写在class中。</p>
</li>
<li><p>调用的方式不同:函数可以直接调用,例如:len() print()等等,方法是在类中,一般用. 来调用,如: 前面学习的:lst.index()</p>
</li>
</ul>
<h2 id="python中的列表list和元组tuple"><a href="#python中的列表list和元组tuple" class="headerlink" title="python中的列表list和元组tuple"></a>python中的列表list和元组tuple</h2><h3 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h3><p><strong>list 列表</strong></p>
<ul>
<li>list是一种有序的集合,可以随时添加和删除其中的元素。</li>
<li>元素的类型可以是任意对象(数字,字符串,逻辑值,列表,对象等等)</li>
<li>使用[]表示列表</li>
</ul>
<p><strong>tuple 元组</strong></p>
<ul>
<li>tuple:另一种有序列表,tuple和list非常类似,但是tuple一旦初始化就不能修改。</li>
<li>使用()表示元组</li>
</ul>
<p>list的创建与初始化:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">mylst=[]</span><br><span class="line">mylst=[1,3,"a","xy",[11,22]]</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>tuple的创建:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">classmates = ('Michael', 'Bob', 'Tracy')</span><br><span class="line">t = ('a', 'b', ['A', 'B'])</span><br></pre></td></tr></table></figure>
<h3 id="列表list相关的操作-函数-方法"><a href="#列表list相关的操作-函数-方法" class="headerlink" title="列表list相关的操作&函数&方法"></a>列表list相关的操作&函数&方法</h3><h3 id="列表的查询:"><a href="#列表的查询:" class="headerlink" title="列表的查询:"></a>列表的查询:</h3><h5 id="支持索引用中括号:"><a href="#支持索引用中括号:" class="headerlink" title="支持索引用中括号:[]"></a>支持索引用中括号:[]</h5><ul>
<li>正索引:从左到右,从0开始</li>
<li>负索引:从右往左,从-1开始</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">lst=["A","T","A","a",22,[333,444]]</span><br><span class="line"></span><br><span class="line">lst[1]</span><br><span class="line">lst[0]</span><br><span class="line">lst[-1]</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h5 id="列表切片"><a href="#列表切片" class="headerlink" title="列表切片"></a>列表切片</h5><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">lst=["A","T","A","a",22,[333,444]]</span><br><span class="line">lst[0:3]</span><br><span class="line">#表示,从索引0开始取,直到索引3为止,但不包括索引3。即索引0,1,2,正好是3个元素。</span><br><span class="line">#如果第一个索引是0,还可以省略:</span><br><span class="line">lst[:3]</span><br><span class="line"></span><br><span class="line">lst[-2:-1]</span><br><span class="line">lst[-2:]</span><br></pre></td></tr></table></figure>
<h5 id="注意多维"><a href="#注意多维" class="headerlink" title="注意多维[][]"></a>注意多维[][]</h5><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">lst=["A","T","A","a",22,[333,444]]</span><br><span class="line">lst[5][0]</span><br></pre></td></tr></table></figure>
<h3 id="列表元素修改"><a href="#列表元素修改" class="headerlink" title="列表元素修改"></a>列表元素修改</h3><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">lst=["A","T","A","a",22,[333,444]]</span><br><span class="line">lst[5][0]=333</span><br><span class="line"></span><br><span class="line">lst[0]="omicsclass"</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="列表相关方法"><a href="#列表相关方法" class="headerlink" title="列表相关方法:"></a>列表相关方法:</h3><p>重点掌握方法:==append extend sort== </p>
<table>
<thead>
<tr>
<th>方法</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>list.append(x)</td>
<td>在列表末尾添加新的对象</td>
</tr>
<tr>
<td>list.sort(key=None, reverse=False)</td>
<td>对原列表进行排序</td>
</tr>
<tr>
<td>list.extend(iterable)</td>
<td>在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)</td>
</tr>
<tr>
<td>list.count(x)</td>
<td>统计某个元素在列表中出现的次数</td>
</tr>
<tr>
<td>list.index(x[, start[, end]])</td>
<td>从列表中找出某个值第一个匹配项的索引位置</td>
</tr>
<tr>
<td>list.insert(i, x)</td>
<td>将对象插入列表</td>
</tr>
<tr>
<td>list.pop([i])</td>
<td>移除列表中的一个元素(默认最后一个元素),并且返回该元素的值</td>
</tr>
<tr>
<td>list.remove(x)</td>
<td>移除列表中某个值的第一个匹配项</td>
</tr>
<tr>
<td>list.reverse()</td>
<td>反向列表中元素</td>
</tr>
</tbody></table>
<h3 id="示例代码"><a href="#示例代码" class="headerlink" title="示例代码"></a>示例代码</h3><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">lst=["A","T","A","a",22,[333,444]]</span><br><span class="line">#列表结尾追加元素</span><br><span class="line">lst.append("omicsgene") </span><br><span class="line"></span><br><span class="line">#将可迭代的对象元素追加进来;</span><br><span class="line">lst.extend(["omicsgene","omicsclass"]) </span><br><span class="line"></span><br><span class="line">#在指定的索引处插入元素</span><br><span class="line">lst.insert(1,"omicsclass") </span><br><span class="line"></span><br><span class="line">#从左到右查询第一个匹配的元素删除</span><br><span class="line">lst.remove("A") </span><br><span class="line"></span><br><span class="line">#不指定索引,就从列表结尾删除元素,并返回该元素</span><br><span class="line">#也可以指定索引弹出;</span><br><span class="line">lst.pop()</span><br><span class="line">lst.pop(2)</span><br><span class="line"></span><br><span class="line">#反转list,会修改原列表</span><br><span class="line"></span><br><span class="line">lst.reverse()</span><br></pre></td></tr></table></figure>
<h3 id="列表相关的函数"><a href="#列表相关的函数" class="headerlink" title="列表相关的函数"></a>列表相关的函数</h3><table>
<thead>
<tr>
<th>函数</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>len(list)</td>
<td>列表元素个数</td>
</tr>
<tr>
<td>list(iterable)</td>
<td>将可迭代对象转换为列表</td>
</tr>
<tr>
<td>max(list)</td>
<td>返回列表元素最大值</td>
</tr>
<tr>
<td>min(list)</td>
<td>返回列表元素最小值</td>
</tr>
</tbody></table>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">a=[11,44,5,55,33,31,62,42]</span><br><span class="line">len(a)</span><br><span class="line">max(a)</span><br><span class="line">min(b)</span><br><span class="line"></span><br><span class="line">list([11,44,5,55,33,31,62,42])</span><br><span class="line">list(range(10))</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="列表的排序"><a href="#列表的排序" class="headerlink" title="列表的排序"></a>列表的排序</h3><p>有全局函数sorted,和列表自带方法sort</p>
<p><strong>list.sort(key=None, reverse=False)</strong></p>
<ul>
<li>key 指定带有一个参数的函数或者方法,用于从每个列表元素中提取比较键(例如key=str.lower)。对应于列表中每一项的键会被计算一次,然后在整个排序过程中使用。默认值None表示直接对列表项排序而不计算一个单独的键值。</li>
<li>reverse – 排序规则,reverse = True 降序 , reverse = False 升序(默认)。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">## 1、最简单的排序</span><br><span class="line">l = [5,2,3,1,4 ]</span><br><span class="line">l.sort()</span><br><span class="line">print(l) </span><br><span class="line"></span><br><span class="line">#反序</span><br><span class="line">l.sort(reverse=True)</span><br><span class="line">print(l) </span><br><span class="line"></span><br><span class="line">##2、字符串排序</span><br><span class="line">StrList = ['Fast', 'Smooth', 'fast', 'isb', 'isa', 'smooth']</span><br><span class="line">#一般字典序排列,但是大写在前,小写在后!!</span><br><span class="line">StrList.sort()</span><br><span class="line">print(StrList) </span><br><span class="line"></span><br><span class="line">##2.2忽略大小写,按abcd顺序</span><br><span class="line">StrList.sort(key=str.lower)</span><br><span class="line">print(StrList) </span><br><span class="line"></span><br><span class="line">##2.3按照字符串长度排序</span><br><span class="line">StrList.sort(key=len)</span><br><span class="line">print(StrList)</span><br><span class="line"></span><br><span class="line">#一起使用两个参数</span><br><span class="line">StrList.sort(key=len, reverse=True)</span><br><span class="line">print(StrList) </span><br></pre></td></tr></table></figure>
<p><strong>sorted(iterable, key=None, reverse=False)</strong></p>
<ul>
<li>iterable – 可迭代对象。 例如:列表 range对象 字符串等</li>
<li>key – 指定带有单个参数的函数,用于从 iterable 的每个元素中提取用于比较的键 (例如 key=str.lower)。 默认值为 None (直接比较元素)。</li>
<li>reverse – 排序规则,reverse = True 降序 , reverse = False 升序(默认)。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">a = [5,7,6,3,4,1,2]</span><br><span class="line">b = sorted(a) # 保留原列表</span><br><span class="line">print(b)</span><br><span class="line">b = sorted(a,reverse=True) </span><br><span class="line">print(b)</span><br><span class="line">a = ['Fast', 'Smooth', 'fast', 'isb', 'isa', 'smooth']</span><br><span class="line">b = sorted(a,key=str.lower,reverse=False) </span><br><span class="line">print(b)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>sort 与 sorted 区别:</strong></p>
<ul>
<li><p>调用方式不同:<br>sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。</p>
</li>
<li><p>返回对象不同:<br>list 的 sort 方法是在原来的列表上操作,无返回值(None),而函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。</p>
</li>
</ul>
<h2 id="python中的字典dict和集合set"><a href="#python中的字典dict和集合set" class="headerlink" title="python中的字典dict和集合set"></a>python中的字典dict和集合set</h2><ul>
<li>dict 字典</li>
</ul>
<p>dict全称dictionary,使用键-值(key-value)存储,具有极快的查找速度。类似perl里面的hash。</p>
<h3 id="字典的创建"><a href="#字典的创建" class="headerlink" title="字典的创建"></a>字典的创建</h3><p>字典的每个键值 key=>value对 用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中 ,格式如下所示:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">dict = {'a': 1, 'b': 2, 'b': '3'}</span><br></pre></td></tr></table></figure>
<h3 id="字典的特性:"><a href="#字典的特性:" class="headerlink" title="字典的特性:"></a>字典的特性:</h3><ol>
<li>键必须不可变,所以可以用数字,字符串或元组充当,所以用列表就不行(列表是可变类型):</li>
<li>值可以没有限制地取任何python对象,既可以是标准的对象,也可以是用户定义的。</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">d = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'} </span><br><span class="line"></span><br><span class="line">#会报错</span><br><span class="line">d = {['Name']: 'Zara', 'Age': 7}</span><br></pre></td></tr></table></figure>
<h3 id="修改字典"><a href="#修改字典" class="headerlink" title="修改字典"></a>修改字典</h3><p>向字典中 添加新内容的方法是增加新的键/值对,修改或删除已有键/值对:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">d = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}</span><br><span class="line">d['Name'] #取值</span><br><span class="line">d['Age'] = 18 # 更新</span><br><span class="line">d['School'] = "omicsclass" # 添加</span><br><span class="line"> </span><br><span class="line">#取值打印</span><br><span class="line">print( "d['Age']: ", d['Age'])</span><br><span class="line">print("d['School']: ", d['School'])</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 删除字典元素</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">d = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}</span><br><span class="line"> </span><br><span class="line">del d['Name'] # 删除键是'Name'的条目</span><br><span class="line">d.clear() # 清空字典所有条目</span><br><span class="line">del d # 删除字典</span><br><span class="line"> </span><br><span class="line">print("d['Age']: ", d['Age'])</span><br><span class="line">print("d['School']: ", d['School'])</span><br></pre></td></tr></table></figure>
<h3 id="字典内置方法"><a href="#字典内置方法" class="headerlink" title="字典内置方法"></a>字典内置方法</h3><table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>dict.keys()</td>
<td>以列表返回一个字典所有的键</td>
</tr>
<tr>
<td>dict.values()</td>
<td>以列表返回字典中的所有值</td>
</tr>
<tr>
<td>dict.items()</td>
<td>以列表返回可遍历的(键, 值) 元组数组</td>
</tr>
<tr>
<td>dict.get(key, default=None)</td>
<td>返回指定键的值,如果值不在字典中返回default值</td>
</tr>
</tbody></table>
<p>判断字典key是否存在</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">#使用格式:</span><br><span class="line">#key in dict</span><br><span class="line">#key not in dict</span><br><span class="line"></span><br><span class="line">d = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}</span><br><span class="line">'Name' in d</span><br><span class="line">'Name' not in d</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h3><ul>
<li>set 集合</li>
</ul>
<p>set 集合是一个无序的不重复元素序列。是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。</p>
<ul>
<li>集合创建</li>
</ul>
<p>可以使用大括号 { } 或者 set() 函数创建集合。</p>
<p>注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。</p>
<h4 id="集合的创建与运算"><a href="#集合的创建与运算" class="headerlink" title="集合的创建与运算"></a>集合的创建与运算</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">genes = {'gene1', 'gene1', 'gene2', 'gene3', 'gene4', 'gene5'}</span><br><span class="line"></span><br><span class="line">genes = set(['gene1', 'gene1', 'gene2', 'gene3', 'gene4', 'gene5'])</span><br><span class="line"></span><br><span class="line">print(genes) </span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#集合运算与判断</span><br><span class="line">'gene1' in genes </span><br><span class="line"></span><br><span class="line">'gene11' in genes</span><br><span class="line"></span><br><span class="line">#字符有列表特性,所以可以直接set </span><br><span class="line">a = set('omicsgene')</span><br><span class="line">b = set('omicsclass')</span><br><span class="line"></span><br><span class="line"># 集合a中包含而集合b中不包含的元素</span><br><span class="line">a - b </span><br><span class="line"># 集合a或b中包含的所有元素</span><br><span class="line">a | b </span><br><span class="line"># 集合a和b中都包含了的元素</span><br><span class="line">a & b </span><br><span class="line"># 不同时包含于a和b的元素</span><br><span class="line">a ^ b </span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="集合常用操作方法"><a href="#集合常用操作方法" class="headerlink" title="集合常用操作方法"></a>集合常用操作方法</h3><table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>set.add(x)</td>
<td>参数作为一个元素添加到原set集合</td>
</tr>
<tr>
<td>set.update(s)</td>
<td>可以添加多个元素,且参数可以是列表,元组,字典等</td>
</tr>
<tr>
<td>set.remove(x)</td>
<td>依据值删除,不存在会抛出异常KeyError</td>
</tr>
<tr>
<td>set.discard(x)</td>
<td>依据值删除,不存在不会报错</td>
</tr>
<tr>
<td>set.clear()</td>
<td>清空原set</td>
</tr>
</tbody></table>
<p>代码示例:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}</span><br><span class="line">basket.add(("aa","dd"))</span><br><span class="line">basket.update("dcc")</span><br><span class="line">print(basket)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">basket.remove("apple")</span><br><span class="line">basket.remove("apple1")</span><br><span class="line">basket.discard("apple1")</span><br><span class="line"></span><br><span class="line">basket.clear()</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="数据类型及类型转换"><a href="#数据类型及类型转换" class="headerlink" title="数据类型及类型转换"></a>数据类型及类型转换</h2><p>Python3 支持 的标准数据类型总结</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>类名</th>
</tr>
</thead>
<tbody><tr>
<td>Number(数字)</td>
<td>int、float、bool、complex(复数)</td>
</tr>
<tr>
<td>String(字符串)</td>
<td>str</td>
</tr>
<tr>
<td>List(列表)</td>
<td>list</td>
</tr>
<tr>
<td>Tuple(元组)</td>
<td>tuple</td>
</tr>
<tr>
<td>Set(集合)</td>
<td>set</td>
</tr>
<tr>
<td>Dictionary(字典)</td>
<td>dict</td>
</tr>
</tbody></table>
<p>Python3 的六个标准数据类型中:</p>
<ul>
<li>不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组);</li>
<li>可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#批量赋值</span><br><span class="line">a, b, c, d ,e= 20, 5.5, "ATGC", True, 4+3j</span><br><span class="line"></span><br><span class="line">#获得数据类型</span><br><span class="line">print(type(a), type(b), type(c), type(d), type(e))</span><br><span class="line"></span><br><span class="line">#判断数据类型</span><br><span class="line">a=1</span><br><span class="line">isinstance(a, int)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>类型转换函数:</p>
<table>
<thead>
<tr>
<th>函数</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>int(x [,base])</td>
<td>将x转换为一个整数</td>
</tr>
<tr>
<td>float(x)</td>
<td>将x转换到一个浮点数</td>
</tr>
<tr>
<td>str(x)</td>
<td>将对象 x 转换为字符串</td>
</tr>
<tr>
<td>list(s)</td>
<td>将序列 s 转换为一个列表</td>
</tr>
<tr>
<td>set(s)</td>
<td>转换为可变集合</td>
</tr>
<tr>
<td>tuple(s)</td>
<td>将序列 s 转换为一个元组</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>dict(d)</td>
<td>创建一个字典。d 必须是一个 (key, value)元组序列。</td>
</tr>
<tr>
<td>complex(real [,imag])</td>
<td>创建一个复数</td>
</tr>
<tr>
<td>repr(x)</td>
<td>将对象 x 转换为表达式字符串</td>
</tr>
<tr>
<td>eval(str)</td>
<td>用来计算在字符串中的有效Python表达式,并返回一个对象</td>
</tr>
<tr>
<td>frozenset(s)</td>
<td>转换为不可变集合</td>
</tr>
<tr>
<td>chr(x)</td>
<td>将一个整数转换为一个字符</td>
</tr>
<tr>
<td>ord(x)</td>
<td>将一个字符转换为它的整数值</td>
</tr>
<tr>
<td>hex(x)</td>
<td>将一个整数转换为一个十六进制字符串</td>
</tr>
<tr>
<td>oct(x)</td>
<td>将一个整数转换为一个八进制字符串</td>
</tr>
</tbody></table>
<h2 id="python中语句-if、for、while"><a href="#python中语句-if、for、while" class="headerlink" title="python中语句(if、for、while)"></a>python中语句(if、for、while)</h2><h3 id="if-语句-做判断"><a href="#if-语句-做判断" class="headerlink" title="if 语句 做判断"></a>if 语句 做判断</h3><p>计算机之所以能做很多自动化的任务,因为它可以自己做条件判断。</p>
<h4 id="python中常见比较判断运算符"><a href="#python中常见比较判断运算符" class="headerlink" title="python中常见比较判断运算符"></a>python中常见比较判断运算符</h4><p>==、 != 、 < 、> 、 >= 、 <=、in、not in</p>
<p>注意:in、not in : 判断值是否存在列表中或者字典的键中</p>
<h5 id="示例代码:"><a href="#示例代码:" class="headerlink" title="示例代码:"></a>示例代码:</h5><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#简单的判断</span><br><span class="line">age = 20</span><br><span class="line">if age >= 18:</span><br><span class="line"> print('your age is', age)</span><br><span class="line"> print('adult')</span><br><span class="line"></span><br><span class="line">l=['Fast', 'Smooth', 'fast', 'isb', 'isa', 'smooth']</span><br><span class="line">s = {'gene1', 'gene1', 'gene2', 'gene3', 'gene4', 'gene5'}</span><br><span class="line">d = {'gene1':122, 'gene2':2212, 'gene3':3121, 'gene4':2323, 'gene5':2543}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if 'Fast' not in l:</span><br><span class="line"> print(True)</span><br><span class="line"></span><br><span class="line">#else 语句</span><br><span class="line"> </span><br><span class="line">age = 3</span><br><span class="line">if age >= 18:</span><br><span class="line"> print('your age is', age)</span><br><span class="line"> print('adult')</span><br><span class="line">else:</span><br><span class="line"> print('your age is', age)</span><br><span class="line"> print('teenager')</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#elif语句 </span><br><span class="line">age = 3</span><br><span class="line">if age >= 18:</span><br><span class="line"> print('adult')</span><br><span class="line">elif age >= 6:</span><br><span class="line"> print('teenager')</span><br><span class="line">else:</span><br><span class="line"> print('kid')</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#判断条件依次执行,有一个为真就跳出</span><br><span class="line">age = 66</span><br><span class="line">if age >= 18:</span><br><span class="line"> print('adult')</span><br><span class="line">elif age >= 6:</span><br><span class="line"> print('teenager')</span><br><span class="line">elif age >= 60:</span><br><span class="line"> print('old people')</span><br><span class="line">else:</span><br><span class="line"> print('kid')</span><br></pre></td></tr></table></figure>
<h4 id="逻辑运算符-增加判断条件"><a href="#逻辑运算符-增加判断条件" class="headerlink" title="逻辑运算符 增加判断条件"></a>逻辑运算符 增加判断条件</h4><ul>
<li>与 、或 、非 : and 、 or 、 not</li>
</ul>
<ol>
<li>and 如果第一个表达式为False,后面没有必要计算</li>
<li>or 如果第一个表达式是True,后面就没有必要计算了</li>
<li>条件很多不知道判断优先顺序可以添加小括号</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#逻辑运算符</span><br><span class="line">age=11</span><br><span class="line">gender="male"</span><br><span class="line"></span><br><span class="line">if age>=18 and age<=60 and gender=="female":</span><br><span class="line"> print("adult woman")</span><br><span class="line"></span><br><span class="line">if age<18 or age >60 and gender=="female":</span><br><span class="line"> print("kid or old woman") </span><br><span class="line"> </span><br><span class="line">if age>=18 and age<=60 and not gender=="female":</span><br><span class="line"> print("adult man")</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="特殊数据真值表"><a href="#特殊数据真值表" class="headerlink" title="特殊数据真值表"></a>特殊数据真值表</h4><table>
<thead>
<tr>
<th>对象/变量</th>
<th>值</th>
</tr>
</thead>
<tbody><tr>
<td>“”</td>
<td>假</td>
</tr>
<tr>
<td>“string”</td>
<td>真</td>
</tr>
<tr>
<td>0</td>
<td>假</td>
</tr>
<tr>
<td>() 空元组</td>
<td>假</td>
</tr>
<tr>
<td>[] 空列表</td>
<td>假</td>
</tr>
<tr>
<td>{} 空字典</td>
<td>假</td>
</tr>
<tr>
<td>None</td>
<td>假</td>
</tr>
</tbody></table>
<h3 id="for-循环语句"><a href="#for-循环语句" class="headerlink" title="for 循环语句"></a>for 循环语句</h3><p>计算机之所以能批量的执行数据分析,就是因为有循环语句。</p>
<p>for…in循环,in 后面跟可迭代对象</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#列表遍历</span><br><span class="line">names = ['Michael', 'Bob', 'Tracy']</span><br><span class="line">for name in names:</span><br><span class="line"> print(name)</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">sum = 0</span><br><span class="line">for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:</span><br><span class="line"> sum = sum + x</span><br><span class="line">print(sum) </span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#产生连续的数字函数:range(start, stop[, step])</span><br><span class="line"></span><br><span class="line">range(10)</span><br><span class="line">range(2,10)</span><br><span class="line">#也可以转换成list对象</span><br><span class="line">list(range(10))</span><br><span class="line">list(range(1, 11))</span><br><span class="line">list(range(0, 30, 5))</span><br><span class="line">list(range(0, 10, 3))</span><br><span class="line">list(range(0, -10, -1))</span><br><span class="line">list(range(0))</span><br><span class="line">list(range(1, 0))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">sum = 0</span><br><span class="line">for x in range(101):</span><br><span class="line"> sum = sum + x</span><br><span class="line">print(sum)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">sum = 0</span><br><span class="line">for x in range(3,101):</span><br><span class="line"> sum = sum + x</span><br><span class="line">print(sum)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">sum = 0</span><br><span class="line">for x in range(3,101,2):</span><br><span class="line"> sum = sum + x</span><br><span class="line">print(sum)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#enumerate函数 同时获取索引还有值</span><br><span class="line"></span><br><span class="line">#用法enumerate(sequence, [start=0])</span><br><span class="line"></span><br><span class="line">names = ['Michael', 'Bob', 'Tracy']</span><br><span class="line">for index,value in enumerate(names):</span><br><span class="line"> print(index,value)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#字典遍历</span><br><span class="line">dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}</span><br><span class="line">for k,v in dict.items():</span><br><span class="line"> print(k,v)</span><br><span class="line"></span><br><span class="line">dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}</span><br><span class="line">for k in dict.keys():</span><br><span class="line"> print(k,dict[k])</span><br></pre></td></tr></table></figure>
<h3 id="while-循环语句"><a href="#while-循环语句" class="headerlink" title="while 循环语句"></a>while 循环语句</h3><p>while循环,只要条件满足,就不断循环,条件不满足时退出循环</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">sum = 0</span><br><span class="line">n = 99</span><br><span class="line">while n > 0:</span><br><span class="line"> sum = sum + n</span><br><span class="line"> n = n - 2</span><br><span class="line">print(sum)</span><br></pre></td></tr></table></figure>
<h3 id="循环控制语句"><a href="#循环控制语句" class="headerlink" title="循环控制语句"></a>循环控制语句</h3><p>break 提前退出循环<br>continue 提前结束本轮循环</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">n = 1</span><br><span class="line">while n <= 100:</span><br><span class="line"> if n > 10: # 当n = 11时,条件满足,执行break语句</span><br><span class="line"> break # break语句会结束当前循环</span><br><span class="line"> print(n)</span><br><span class="line"> n = n + 1</span><br><span class="line">print('END')</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">n = 0</span><br><span class="line">while n < 10:</span><br><span class="line"> n = n + 1</span><br><span class="line"> if n % 2 == 0: # 如果n是偶数,执行continue语句</span><br><span class="line"> continue # continue语句会直接继续下一轮循环,后续的print()语句不会执行</span><br><span class="line"> print(n)</span><br></pre></td></tr></table></figure>
<p>==练习题==</p>
<ol>
<li>for 循环打印星号金字塔:</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"> *</span><br><span class="line"> ***</span><br><span class="line"> *****</span><br><span class="line"> *******</span><br><span class="line">*********</span><br></pre></td></tr></table></figure>
<p>答案:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">for i in range(1, 6):</span><br><span class="line"> for j in range(0, 6 - i):</span><br><span class="line"> print (" ",end="")</span><br><span class="line"> </span><br><span class="line"> print( "*"*(i),end="")</span><br><span class="line"> print( "*"*(i-1),end="") </span><br><span class="line"> print("")</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>利用for循环,打印乘法口诀:</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">1*1=1 </span><br><span class="line">1*2=2 2*2=4 </span><br><span class="line">1*3=3 2*3=6 3*3=9 </span><br><span class="line">1*4=4 2*4=8 3*4=12 4*4=16 </span><br><span class="line">1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 </span><br><span class="line">1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 </span><br><span class="line">1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 </span><br><span class="line">1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 </span><br><span class="line">1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 </span><br></pre></td></tr></table></figure>
<p>答案:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">for m in range(1, 10):</span><br><span class="line"> for n in range(1, m+1):</span><br><span class="line"> print("%d*%d=%d\t"%(n,m,n*m), end="")</span><br><span class="line"> print("")</span><br></pre></td></tr></table></figure>
<h2 id="python-读写数据文件"><a href="#python-读写数据文件" class="headerlink" title="python 读写数据文件"></a>python 读写数据文件</h2><h3 id="python文件读写分为三步"><a href="#python文件读写分为三步" class="headerlink" title="python文件读写分为三步"></a>python文件读写分为三步</h3><ol>
<li>打开文件获取文件对象</li>
<li>操作文件</li>
<li>关闭文件</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">fr = open("test.txt","r") #打开文件</span><br><span class="line">ff = fr.read() #读取文件所有内容 (不建议使用,如果文件内容巨大,内存会爆)</span><br><span class="line">print(ff) </span><br></pre></td></tr></table></figure>
<h3 id="文件类型"><a href="#文件类型" class="headerlink" title="文件类型"></a>文件类型</h3><table>
<thead>
<tr>
<th>参数</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>r</td>
<td>只读,默认模式 打开数据文件</td>
</tr>
<tr>
<td>w</td>
<td>只写,不可读,若文件不存在则创建,若存在,则删除内容,写入新内容</td>
</tr>
<tr>
<td>a</td>
<td>只追加,不可读,若文件不存在则创建,存在则追加新内容</td>
</tr>
</tbody></table>
<h3 id="文件方法(不常用)"><a href="#文件方法(不常用)" class="headerlink" title="文件方法(不常用)"></a>文件方法(不常用)</h3><p> 前三个方法:在大文件时慎用,会把内容读到内存中,占用大内存</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>f.write()</td>
<td>字符串写入一个打开的文件</td>
</tr>
<tr>
<td>f.close()</td>
<td>刷新缓冲区里任何还没写入的信息,并关闭该文件,这之后便不能再进行写入。</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>f.read()</td>
<td>#读取所有内容</td>
</tr>
<tr>
<td>f.readline()</td>
<td>#读取一行</td>
</tr>
<tr>
<td>f.readlines()</td>
<td>#读取所有文件内容,返回一个list</td>
</tr>
<tr>
<td>f.seek(0)</td>
<td>#当前文件指针位置在0位</td>
</tr>
<tr>
<td>f.writelines([“a”,”b”])</td>
<td>#把列表写入文件</td>
</tr>
</tbody></table>
<h4 id="文件的读入与写出(常用方法):"><a href="#文件的读入与写出(常用方法):" class="headerlink" title="文件的读入与写出(常用方法):"></a>文件的读入与写出(常用方法):</h4><p>文件的读入与写出主要用的是for循环:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">fr=open("input.fa","r") #输入文件,读取数据</span><br><span class="line">fw=open("output.fa","w") #输出文件,写出结果</span><br><span class="line">for line in fr: #循环一行一行的读取文件 </span><br><span class="line"> new_line = line.replace("A","T") </span><br><span class="line"> fw.write(new_line)</span><br><span class="line">fr.close() #关闭文件</span><br><span class="line">fw.close() #关闭文件</span><br></pre></td></tr></table></figure>
<p>测试文件内容:<br>input.fa</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">AGTTAGCGGATAATGGCCATCAAAGCAACGCTTACCAACACTGCACCCCTTGTTTTGGAAATGCAACCAC</span><br><span class="line">AAAAGCATTGGACACTTGCTTACTTCAAATAAAACACATTTAAACAATTAGATGACGTGATGGACCAGAA</span><br><span class="line">TGGCGCATCGGGAAGTCATCCGAACAGGCTATCCCAAGGAAGAGGAGCCCATGCGCGCGAACGTGGCGCC</span><br><span class="line">ACAGTTTCCGCGGCGGCAAATCGGAGTAACATTATCGACGAAATGGCCAAAATATGCGAAGCCGATCGCC</span><br><span class="line">AGACTTTCGCCATCGCTCGACGGACTCGGGGTCACGAGCGGCTTGCGGTGGACAACAGCGACTTCGTCGC</span><br><span class="line">CGTGGAGGATCTTATTTTGTCCTACGCAGAGCCCACGCCCGAGGACCAGGTCGAGATGATCATGAGCGAC</span><br><span class="line">TTTTGCTCGTCTCCAACATACGCAGAGGATGAGGATGAGCCCAGCCATGAGTCGGAGCCGTGGTTTCGAT</span><br><span class="line">TTCGCAACAAAAGGATCAGAACCTACAGCCGGAAGAGGGATCCCAAAAGCCACAAGGCCGTTCAAAACGA</span><br><span class="line">GAAGCGTAGAGGTTCCTCAGGCCTCTCCGTGCAGAGGGATCTCAATACTTCGTTCACATCTATGGCTTGT</span><br><span class="line">GATTTCGATGCTTCATCACAGAAGATACACGAGGTCCTTTTGAACCTCAGTCAATACTTTTCCGCGACCG</span><br><span class="line">CGACAGCTTCCGGTCCGACTCCTGTCCCATCGCAAATAGATCTGCCAACCGAAGCAAGGCAGGATT</span><br></pre></td></tr></table></figure>
<hr>
<p>==学习任务==</p>
<ul>
<li>编写脚本要求:</li>
<li><input checked="" disabled="" type="checkbox"> 读取gff文件,筛选出文件中1号染色体100000-500000之间的基因</li>
<li><input checked="" disabled="" type="checkbox"> 输出基因的名字,染色体,起始位置,终止位置信息用tab分隔各列</li>
</ul>
<p>输入文件下载地址:</p>
<p><a href="ftp://ftp.ensemblgenomes.org/pub/plants/release-44/gff3/arabidopsis_thaliana/Arabidopsis_thaliana.TAIR10.44.chromosome.1.gff3.gz">ftp://ftp.ensemblgenomes.org/pub/plants/release-44/gff3/arabidopsis_thaliana/Arabidopsis_thaliana.TAIR10.44.chromosome.1.gff3.gz</a></p>
<p>注意:几乎所有的生物数据文件都是文本文件,都可以用notepad++,editplus等文本编辑器打开,不要被文件后缀名迷惑,我们的编程语言python绝大多数情况下打开处理的也都是文本文件。</p>
<hr>
<p><strong>答案:</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">fr=open("D:\python_script\Arabidopsis_thaliana.TAIR10.44.chromosome.1.gff3\Arabidopsis_thaliana.TAIR10.44.chromosome.1.gff3","r")</span><br><span class="line">fw=open("D:\python_script\Arabidopsis_thaliana.TAIR10.44.chromosome.1.gff3\out.txt","w")</span><br><span class="line"></span><br><span class="line">for line in fr:</span><br><span class="line"> line=line.strip()</span><br><span class="line"> if not line[0]=="#":</span><br><span class="line"> tmp=line.split("\t")</span><br><span class="line"> if tmp[0]=="1" and tmp[2]=="gene" and int(tmp[3])>100000 and int(tmp[3])<500000:</span><br><span class="line"> geneID=tmp[8].split(";")[0].split("=")[1]</span><br><span class="line"> #mystr=tmp[0]+"\t"+tmp[3]+"\t"+tmp[4]+"\t"+geneID+"\n"</span><br><span class="line"> mystr="\t".join([tmp[0],tmp[3],tmp[4],geneID])+"\n"</span><br><span class="line"> fw.write(mystr)</span><br><span class="line"> #fw.write(line+"\n")</span><br><span class="line"> </span><br><span class="line">fr.close()</span><br><span class="line">fw.close()</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category>Learn_Python</category>
</categories>
<tags>
<tag>Python</tag>
</tags>
</entry>
<entry>
<title>Python 高级编程</title>
<url>/2024/07/02/Python%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B/</url>
<content><![CDATA[<h1 id="Python高级编程"><a href="#Python高级编程" class="headerlink" title="Python高级编程"></a>Python高级编程</h1><h2 id="1-函数"><a href="#1-函数" class="headerlink" title="1 函数"></a>1 函数</h2><h3 id="1-1-Python内置函数:"><a href="#1-1-Python内置函数:" class="headerlink" title="1.1 Python内置函数:"></a>1.1 Python内置函数:</h3><p>Python内置了很多有用的函数,我们可以直接调用。</p>
<p>内置函数总结:<a href="https://docs.python.org/zh-cn/3.7/library/functions.html">https://docs.Python.org/zh-cn/3.7/library/functions.html</a></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">abs(-100)</span><br><span class="line">max(1, 2)</span><br><span class="line">int('123')</span><br><span class="line">float('12.34')</span><br><span class="line">sum([2,323,23])</span><br></pre></td></tr></table></figure>
<h3 id="1-2-Python-用户自定义函数"><a href="#1-2-Python-用户自定义函数" class="headerlink" title="1.2 Python 用户自定义函数"></a>1.2 Python 用户自定义函数</h3><p>自定义函数:把具有独立功能的代码块组织成为一个小模块。</p>
<p>好处:</p>
<ol>
<li>代码复用,提高编程效率,使程序易于理解。</li>
<li>自己学会定义函数,有助于理解看懂别人函数的帮助</li>
</ol>
<h4 id="定义一个函数"><a href="#定义一个函数" class="headerlink" title="定义一个函数"></a>定义一个函数</h4><p>你可以定义一个自定义功能的函数,以下是简单的规则:</p>
<ol>
<li>函数代码块以 def 关键词开头,后接函数标识符名称和圆括号()。</li>
<li>括号里面可设置参数,参数不是必必需的(可有可无),传入参数必须放在圆括号中间。</li>
<li>函数的第一行语句可以选择性地(可有可无)使用文档字符串—用于存放函数使用说明。</li>
<li>函数内容以冒号起始,并且缩进。</li>
<li>return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。</li>
</ol>
<p>参数作用:增加函数的通用性,针对相同的数据处理逻辑,能够适应更多的数据</p>
<ol>
<li>在函数内部,把参数当作变量使用,进行需要的数据处理</li>
<li>函数调用时,按照函数定义的参数顺序,把希望在函数内部处理的数据,通过参数传递</li>
</ol>
<h4 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">#定义</span><br><span class="line">def hello(s):</span><br><span class="line"> '''this function is used to say hello,</span><br><span class="line"> and exit.</span><br><span class="line"> </span><br><span class="line"> '''</span><br><span class="line"> print('hello '+s)</span><br><span class="line"> return</span><br><span class="line"> </span><br><span class="line">#调用</span><br><span class="line">hello(s="omicsgene")</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="不同类型参数的设置与使用"><a href="#不同类型参数的设置与使用" class="headerlink" title="不同类型参数的设置与使用"></a>不同类型参数的设置与使用</h4><h5 id="参数根据位置传递"><a href="#参数根据位置传递" class="headerlink" title="参数根据位置传递"></a>参数根据位置传递</h5><p>如果我们设置了多个参数,python解释器会根据位置进行传递;</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">def power(x, n):</span><br><span class="line"> s = 1</span><br><span class="line"> while n > 0:</span><br><span class="line"> n = n - 1</span><br><span class="line"> s = s * x</span><br><span class="line"> return s</span><br><span class="line"></span><br><span class="line">power(2,3)</span><br><span class="line"></span><br><span class="line">power(x=2,n=3)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h5 id="必需参数"><a href="#必需参数" class="headerlink" title="必需参数"></a>必需参数</h5><p>直接设定参数,没有给参数的默认值,函数调用时必需给参数传入值</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">def power(x, n):</span><br><span class="line"> s = 1</span><br><span class="line"> while n > 0:</span><br><span class="line"> n = n - 1</span><br><span class="line"> s = s * x</span><br><span class="line"> return s</span><br><span class="line"> </span><br><span class="line">power(4)</span><br></pre></td></tr></table></figure>
<h5 id="设置默认参数-函数调用时可有可无"><a href="#设置默认参数-函数调用时可有可无" class="headerlink" title="设置默认参数(函数调用时可有可无)"></a>设置默认参数(函数调用时可有可无)</h5><p>设置参数是可以设定参数的默认值,函数调用时可选给参数传入值们不给就是默认值。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">def power(x, n=2):</span><br><span class="line"> s = 1</span><br><span class="line"> while n > 0:</span><br><span class="line"> n = n - 1</span><br><span class="line"> s = s * x</span><br><span class="line"> return s</span><br><span class="line"></span><br><span class="line">def info(name, gender, age=6, city='Beijing'):</span><br><span class="line"> print('name:', name)</span><br><span class="line"> print('gender:', gender)</span><br><span class="line"> print('age:', age)</span><br><span class="line"> print('city:', city)</span><br></pre></td></tr></table></figure>
<h5 id="可变参数位置参数"><a href="#可变参数位置参数" class="headerlink" title="可变参数位置参数"></a>可变参数位置参数</h5><p>* 在参数名字前面加一个星号,使参数成为一个可变参数,参数传入一个列表</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">def add(a,b):</span><br><span class="line"> return a+b</span><br><span class="line"></span><br><span class="line">def add1(*numbers):</span><br><span class="line"> sum = 0</span><br><span class="line"> for n in numbers:</span><br><span class="line"> sum = sum + n </span><br><span class="line"> return sum</span><br></pre></td></tr></table></figure>
<h5 id="可变关键字参数"><a href="#可变关键字参数" class="headerlink" title="可变关键字参数"></a>可变关键字参数</h5><p>** 在参数名字前面加两个星号,使参数成为一个可变关键字参数,参数传入一个字典</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">def person(name, age, **kwargs):</span><br><span class="line"> print('name:', name, 'age:', age, 'other:', kwargs)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#联合使用,参数如何传递? </span><br><span class="line">def foo(a,b,c=22,*args,**kwargs):</span><br><span class="line"> print("a:",a,",","b:",b,",","c:",c,",","args:",args,",","kwargs:",kwargs)</span><br></pre></td></tr></table></figure>
<h5 id="强制-key-value形式传递"><a href="#强制-key-value形式传递" class="headerlink" title="强制 key=value形式传递"></a>强制 key=value形式传递</h5><p>有个参数只有一个星号,表示星号后面的参数必须用(key=value)的形式进行传递参数,不能省略key。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">def foo1(a,b,*,c=3):</span><br><span class="line"> print(a,b,c)</span><br><span class="line"> </span><br><span class="line">def foo2(a,b,c=3):</span><br><span class="line"> print(a,b,c)</span><br><span class="line"></span><br><span class="line">foo1(1,2,4)</span><br><span class="line">foo2(1,2,4)</span><br></pre></td></tr></table></figure>
<h5 id="函数编写与调用注意事项"><a href="#函数编写与调用注意事项" class="headerlink" title="函数编写与调用注意事项"></a>函数编写与调用注意事项</h5><ol>
<li>必需参数一般写在前面,有默认值的参数紧随其后(位置参数)</li>
<li>参数名省略时,注意位置</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">def foo(a,b,c=22,*args,**kwargs):</span><br><span class="line"> print(a,b,c,args,kwargs)</span><br><span class="line"></span><br><span class="line">foo()</span><br></pre></td></tr></table></figure>
<h3 id="1-3-Python中匿名函数"><a href="#1-3-Python中匿名函数" class="headerlink" title="1.3 Python中匿名函数"></a>1.3 Python中匿名函数</h3><p>有时候一个函数只是临时使用一下,以后就不会再使用了,要是用def定义比较麻烦。这个时候就可以用lambda来定义一个匿名函数</p>
<h5 id="语法:"><a href="#语法:" class="headerlink" title="语法:"></a>语法:</h5><p>变量名= lambda [arg1[, arg2, … argN]]: expression</p>
<h5 id="注意"><a href="#注意" class="headerlink" title="注意:"></a>注意:</h5><ol>
<li>参数:可选,通常以逗号分隔的变量表达式形式,也就是位置参数</li>
<li>表达式中不能包含 循环,return</li>
<li>可以包含 if…else…语句.</li>
<li>表达式计算的结果直接返回</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"># 使用lambda的表达式</span><br><span class="line">lambda x, y: x + y</span><br><span class="line"></span><br><span class="line"># 使用def定义的函数</span><br><span class="line">def add( x, y ):</span><br><span class="line"> return x + y</span><br><span class="line"></span><br><span class="line"># lambda也允许有默认值和使用变长参数</span><br><span class="line">lambda x, y = 2: x + y</span><br><span class="line">lambda *z: z</span><br><span class="line"></span><br><span class="line"># 调用lambda函数</span><br><span class="line">a = lambda x, y: x + y</span><br><span class="line">a(1, 3)</span><br><span class="line"></span><br><span class="line">b = lambda x, y = 2: x + y</span><br><span class="line">b(1)</span><br><span class="line"></span><br><span class="line">b(1, 3)</span><br><span class="line"></span><br><span class="line">c = lambda *z: z</span><br><span class="line">c(10, 'test')</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="匿名函数应用"><a href="#匿名函数应用" class="headerlink" title="匿名函数应用"></a>匿名函数应用</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#自定义排序关键字</span><br><span class="line"></span><br><span class="line">students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]</span><br><span class="line">sorted(students, key=lambda s: s[2]) # 按年龄排序</span><br><span class="line">sorted(students, key=lambda s: s[2], reverse=True) # 按降序</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#字典排序:</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">dic = {'a':2,'b':1}</span><br><span class="line">#按照key排序</span><br><span class="line">d = sorted(dic.items(), key = lambda k:k[0])</span><br><span class="line"></span><br><span class="line">#按照values排序</span><br><span class="line"> e = sorted(dic.items(), key = lambda k:k[1])</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="1-4-变量作用域"><a href="#1-4-变量作用域" class="headerlink" title="1.4 变量作用域"></a>1.4 变量作用域</h3><p>变量的作用域决定了在哪一部分程序你可以访问那个特定的变量名称。一个程序的所有的变量并不是在任何位置都可以访问的。访问权限决定于这个变量是在哪里赋值的。</p>
<p>两种最基本的变量作用域如下:</p>
<ul>
<li>全局变量</li>
<li>局部变量</li>
</ul>
<h5 id="全局变量和局部变量"><a href="#全局变量和局部变量" class="headerlink" title="全局变量和局部变量"></a>全局变量和局部变量</h5><ul>
<li><p>定义在函数内部的变量拥有一个局部作用域,定义在函数外的拥有全局作用域。</p>
</li>
<li><p>局部变量只能在其被声明的函数内部访问,而全局变量可以在整个程序范围内访问。</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">total = 0; # 这是一个全局变量</span><br><span class="line"></span><br><span class="line">def mysum( arg1, arg2 ):</span><br><span class="line"> total = arg1 + arg2; # total在这里是局部变量.</span><br><span class="line"> print("inside ", total)</span><br><span class="line"> return total</span><br><span class="line"> </span><br><span class="line">#调用mysum函数</span><br><span class="line">mysum( 10, 20 )</span><br><span class="line">print("global var ", total)</span><br></pre></td></tr></table></figure>
<h3 id="1-5-变量传递给参数参数的可变性"><a href="#1-5-变量传递给参数参数的可变性" class="headerlink" title="1.5 变量传递给参数参数的可变性"></a>1.5 变量传递给参数参数的可变性</h3><p>函数的参数传递中传入可变数据和不可变数据会有不同:</p>
<ul>
<li>不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组);</li>
<li>可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)。</li>
</ul>
<h5 id="传不可变对象实例-传进去的变量不改变原变量"><a href="#传不可变对象实例-传进去的变量不改变原变量" class="headerlink" title="传不可变对象实例,传进去的变量不改变原变量"></a>传不可变对象实例,传进去的变量不改变原变量</h5><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">def ChangeInt(a):</span><br><span class="line"> a = 10</span><br><span class="line"> </span><br><span class="line">b = 2</span><br><span class="line">ChangeInt(b)</span><br><span class="line">print (b) # 结果是 2</span><br></pre></td></tr></table></figure>
<h5 id="传可变对象实例-传进去的变量可改变原变量"><a href="#传可变对象实例-传进去的变量可改变原变量" class="headerlink" title="传可变对象实例,传进去的变量可改变原变量"></a>传可变对象实例,传进去的变量可改变原变量</h5><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">def changeme(mylist):</span><br><span class="line"> "修改传入的列表"</span><br><span class="line"> mylist.append([1,2,3,4]);</span><br><span class="line"> print("Inside value:", mylist)</span><br><span class="line"> return</span><br><span class="line"> </span><br><span class="line"># 调用changeme函数</span><br><span class="line">mynum = [10,20,30]</span><br><span class="line">changeme(mynum)</span><br><span class="line">print("Outside value:", mynum)</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="2-面向对象的编程与获取帮助"><a href="#2-面向对象的编程与获取帮助" class="headerlink" title="2 面向对象的编程与获取帮助"></a>2 面向对象的编程与获取帮助</h2><h3 id="2-1-类和实例"><a href="#2-1-类和实例" class="headerlink" title="2.1 类和实例"></a>2.1 类和实例</h3><blockquote>
<p>面向对象最重要的概念就是类(Class)和实例(Instance),类描述了一组具有相同特性(属性)和相同行为(方法)的对象(Object)。面向对象的编程语言最大的特色就是可以编写自己所需的数据类型,以更好的解决问题。</p>
</blockquote>
<blockquote>
<p>必须牢记类是抽象的模板,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法或者属性,但各自的数据可能不同。</p>
</blockquote>
<p><img src="https://note.youdao.com/yws/api/group/92990402/noteresource/8DB0B3FB6790404EADCA0B71D9EB70F1/version/987?method=get-resource&shareToken=08DB9C16FB6D47AA8EBD104017423D2D&entryId=446741437" alt="image"></p>
<h4 id="面向对象编程特点"><a href="#面向对象编程特点" class="headerlink" title="面向对象编程特点"></a>面向对象编程特点</h4><p>面向对象编程(Object-oriented programming,缩写:OOP)的3个基本特征是:封装、继承、多态</p>
<ul>
<li><p>封装:将属性和方法(数据和功能)封装在一起形成类。 </p>
</li>
<li><p>继承:可以使用现有类的功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。</p>
</li>
<li><p>多态:允许让父类的指针分别指向不同的子类, 调用不同子类的同一个方法, 会有不同的执行效果</p>
</li>
</ul>
<h3 id="2-2-面向对象编程"><a href="#2-2-面向对象编程" class="headerlink" title="2.2 面向对象编程"></a>2.2 面向对象编程</h3><h4 id="定义一个类"><a href="#定义一个类" class="headerlink" title="定义一个类"></a>定义一个类</h4><h5 id="类的组成"><a href="#类的组成" class="headerlink" title="类的组成"></a>类的组成</h5><ul>
<li>属性(对象的属性) ——变量:状态、静态的</li>
<li>方法(对象的行为) ——函数:过程、动态的</li>
</ul>
<h5 id="类的方法与属性"><a href="#类的方法与属性" class="headerlink" title="类的方法与属性"></a>类的方法与属性</h5><blockquote>
<p>在类的内部,使用 def 关键字来定义一个方法,类的方法与普通的函数只有一个特别的区别——他们的第一个参数必须是 self。</p>
</blockquote>
<blockquote>
<p>self代表类的实例,而非类。</p>
</blockquote>
<h5 id="构造方法"><a href="#构造方法" class="headerlink" title="构造方法"></a>构造方法</h5><p>类有一个名为 __init__() 的特殊方法(构造方法),该方法在类实例化时会自动调用。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#类的定义</span><br><span class="line">class Car:</span><br><span class="line"> '''this class define a car '''</span><br><span class="line"> #类属性 共有属性</span><br><span class="line"> wheels=4</span><br><span class="line"> #构造方法</span><br><span class="line"> def __init__(self, make, model, year):</span><br><span class="line"> #成员属性</span><br><span class="line"> self.make = make</span><br><span class="line"> self.model = model</span><br><span class="line"> self.year = year</span><br><span class="line"> self.orometer_reading = 0</span><br><span class="line"> #类方法</span><br><span class="line"> def get_description(self):</span><br><span class="line"> long_name = str(self.year) + ' ' + self.make + ' ' + self.model+" "+str(self.wheels)</span><br><span class="line"> return long_name</span><br><span class="line"> </span><br><span class="line"> def get_odometer(self):</span><br><span class="line"> print("This car has "+ str(self.orometer_reading) + " miles on it")</span><br><span class="line"> </span><br><span class="line"> def increase(self,miles):</span><br><span class="line"> self.orometer_reading +=miles</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="创建类的实例化对象,访问属性和使用方法"><a href="#创建类的实例化对象,访问属性和使用方法" class="headerlink" title="创建类的实例化对象,访问属性和使用方法"></a>创建类的实例化对象,访问属性和使用方法</h4><p>现在让我们新建一个对象my_car:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#类实例my_car</span><br><span class="line">my_car = Car("yellow", "beetle", 1967)</span><br><span class="line">#查看属性</span><br><span class="line">print(f" My {my_car.color} car {my_car.model} is made in {my_car.year}")</span><br><span class="line"></span><br><span class="line">#属性修改</span><br><span class="line">my_car.color="black"</span><br><span class="line"></span><br><span class="line">#对象方法调用</span><br><span class="line"></span><br><span class="line">my_car.get_description()</span><br><span class="line">my_car.increase(1000)</span><br><span class="line">my_car.read_odometer()</span><br><span class="line">my_car.update_orometer(20000)</span><br><span class="line"></span><br><span class="line">#查看类或者实例所有的属性与方法</span><br><span class="line">dir(my_car)</span><br><span class="line">dir(Car)</span><br></pre></td></tr></table></figure>
<h3 id="2-3-Python-类定义中的特殊属性与方法"><a href="#2-3-Python-类定义中的特殊属性与方法" class="headerlink" title="2.3 Python 类定义中的特殊属性与方法"></a>2.3 Python 类定义中的特殊属性与方法</h3><p>Python中用下划线作为变量前缀和后缀指定特殊变量</p>
<ul>
<li>_xxx 不能用’from module import *’导入</li>
<li>__xxx 类中的私有变量名</li>
<li>__xxx__ 系统定义的名字</li>
<li>核心风格:避免用下划线作为变量名的开始。</li>
</ul>
<p>因为下划线对解释器有特殊的意义,而且是内建标识符所使用的符号,我们建议程序员避免用下划线作为变量名的开始。</p>
<p>==一般来讲,变量名_xxx被看作是“私有的”,在模块或类外不可以使用。==</p>
<p>当变量是私有的时候,用_xxx 来表示变量是很好的习惯。因为变量名__xxx__对Python 来说有特殊含义,对于普通的变量应当避免这种命名风格。</p>
<ul>
<li>“单下划线” 开始的成员变量叫做保护变量,意思是只有类对象和子类对象自己能访问到这些变量;</li>
<li>“双下划线” 开始的是私有成员,意思是只有类对象自己能访问,连子类对象也不能访问到这个数据。</li>
</ul>
<h5 id="Python类的特殊属性"><a href="#Python类的特殊属性" class="headerlink" title="Python类的特殊属性"></a>Python类的特殊属性</h5><table>
<thead>
<tr>
<th>属性</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>__class__</td>
<td>对象或类所属的类</td>
</tr>
<tr>
<td>__name__</td>
<td>类、函数、方法等的名字</td>
</tr>
<tr>
<td>__dict__</td>
<td>类的属性 以key-value形式展示的字典,展示出属性所对应的值</td>
</tr>
<tr>
<td>__module__</td>
<td>类定义所在的模块名称</td>
</tr>
<tr>
<td>__doc__</td>
<td>类、函数的文档字符串,如果没有定义则为None</td>
</tr>
</tbody></table>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">#print(my_car.__name__)</span><br><span class="line">print(my_car.__doc__)</span><br><span class="line">print(my_car.__dict__)</span><br><span class="line">print(my_car.__module__)</span><br><span class="line">print(my_car.__class__)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">print(Car.__name__)</span><br><span class="line">print(Car.__doc__)</span><br><span class="line">print(Car.__dict__)</span><br><span class="line">print(Car.__module__)</span><br><span class="line">print(Car.__class__)</span><br></pre></td></tr></table></figure>
<h5 id="Python类特殊方法"><a href="#Python类特殊方法" class="headerlink" title="Python类特殊方法"></a>Python类特殊方法</h5><table>
<thead>
<tr>
<th>方法</th>
<th>功能说明</th>
</tr>
</thead>
<tbody><tr>
<td>__new__()</td>
<td>类的静态方法,用于确定是否创建对象</td>
</tr>
<tr>
<td>__init__()</td>
<td>构造函数,生成对象时调用</td>
</tr>
<tr>
<td>__dir__</td>
<td>返回类或者对象的所有方法与属性,dir()操作实例就是调用</td>
</tr>
<tr>
<td>__del__()</td>
<td>析构函数,释放对象时调用</td>
</tr>
<tr>
<td>__add__()</td>
<td>+</td>
</tr>
<tr>
<td>__sub__()</td>
<td>-</td>
</tr>
<tr>
<td>__mul__()</td>
<td>*</td>
</tr>
<tr>
<td>__truediv__()</td>
<td>/</td>
</tr>
<tr>
<td>__floordiv__()</td>
<td>//</td>
</tr>
<tr>
<td>__mod__()</td>
<td>%</td>
</tr>
<tr>
<td>__pow__()</td>
<td>**</td>
</tr>
<tr>
<td>__repr__()</td>
<td>打印,转换</td>
</tr>
<tr>
<td>__setitem__()</td>
<td>按照索引赋值</td>
</tr>
<tr>
<td>__getitem__()</td>
<td>按照索引获取值</td>
</tr>
<tr>
<td>__len__()</td>
<td>计算长度</td>
</tr>
<tr>
<td>__call__()</td>
<td>函数调用</td>
</tr>
<tr>
<td>__contains__()</td>
<td>in</td>
</tr>
<tr>
<td>__eq__()</td>
<td>==</td>
</tr>
<tr>
<td>__ne__()</td>
<td>!=</td>
</tr>
<tr>
<td>__lt__()</td>
<td><</td>
</tr>
<tr>
<td>__le__()</td>
<td><==</td>
</tr>
<tr>
<td>__gt__()</td>
<td>></td>
</tr>
<tr>
<td>__ge__()</td>
<td>>=</td>
</tr>
<tr>
<td>__str__()</td>
<td>转换为字符串</td>
</tr>
<tr>
<td>__shift__(), __rshift__()</td>
<td><<, >></td>
</tr>
<tr>
<td>__and__(), __or__()</td>
<td>&,</td>
</tr>
<tr>
<td>__invert__(), __xor__()</td>
<td>~, ^</td>
</tr>
<tr>
<td>__iadd__(), __isub__()</td>
<td>+=, -=</td>
</tr>
</tbody></table>
<h5 id="例子:实现特殊方法-repr-:"><a href="#例子:实现特殊方法-repr-:" class="headerlink" title="例子:实现特殊方法__repr__:"></a>例子:实现特殊方法__repr__:</h5><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line">## __repr__</span><br><span class="line">class Person:</span><br><span class="line"> def __init__(self, name):</span><br><span class="line"> self.name = name</span><br><span class="line"> def __repr__(self):</span><br><span class="line"> return "hello %s." % self.name</span><br><span class="line"></span><br><span class="line"> p = Person('hkey')</span><br><span class="line"></span><br><span class="line"> p</span><br><span class="line"> print(p)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>定义了__repr__方法,不管是直接打印对象还是通过print打印对象,都是走的__repr__中定义的格式。</p>