-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
180 lines (85 loc) · 256 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
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>2020年度总结</title>
<link href="/2020/12/31/2020-nian-du-zong-jie/"/>
<url>/2020/12/31/2020-nian-du-zong-jie/</url>
<content type="html"><![CDATA[<h1 id="2020年度总结"><a href="#2020年度总结" class="headerlink" title="2020年度总结"></a>2020年度总结</h1><blockquote><p>突发奇想写的这篇文章,并没有细细构思,只是回忆一下2020年菜鸡豪做的事情,顺带整理一下回忆继续出发,奔向崭新的2021!<strong>我们都是努力奔跑的追梦人!</strong></p></blockquote><h2 id="一月"><a href="#一月" class="headerlink" title="一月"></a>一月</h2><p>其实现在回想一月发生的事情,脑海里只存留着几个断断续续的片段,但是我清楚的记得我的新年愿望:</p><p>“<strong>能够比19年更努力,更幸运。</strong>”</p><p><a href="https://imgchr.com/i/rjzuWj" target="_blank" rel="noopener"><img src="https://s3.ax1x.com/2020/12/31/rjzuWj.md.png" alt="rjzuWj.md.png"></a></p><p>当然这个目标没有限定的界限,我说它实现了,那它就是实现了;我说它没有实现,那它就是没有实现。之所以定这么一个模糊的目标,是因为我不想失望了。至少我心里知道,比起整个19,我确实更加努力了,掌握了一些技能,也明白了一些道理。至于幸运,也算是挺幸运的吧。</p><p><strong>总有一些误解,不去了解,是不会解开的!</strong>很多时候我们总是会主观臆断,从自己主观的角度去理解问题,不会明白别人是怎么想的。不过这都是人之常情,所谓<em>人不为己,天诛地灭。</em>确实是这样的,因为大家都是普通人。所以有时候真的需要冷静下来了解一下让你烦恼的事情,然后就会发现,有些事情,看似偶然,实则必然。</p><p>还有就是,确实是有点可惜,来中北三年了,连一场像样的大学都没有见到过,北方的冬天真是这样子吗?很明显不是的,回家以后我就见到了真正的冬天,放眼望去,整个大地都是白的,那种久违的感觉着实Nice。再有就是在1月底看到偶像<strong>科比</strong>坠机事件,当时第一感觉就是:“卧槽,假的吧!!”然而事实就是如此,不接受也没办法。</p><h2 id="二-三月"><a href="#二-三月" class="headerlink" title="二/三月"></a>二/三月</h2><p>这两个月对我来说过的十分平凡,但对中国来说非常不平凡,也就是老生常谈的新馆疫情,现在想想好像也就那么回事,但是真正经历的时候确实感受很深,小区不让出,街上每天都有交警巡逻,为了大家的生命安全,白衣天使们义不容辞的奔向战场,好像据说年龄比我也大不了几岁,当时的笔者热血沸腾,但是现在想想都是故事。当时,有程序员自发的在网站上面收集整理疫情信息,我有关注,还加了一个微信群,群里每天都会有人发一些疫情信息,并在<strong>GitHub</strong>上进行同步整理。</p><p>大概是从三月份开始的吧,我每天早上都会和铁子去公园跑步,跑的不咋快,确挺快乐,其实我们还坚持了蛮久的,哈哈哈哈,不过自从铁子回学校以后我就开始堕落了,那个时候我才知道什么两周定律都是假的,唯一的敌人就是心中的欲望,而要做的就是满足心中的欲望,战胜自己,好的上战胜任何敌人:这也是为什么<strong>“我不是想赢,我只是不想输”</strong>,大概大家都是这么想的吧。</p><p><a href="https://imgchr.com/i/rvkCrj" target="_blank" rel="noopener"><img src="https://s3.ax1x.com/2020/12/31/rvkCrj.md.jpg" alt="rvkCrj.md.jpg"></a></p><h2 id="四-五月份"><a href="#四-五月份" class="headerlink" title="四/五月份"></a>四/五月份</h2><p>这个时候应该是上网课的时候了吧,可能这辈子都不会想到,竟然会有一天,真的可以坐在家里上课,听着老师在电脑上讲的那些知识,什么寄存器了,什么OSI了,听着听着,我好困啊,直接就睡了。哈哈,再家确实666,在家呆的想回学校也是没谁了,那应该是最长的一个寒假了,直接寒暑假一起过了,如果不是还有几个比赛一直在催着我学点东西,那我可能真的挂了。</p><p>这个时候的考试就更不用说了,哈哈哈哈,大家懂的都懂,有考好的,就有考差的,我就属于后者,其实我觉得大学的考试真的不能说明一个人的专业水平,因为老师们经常说考试是通过性考试,目的就是让大家通过。如果真正做过项目的话就应该知道,在课上学的那点点皮毛的知识远远不够,不是说课上的东西不重要,而是说要学的东西远远比课上学的那点要多得多,很多知识,需要在自己摸索中去寻找规律,我发现,其实生活中的好多东西都是相同的,大家都能够理解的,这就是为什么大家都是普通人。</p><p>网课大概就是这个样子,我不知道大家的单片机费了没有,反正我是废了,我就是个废物。</p><h2 id="六-七-八月份"><a href="#六-七-八月份" class="headerlink" title="六/七/八月份"></a>六/七/八月份</h2><p>这几个月份,应该是连着暑假了吧,暑假有一个社会实践,我就是在本地找了一份兼职,每天的任务也很简单,就是擦车,在亲戚家的一个洗车厂做了大概有快一个月吧,给我的感觉就是,这大概就是普通人每天的生活吧,其实想想,如果能够在这样一个城市,办一个店铺,自己做一点自己喜欢的事情,每天有喜欢的人,偶尔碰到烦心的事情,这样的养老生活也是十分惬意的。之所以说是养老生活是因为我觉得,趁年轻还是应该出去闯一闯,长长见识,了解世界的多姿多彩,就能够了解自己的渺小。</p><p>除了暑假社会实践,我剩下的任务应该就是两个比赛了,一个计设,一个互联网+,都说自己决定不了一生的贵人相助,我很感谢那些曾经给我帮助的人,但无以为报。有很多矫情的话,大家懂的都懂。今年的计设可以说是对我的成长比较大,对我的整个的人格成长也起到了至关重要的作用,让我明白了这个世界上,有些东西,只要你努力去做了,结果总不会太差。我们的作品是<strong>《检路》</strong>,也在比赛中取得了比较好的名次。</p><p><a href="https://imgchr.com/i/rvEVvF" target="_blank" rel="noopener"><img src="https://s3.ax1x.com/2020/12/31/rvEVvF.png" alt="rvEVvF.png"></a></p><p><a href="https://imgchr.com/i/rvEKER" target="_blank" rel="noopener"><img src="https://s3.ax1x.com/2020/12/31/rvEKER.jpg" alt="rvEKER.jpg"></a></p><p>名字就不打码了,哈哈哈哈哈。</p><h2 id="九月"><a href="#九月" class="headerlink" title="九月"></a>九月</h2><p>这个时候应该是开学了,开学就意味着有一堆的事情要做,据说大三上是物联网专业最忙的一个学期,现在想想好像这确实是真的,交不完的实验报告,计科都考完试了我们还没结课,当然这都是后话,金工实习也是比较开心的一件事情,不过确实蛮累的,到现在都依稀能想起钢锯在我耳边响的声音,都是一种经历吧。这个学期真的累成狗。</p><h2 id="十-十一月"><a href="#十-十一月" class="headerlink" title="十/十一月"></a>十/十一月</h2><p>这两个月是课最多的时候,物联网最苦的时候大概就是,一次要交十几分实验报告,要求是先打印出来,然后手写,然后在一张一张的拍照,然后再把图片放在word文件中弄成电子版上传。这是我见过最6的交作业的方式,简直不要太好了,哈哈哈哈。这学期光实验报告就打了很多很多,都是money啊。</p><h2 id="十二月"><a href="#十二月" class="headerlink" title="十二月"></a>十二月</h2><p>这个月也是魔鬼的一个月,分分分,学生的命根。不知道大家有没有听过这样一句话,<strong>内卷没有上限</strong>。这是今年才出来的,我的感受是比较深的,懂的都懂。大家都在以自己的方式努力着,我也一样,我也不知道我还能走多远,也不知道能到达一个什么样的高度,但是或许就是这样的未知数,才更让人向往。</p><p>这个月有一个彩蛋,在2020年12月21号,我党正式接收我为中共预备党员,从此,笔者必将更加努力,更加用工,尽自己的努力做自己力所能及的事情。</p><p>今天是2020年的最后一天了,保不齐明天就要被消灭了。</p><p><code>2021的目标:</code>希望自己能够更加努力,更加幸运。</p><p><strong>最后结尾:</strong>渐渐明白一些道理,也就没有什么很想维持的关系了,来着不拒,走者不留!当时光流逝,兜兜转转……</p>]]></content>
<tags>
<tag> 杂记 </tag>
</tags>
</entry>
<entry>
<title>20年安卓暑期考核算法题</title>
<link href="/2020/08/08/20-nian-an-zhuo-shu-qi-kao-he-suan-fa-ti/"/>
<url>/2020/08/08/20-nian-an-zhuo-shu-qi-kao-he-suan-fa-ti/</url>
<content type="html"><![CDATA[<p>[TOC]</p><h1 id="20年安卓暑期考核算法题"><a href="#20年安卓暑期考核算法题" class="headerlink" title="20年安卓暑期考核算法题"></a>20年安卓暑期考核算法题</h1><h2 id="一:缺失数字"><a href="#一:缺失数字" class="headerlink" title="一:缺失数字"></a>一:缺失数字</h2><h3 id="题目描述"><a href="#题目描述" class="headerlink" title="题目描述"></a>题目描述</h3><blockquote><p> 给定一个包含 <code>0, 1, 2, ..., n</code> 中 <em>n</em> 个数的序列,找出 0 .. <em>n</em> 中没有出现在序列中的那个数。 </p></blockquote><h3 id="解题思路"><a href="#解题思路" class="headerlink" title="解题思路"></a>解题思路</h3><blockquote><p>将给定数列排序,然后使用for循环依次进行比对,比对结果不匹配的数字即为所求。</p></blockquote><h3 id="Java代码"><a href="#Java代码" class="headerlink" title="Java代码"></a>Java代码</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">missingNumber</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums<span class="token punctuation">)</span> <span class="token punctuation">{</span> Arrays<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>nums<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//排序操作</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> i<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">//if判断</span> <span class="token keyword">return</span> i<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> nums<span class="token punctuation">.</span>length<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="python代码"><a href="#python代码" class="headerlink" title="python代码"></a>python代码</h3><pre class=" language-python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">Solution</span><span class="token punctuation">:</span> <span class="token keyword">def</span> <span class="token function">missingNumber</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> nums<span class="token punctuation">:</span> List<span class="token punctuation">[</span>int<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> int<span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># 排序</span> nums<span class="token punctuation">.</span>sort<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 使用for循环进行对比</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span>len<span class="token punctuation">(</span>nums<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">if</span> nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> i<span class="token punctuation">:</span> <span class="token keyword">return</span> i <span class="token keyword">return</span> i<span class="token operator">+</span><span class="token number">1</span></code></pre><h2 id="二:字母异位词分组"><a href="#二:字母异位词分组" class="headerlink" title="二:字母异位词分组"></a>二:字母异位词分组</h2><h3 id="题目描述-1"><a href="#题目描述-1" class="headerlink" title="题目描述"></a>题目描述</h3><blockquote><p>给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。</p><p>示例:</p><p>输入: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]<br>输出:<br>[<br>[“ate”,”eat”,”tea”],<br>[“nat”,”tan”],<br>[“bat”]<br>]<br>说明:</p><p>所有输入均为小写字母。<br>不考虑答案输出的顺序。</p></blockquote><h3 id="解决思路"><a href="#解决思路" class="headerlink" title="解决思路"></a>解决思路</h3><blockquote><p>将给定的字符串转换为数组,在将数组排序后重新转换为字符串然后进行比对,如果存在就放在一起,不存在就另起一行,使用HashMap建立映射关系,最终返回一个ArrayList。</p></blockquote><h3 id="Java代码-1"><a href="#Java代码-1" class="headerlink" title="Java代码"></a>Java代码</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> List<span class="token operator"><</span>List<span class="token operator"><</span>String<span class="token operator">>></span> <span class="token function">groupAnagrams</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> strs<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 返回值为List类型,List里面嵌套List,数据为String类型,因此定义HashMap值为List</span> Map<span class="token operator"><</span>String<span class="token punctuation">,</span> List<span class="token operator">></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token operator"><</span>String<span class="token punctuation">,</span> List<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>String i <span class="token operator">:</span> strs<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 将字符串转换为数组</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> i<span class="token punctuation">.</span><span class="token function">toCharArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Arrays<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 重新转换回来</span> String str <span class="token operator">=</span> String<span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>map<span class="token punctuation">.</span><span class="token function">containsKey</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 若不存在建立映射关系,排序后的字符串成为新的List集合</span> map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 建立映射关系后添加</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//返回值是List集合 通过构造器 构造一个包含指定 collection 的元素的列表</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token punctuation">(</span>map<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="python代码-1"><a href="#python代码-1" class="headerlink" title="python代码"></a>python代码</h3><pre class=" language-python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">Solution</span><span class="token punctuation">:</span> <span class="token keyword">def</span> <span class="token function">groupAnagrams</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> strs<span class="token punctuation">:</span> List<span class="token punctuation">[</span>str<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> List<span class="token punctuation">[</span>List<span class="token punctuation">[</span>str<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token keyword">from</span> collections <span class="token keyword">import</span> defaultdict look <span class="token operator">=</span> defaultdict<span class="token punctuation">(</span>list<span class="token punctuation">)</span> <span class="token keyword">for</span> s <span class="token keyword">in</span> strs<span class="token punctuation">:</span> look<span class="token punctuation">[</span><span class="token string">""</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>sorted<span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token keyword">return</span> list<span class="token punctuation">(</span>look<span class="token punctuation">.</span>values<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></code></pre><h2 id="三:金字塔转换矩阵"><a href="#三:金字塔转换矩阵" class="headerlink" title="三:金字塔转换矩阵"></a>三:金字塔转换矩阵</h2><h3 id="题目描述-2"><a href="#题目描述-2" class="headerlink" title="题目描述"></a>题目描述</h3><blockquote><p>现在,我们用一些方块来堆砌一个金字塔。 每个方块用仅包含一个字母的字符串表示。</p><p>使用三元组表示金字塔的堆砌规则如下:</p><p>对于三元组(A, B, C) ,“C”为顶层方块,方块“A”、“B”分别作为方块“C”下一层的的左、右子块。当且仅当(A, B, C)是被允许的三元组,我们才可以将其堆砌上。</p><p>初始时,给定金字塔的基层 bottom,用一个字符串表示。一个允许的三元组列表 allowed,每个三元组用一个长度为 3 的字符串表示。</p><p>如果可以由基层一直堆到塔尖就返回 true ,否则返回 false 。</p><p>示例 1:</p><p>输入:bottom = “BCD”, allowed = [“BCG”, “CDE”, “GEA”, “FFF”]<br>输出:true<br>解析:<br>可以堆砌成这样的金字塔:<br> A<br> / <br> G E<br> / \ / <br>B C D</p><p>因为符合(‘B’, ‘C’, ‘G’), (‘C’, ‘D’, ‘E’) 和 (‘G’, ‘E’, ‘A’) 三种规则。<br>示例 2:</p><p>输入:bottom = “AABA”, allowed = [“AAA”, “AAB”, “ABA”, “ABB”, “BAC”]<br>输出:false<br>解析:<br>无法一直堆到塔尖。<br>注意, 允许存在像 (A, B, C) 和 (A, B, D) 这样的三元组,其中 C != D。</p><p>提示:</p><p>bottom 的长度范围在 [2, 8]。<br>allowed 的长度范围在[0, 200]。<br>方块的标记字母范围为{‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’}。</p></blockquote><h3 id="python代码-2"><a href="#python代码-2" class="headerlink" title="python代码"></a>python代码</h3><pre class=" language-python"><code class="language-python"><span class="token keyword">class</span> <span class="token class-name">Solution</span><span class="token punctuation">:</span> <span class="token keyword">def</span> <span class="token function">pyramidTransition</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> bottom<span class="token punctuation">:</span> str<span class="token punctuation">,</span> allowed<span class="token punctuation">:</span> List<span class="token punctuation">[</span>str<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> bool<span class="token punctuation">:</span> table <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">for</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c <span class="token keyword">in</span> allowed<span class="token punctuation">:</span> table<span class="token punctuation">.</span>setdefault<span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>setdefault<span class="token punctuation">(</span>b<span class="token punctuation">,</span> set<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>add<span class="token punctuation">(</span>c<span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token function">get_next_bottoms</span><span class="token punctuation">(</span>bottom<span class="token punctuation">,</span> start<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">if</span> start <span class="token operator">>=</span> len<span class="token punctuation">(</span>bottom<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">:</span> <span class="token keyword">yield</span> <span class="token string">''</span> <span class="token keyword">return</span> a<span class="token punctuation">,</span> b <span class="token operator">=</span> bottom<span class="token punctuation">[</span>start<span class="token punctuation">]</span><span class="token punctuation">,</span> bottom<span class="token punctuation">[</span>start<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword">if</span> a <span class="token keyword">in</span> table <span class="token operator">and</span> b <span class="token keyword">in</span> table<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token keyword">for</span> c <span class="token keyword">in</span> table<span class="token punctuation">[</span>a<span class="token punctuation">]</span><span class="token punctuation">[</span>b<span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token keyword">for</span> res <span class="token keyword">in</span> get_next_bottoms<span class="token punctuation">(</span>bottom<span class="token punctuation">,</span> start<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">yield</span> c <span class="token operator">+</span> res <span class="token keyword">def</span> <span class="token function">check</span><span class="token punctuation">(</span>bottom<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">return</span> len<span class="token punctuation">(</span>bottom<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token operator">or</span> any<span class="token punctuation">(</span>check<span class="token punctuation">(</span>next_bottom<span class="token punctuation">)</span> <span class="token keyword">for</span> next_bottom <span class="token keyword">in</span> get_next_bottoms<span class="token punctuation">(</span>bottom<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> check<span class="token punctuation">(</span>bottom<span class="token punctuation">)</span></code></pre><h2 id="四:被围绕的区域"><a href="#四:被围绕的区域" class="headerlink" title="四:被围绕的区域"></a>四:被围绕的区域</h2><h3 id="题目描述-3"><a href="#题目描述-3" class="headerlink" title="题目描述"></a>题目描述</h3><blockquote><p>给定一个二维的矩阵,包含 ‘X’ 和 ‘O’(字母 O)。</p><p>找到所有被 ‘X’ 围绕的区域,并将这些区域里所有的 ‘O’ 用 ‘X’ 填充。</p><p>示例:</p><p>X X X X<br>X O O X<br>X X O X<br>X O X X<br>运行你的函数后,矩阵变为:</p><p>X X X X<br>X X X X<br>X X X X<br>X O X X<br>解释:</p><p>被围绕的区间不会存在于边界上,换句话说,任何边界上的 ‘O’ 都不会被填充为 ‘X’。 任何不在边界上,或不与边界上的 ‘O’ 相连的 ‘O’ 最终都会被填充为 ‘X’。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。</p></blockquote><h3 id="解题思路-1"><a href="#解题思路-1" class="headerlink" title="解题思路"></a>解题思路</h3><blockquote><p>典型的bfs问题,找到所有与边界‘O’相连的‘O’,标记为1,将其余标记为0的‘O’变换为‘X’</p></blockquote><h3 id="Java代码-2"><a href="#Java代码-2" class="headerlink" title="Java代码"></a>Java代码</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> m<span class="token punctuation">,</span> n<span class="token punctuation">;</span> <span class="token keyword">boolean</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> visited<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token keyword">int</span> d<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">{</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">solve</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> board<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>board<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> m <span class="token operator">=</span> board<span class="token punctuation">.</span>length<span class="token punctuation">;</span> n <span class="token operator">=</span> board<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> visited <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">boolean</span><span class="token punctuation">[</span>m<span class="token punctuation">]</span><span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//首先找到所有跟外界相连的O</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> m<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><</span> n<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">boolean</span> isEdge <span class="token operator">=</span> i <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> j <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> i <span class="token operator">==</span> m <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">||</span> j <span class="token operator">==</span> n <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>isEdge <span class="token operator">&&</span> board<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'O'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">dfs</span><span class="token punctuation">(</span>board<span class="token punctuation">,</span> i<span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//根据数组visited的情况进行替换</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> m<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator"><</span> n<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>visited<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">&&</span> board<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'O'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> board<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'X'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">dfs</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> board<span class="token punctuation">,</span> <span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> visited<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">[</span>y<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">4</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> newx <span class="token operator">=</span> x <span class="token operator">+</span> d<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> newy <span class="token operator">=</span> y <span class="token operator">+</span> d<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">inArea</span><span class="token punctuation">(</span>newx<span class="token punctuation">,</span> newy<span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token operator">!</span>visited<span class="token punctuation">[</span>newx<span class="token punctuation">]</span><span class="token punctuation">[</span>newy<span class="token punctuation">]</span> <span class="token operator">&&</span> board<span class="token punctuation">[</span>newx<span class="token punctuation">]</span><span class="token punctuation">[</span>newy<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'O'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">dfs</span><span class="token punctuation">(</span>board<span class="token punctuation">,</span> newx<span class="token punctuation">,</span> newy<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">boolean</span> <span class="token function">inArea</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> x <span class="token operator">>=</span> <span class="token number">0</span> <span class="token operator">&&</span> x <span class="token operator"><</span> m <span class="token operator">&&</span> y <span class="token operator">>=</span> <span class="token number">0</span> <span class="token operator">&&</span> y <span class="token operator"><</span> n<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h2 id="五:最小路径和"><a href="#五:最小路径和" class="headerlink" title="五:最小路径和"></a>五:最小路径和</h2><h3 id="题目描述-4"><a href="#题目描述-4" class="headerlink" title="题目描述"></a>题目描述</h3><blockquote><p>给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。</p><p>说明:每次只能向下或者向右移动一步。</p><p>示例:</p><p>输入:<br>[<br>[1,3,1],<br>[1,5,1],<br>[4,2,1]<br>]<br>输出: 7<br>解释: 因为路径 1→3→1→1→1 的总和最小。</p></blockquote><h3 id="解题思路-2"><a href="#解题思路-2" class="headerlink" title="解题思路"></a>解题思路</h3><blockquote><p>这个是典型的数据结构中的最小路径问题,解题方法可用dfs+动态规划。</p></blockquote><h3 id="Java代码-3"><a href="#Java代码-3" class="headerlink" title="Java代码"></a>Java代码</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">minPathSum</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> grid<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>grid <span class="token operator">==</span> null <span class="token operator">||</span> grid<span class="token punctuation">.</span>length <span class="token operator"><</span> <span class="token number">1</span> <span class="token operator">||</span> grid<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">==</span> null <span class="token operator">||</span> grid<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>length <span class="token operator"><</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">int</span> row <span class="token operator">=</span> grid<span class="token punctuation">.</span>length<span class="token punctuation">;</span> <span class="token keyword">int</span> col <span class="token operator">=</span> grid<span class="token punctuation">[</span>row <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> <span class="token keyword">int</span> dp<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">int</span><span class="token punctuation">[</span>row<span class="token punctuation">]</span><span class="token punctuation">[</span>col<span class="token punctuation">]</span><span class="token punctuation">;</span> dp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> grid<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>i <span class="token operator"><</span> row<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>i <span class="token operator"><</span> col<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> dp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> dp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>i <span class="token operator"><</span> row<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>j <span class="token operator"><</span> col<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>dp<span class="token punctuation">[</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> dp<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">+</span> grid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> dp<span class="token punctuation">[</span>row <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>col <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="python代码-3"><a href="#python代码-3" class="headerlink" title="python代码"></a>python代码</h3><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># 网上看到的大神代码,在原数组上进行最小计算,最终得到的结果</span><span class="token keyword">class</span> <span class="token class-name">Solution</span><span class="token punctuation">:</span> <span class="token keyword">def</span> <span class="token function">minPathSum</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> grid<span class="token punctuation">:</span> List<span class="token punctuation">[</span>List<span class="token punctuation">[</span>int<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> int<span class="token punctuation">:</span> h<span class="token punctuation">,</span> w<span class="token operator">=</span>len<span class="token punctuation">(</span>grid<span class="token punctuation">)</span><span class="token punctuation">,</span> len<span class="token punctuation">(</span>grid<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">for</span> j <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>w<span class="token punctuation">)</span><span class="token punctuation">:</span> grid<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">+=</span>grid<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token number">-1</span><span class="token punctuation">]</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>h<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">for</span> j <span class="token keyword">in</span> range<span class="token punctuation">(</span>w<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">if</span> j<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">:</span> grid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">+=</span>grid<span class="token punctuation">[</span>i<span class="token number">-1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token keyword">else</span><span class="token punctuation">:</span> grid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">+=</span>min<span class="token punctuation">(</span>grid<span class="token punctuation">[</span>i<span class="token number">-1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span>grid<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token number">-1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">return</span> grid<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span></code></pre>]]></content>
<tags>
<tag> python </tag>
<tag> Java </tag>
<tag> 算法 </tag>
</tags>
</entry>
<entry>
<title>懂点十八</title>
<link href="/2020/06/20/dong-dian-shi-ba/"/>
<url>/2020/06/20/dong-dian-shi-ba/</url>
<content type="html"><![CDATA[<p>[TOC]</p><h2 id="在大三你应该明白的道理"><a href="#在大三你应该明白的道理" class="headerlink" title="在大三你应该明白的道理"></a>在大三你应该明白的道理</h2><h3 id="成绩"><a href="#成绩" class="headerlink" title="成绩"></a>成绩</h3><ul><li>如果你是一个普通农民家里出生的孩子,没有什么过硬的背景,只能通过自己改变自己的命运。那么你只有选择上大学、上一个名校的研究生,这是当前逆袭最可行的办法。否则我们只能被压榨。</li><li>在大学,你的成绩真的非常重要,不要再说什么“60分万岁的话”,真不知道这些话是谁传出来的,考研,保研,能考多高就考多高,别给自己留下遗憾。</li><li>在大学学习到专业知识很重要,如果你有一技之长或者说名校学历。居前者可以自保,居后者可以生活的很潇洒。非双大学的学生可以考取有含金量的证书,认认真真做实习,毕竟是金子总会发光。</li><li>多多接触你的老师,老师真的会给你很多帮助,至少要在老师心里留下一点印象。</li><li>学习是一辈子的事情,无论在哪里都要学习,但是千万不要假装学习,因为结果不会陪你演戏。</li><li>所以,走出宿舍,远离被窝,远离游戏,认认真真的开启你的逆袭之路吧。</li></ul><h3 id="谦虚"><a href="#谦虚" class="headerlink" title="谦虚"></a>谦虚</h3><ul><li>自以为自己很牛逼的人通常会被别人当作傻逼看待,当然我的意思并不是让你不表现自己,相反,你一定要勇于表现自己,证明自己的实力,但是绝不能骄傲自大,因为那样真的很傻。</li><li>不要和不在一个层级的人去争论些什么,因为不在一个阶级,考虑问题的角度不一样,利益产生方式不同,争论是没有结果的,否则就会自贬身价,拉低自己的智商。</li><li>总之,不要装逼,装逼遭雷劈。你只需要默默的成长、变强,在适当的时候表现自己就够了。</li></ul><h3 id="明确"><a href="#明确" class="headerlink" title="明确"></a>明确</h3><ul><li>一定要清楚,这个世界很现实,也许有很多不公平,也许你真的怀才不遇,也许你真的受了委屈,也许……但是不可否认,我喜欢这个世界,我珍爱自己的生命,因为这个世界至少给我们机会,让我们去改变一些东西。</li><li>一定要有一个明确的大方向,否则你就是在原地转圈圈。</li><li>不要以锻炼为名做自己不擅长的事情,在大学,每个人都有自己的价值,你只需要找到自己热爱的事情,然后去像这个方向发展,去努力成为这个方向的金字塔顶就够了。</li><li>要谨慎地做出每一个决定,要明确自己的人生,给自己一个目标,你给我时间,我还你奇迹。</li><li>永远不要在假期的时候带很多书回家,你怎么带回去的就一定会怎么原封不动的带回来。因此,在放假前做的那些学习设想就永远只能是设想。</li></ul><h3 id="后悔"><a href="#后悔" class="headerlink" title="后悔"></a>后悔</h3><ul><li>我最后悔的一件事情就是没有继续前进担任学习部的副部,当时给自己找了很多理由退缩,在现在看来都是借口。</li></ul><h3 id="幸运"><a href="#幸运" class="headerlink" title="幸运"></a>幸运</h3><ul><li>的确,我在大学最幸运的事情就是成功加入了Android实验室并不断成长,在这里有必要提一下,Android实验室的选拔是非常严格的,因为经过几届的发展,Android实验室逐步壮大,有很多人想要加入,可以说选拔制度就是十进一,因此我非常幸运,在实验室里慢慢成长,认识了很多优秀的学长学姐,在实验室已经差不多两年了,虽然感觉自己依旧很菜,然是比起同级的学生,差距还是有的。</li><li>还有一个非常幸运的事情就是我加入了学习部,这真的是一个非常有爱的集体,虽然平时的工作很忙,学长们很严格,但是却能给我们带来成长。但我想说的是,在学习部,你可以感受到一种温暖,就像家的感觉,很开心,也很严格。不管出于什么样的原因我大二没有继续留在部门,但是我发自内心的喜欢这个集体。</li></ul><p><strong><u>致正在读大学的你们或者是即将要读大学的你们还有我,我马上就要升大三了,写点东西勉励自己</u></strong></p><blockquote><p><strong>大学时光已经过去一半,还有一半时间可以说真的很快,快到让你来不及反应就已经要毕业了,因此如果你有幸看到这篇文章,就要好好的想一想,早做准备……</strong></p></blockquote>]]></content>
<tags>
<tag> 杂记 </tag>
</tags>
</entry>
<entry>
<title>Java 基础知识(2)</title>
<link href="/2020/06/19/java-ji-chu-zhi-shi-2/"/>
<url>/2020/06/19/java-ji-chu-zhi-shi-2/</url>
<content type="html"><![CDATA[<p>[TOC]</p><h1 id="面向对象基础知识-二"><a href="#面向对象基础知识-二" class="headerlink" title="面向对象基础知识(二)"></a>面向对象基础知识(二)</h1><h2 id="string基本概念"><a href="#string基本概念" class="headerlink" title="string基本概念"></a>string基本概念</h2><h3 id="为string类对象直接赋值"><a href="#为string类对象直接赋值" class="headerlink" title="为string类对象直接赋值"></a>为string类对象直接赋值</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"www.baidu.com"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 输出字符串数据</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="利用构造方法实例化"><a href="#利用构造方法实例化" class="headerlink" title="利用构造方法实例化"></a>利用构造方法实例化</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"www.baidu.com"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 输出字符串数据</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="在string对象上使用等号比较"><a href="#在string对象上使用等号比较" class="headerlink" title="在string对象上使用等号比较"></a>在string对象上使用等号比较</h3><ul><li>在string对象上使用”==”比较</li></ul><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String stra <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值定义字符串</span> String strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 构造方法定义字符串</span> String strc <span class="token operator">=</span> strb<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 引用传递</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stra <span class="token operator">==</span> strb<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 比较结果:false</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stra <span class="token operator">==</span> strc<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 比较结果:false</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb <span class="token operator">==</span> strc<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 比较结果:true</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="实现字符串内容比较"><a href="#实现字符串内容比较" class="headerlink" title="实现字符串内容比较"></a>实现字符串内容比较</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String stra <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值定义字符串</span> String strb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 构造方法定义字符串</span> String strc <span class="token operator">=</span> strb<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 引用传递</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stra<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 比较结果:true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stra<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>strc<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 比较结果:true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>strc<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 比较结果:true</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="直接赋值时的堆内存自动引用"><a href="#直接赋值时的堆内存自动引用" class="headerlink" title="直接赋值时的堆内存自动引用"></a>直接赋值时的堆内存自动引用</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String stra <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值实例化</span> String strb <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值实例化</span> String strc <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值实例化</span> String strd <span class="token operator">=</span> <span class="token string">"yootk"</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值实例化,内容不相同</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stra <span class="token operator">==</span> strb<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 判断结果:true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stra <span class="token operator">==</span> strc<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 判断结果:true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>strb <span class="token operator">==</span> strc<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 判断结果:true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stra <span class="token operator">==</span> strd<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 判断结果:false</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="不自动保存对象池操作"><a href="#不自动保存对象池操作" class="headerlink" title="不自动保存对象池操作"></a>不自动保存对象池操作</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String stra <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 使用构造方法定义了新的内存空间,不会自动入池</span> String strb <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stra <span class="token operator">==</span> strb<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 判断结果:false</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="手工入池"><a href="#手工入池" class="headerlink" title="手工入池"></a>手工入池</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String stra <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">intern</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 使用构造方法定义新的内存空间,手工入池</span> String strb <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stra <span class="token operator">==</span> strb<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 判断结果:true</span> <span class="token punctuation">}</span>ja<span class="token punctuation">}</span></code></pre><h3 id="修改字符串对象引用"><a href="#修改字符串对象引用" class="headerlink" title="修改字符串对象引用"></a>修改字符串对象引用</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"Hello "</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 直接赋值实例化String类对象</span> str <span class="token operator">=</span> str <span class="token operator">+</span> <span class="token string">"World "</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 字符串连接,同时修改String类对象的引用关系</span> str <span class="token operator">+=</span> <span class="token string">"!!!"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 字符串连接,同时修改String类对象的引用关系</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 输出当前的String类对象内容</span> <span class="token comment" spellcheck="true">//Hello World !!!</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h2 id="string类的常用方法"><a href="#string类的常用方法" class="headerlink" title="string类的常用方法"></a>string类的常用方法</h2><h3 id="取出指定索引的字符"><a href="#取出指定索引的字符" class="headerlink" title="取出指定索引的字符"></a>取出指定索引的字符</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串对象</span> <span class="token keyword">char</span> c <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 截取第一个字符</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 输出字符</span> <span class="token comment" spellcheck="true">// h</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="字符数组与字符串的转换"><a href="#字符数组与字符串的转换" class="headerlink" title="字符数组与字符串的转换"></a>字符数组与字符串的转换</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> data <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">toCharArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 将字符串变为字符数组</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> data<span class="token punctuation">.</span>length<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 循环输出每一个字符</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>data<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">"、"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// h、e、l、l、o、</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="将字符串转为大写"><a href="#将字符串转为大写" class="headerlink" title="将字符串转为大写"></a>将字符串转为大写</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 字符串由小写字母组成</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> data <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">toCharArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 将字符串变为字符数组</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> data<span class="token punctuation">.</span>length<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 改变每一个字符的编码值</span> data<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">-=</span> <span class="token number">32</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 将全部字符数组变为String</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 将部分字符数组变为String</span> <span class="token comment" spellcheck="true">// HELLO</span> <span class="token comment" spellcheck="true">// EL</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="判断字符串是否由数字组成"><a href="#判断字符串是否由数字组成" class="headerlink" title="判断字符串是否由数字组成"></a>判断字符串是否由数字组成</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"123423432"</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">isNumber</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"字符串由数字组成!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"字符串由非数字组成!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 判断字符串是否由数字所组成 * @param temp 要判断的字符串数据 * @return 如果字符串由数字组成返回true,否则返回false */</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">boolean</span> <span class="token function">isNumber</span><span class="token punctuation">(</span>String temp<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> data <span class="token operator">=</span> temp<span class="token punctuation">.</span><span class="token function">toCharArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 将字符串变为字符数组,可以取出每一位字符进行判断</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> data<span class="token punctuation">.</span>length<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 循环判断</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>data<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">></span> <span class="token string">'9'</span> <span class="token operator">||</span> data<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator"><</span> <span class="token string">'0'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 不是数字字符范围</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 后续不再判断</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 如果全部验证通过返回true</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="字符串与字节数组的转换"><a href="#字符串与字节数组的转换" class="headerlink" title="字符串与字节数组的转换"></a>字符串与字节数组的转换</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"helloworld"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> data <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 将字符串变为字节数组</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> data<span class="token punctuation">.</span>length<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> data<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">-=</span> <span class="token number">32</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 将小写字母变为大写形式</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 全部转换</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 部分转换</span> <span class="token comment" spellcheck="true">/* HELLOWORLD WORLD */</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="字符串比较的方法"><a href="#字符串比较的方法" class="headerlink" title="字符串比较的方法"></a>字符串比较的方法</h3><ul><li>compareTo()方法</li></ul><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String stra <span class="token operator">=</span> <span class="token string">"Hello"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串对象</span> String strb <span class="token operator">=</span> <span class="token string">"HEllo"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串对象</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stra<span class="token punctuation">.</span><span class="token function">compareTo</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 32,大于0</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>stra<span class="token punctuation">.</span><span class="token function">compareTo</span><span class="token punctuation">(</span>strb<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 可以利用大小等于0的方式来判断大小</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"大于"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* 32 大于 */</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="字符串查找的方法"><a href="#字符串查找的方法" class="headerlink" title="字符串查找的方法"></a>字符串查找的方法</h3><ul><li>使用indexOf()等功能查找</li></ul><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"helloworld"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 实例化字符串对象</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"world"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 返回满足条件单词的第一个字母的索引</span> <span class="token comment" spellcheck="true">// 5</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"l"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 返回的是第一个查找到的子字符串位置</span> <span class="token comment" spellcheck="true">// 2 </span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"l"</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 从第6个元素开始查找子字符串位置</span> <span class="token comment" spellcheck="true">// 8</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">"l"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 从后向前开始查找指定字符串的位置</span> <span class="token comment" spellcheck="true">// 8</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="判断子字符串是否存在的第一种string方法"><a href="#判断子字符串是否存在的第一种string方法" class="headerlink" title="判断子字符串是否存在的第一种string方法"></a>判断子字符串是否存在的第一种string方法</h3><ul><li>利用indexOf()方法判断子字符串是否存在</li></ul><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"helloworld"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 字符串对象</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"world"</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 能找到子字符串</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"可以查询到数据"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 可以查询到数据</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="判断子字符串是否存在的第二种string方法"><a href="#判断子字符串是否存在的第二种string方法" class="headerlink" title="判断子字符串是否存在的第二种string方法"></a>判断子字符串是否存在的第二种string方法</h3><ul><li>使用contains()方法判断子字符串是否存在</li></ul><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"helloworld"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 字符串对象</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">"world"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 子字符串存在</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"可以查询到数据。"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 可以查询到数据</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="开头或结尾判断"><a href="#开头或结尾判断" class="headerlink" title="开头或结尾判断"></a>开头或结尾判断</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"##@@hello**"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 字符串对象</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string">"##"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 是否以“##”开头</span> <span class="token comment" spellcheck="true">/// ture</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string">"@@"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 从第2个索引开始是否以“@@”开头</span> <span class="token comment" spellcheck="true">// ture</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string">"**"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 是否以“**”结尾</span> <span class="token comment" spellcheck="true">// false</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="观察替换的结果"><a href="#观察替换的结果" class="headerlink" title="观察替换的结果"></a>观察替换的结果</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"helloworld"</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串</span> String resultA <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">replaceAll</span><span class="token punctuation">(</span><span class="token string">"l"</span><span class="token punctuation">,</span><span class="token string">"_"</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 全部替换</span> String resultB <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">replaceFirst</span><span class="token punctuation">(</span><span class="token string">"l"</span><span class="token punctuation">,</span><span class="token string">"_"</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 替换首个</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>resultA<span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// he__owor_d</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>resultB<span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// he_loworld</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="截取数据"><a href="#截取数据" class="headerlink" title="截取数据"></a>截取数据</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"helloworld"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串</span> String resultA <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 从指定索引截取到结尾</span> String resultB <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 截取部分子字符串</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>resultA<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// world</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>resultB<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// hello</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="字符串拆分单词"><a href="#字符串拆分单词" class="headerlink" title="字符串拆分单词"></a>字符串拆分单词</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"hello i like you"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串,中间使用空格作为间隔</span> String result<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 字符串拆分</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> result<span class="token punctuation">.</span>length<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 循环输出</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>result<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">"、"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// hello、i、like、you、</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="字符串拆分字母"><a href="#字符串拆分字母" class="headerlink" title="字符串拆分字母"></a>字符串拆分字母</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"hello i like you"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串</span> String result<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 字符串全部拆分</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> result<span class="token punctuation">.</span>length<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 循环输出</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>result<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">"、"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// h、e、l、l、o、 、i、 、l、i、k、e、 、y、o、u、</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="拆分为指定的个数"><a href="#拆分为指定的个数" class="headerlink" title="拆分为指定的个数"></a>拆分为指定的个数</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"hello i like you"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串,中间使用空格作为间隔</span> String result<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">" "</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 字符串拆分</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> result<span class="token punctuation">.</span>length<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 循环输出</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>result<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* hello i like you */</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="拆分例子"><a href="#拆分例子" class="headerlink" title="拆分例子"></a>拆分例子</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"张三:20|李四:21|王五:22"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串</span> String result<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">"\\|"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 第一次拆分</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> result<span class="token punctuation">.</span>length<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String temp<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> result<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">":"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 第二次拆分</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"姓名:"</span> <span class="token operator">+</span> temp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">",年龄:"</span> <span class="token operator">+</span> temp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* 姓名:张三,年龄:20 姓名:李四,年龄:21 姓名:王五,年龄:22 */</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="字符串连接"><a href="#字符串连接" class="headerlink" title="字符串连接"></a>字符串连接</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token string">"world"</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 等价于“+”</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">/*hello world*/</span></code></pre><h3 id="转小写与转大写操作"><a href="#转小写与转大写操作" class="headerlink" title="转小写与转大写操作"></a>转小写与转大写操作</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"(*(*Hello(*(*"</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 转大写后输出</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 转小写后输出</span> <span class="token comment" spellcheck="true">/* (*(*HELLO(*(* (*(*hello(*(* */</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="去掉左右空格"><a href="#去掉左右空格" class="headerlink" title="去掉左右空格"></a>去掉左右空格</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">" hello world "</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串,包含空格</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"【"</span> <span class="token operator">+</span> str <span class="token operator">+</span> <span class="token string">"】"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 原始字符串</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"【"</span> <span class="token operator">+</span> str<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"】"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 去掉空格后的字符串</span> <span class="token comment" spellcheck="true">/* 【 hello world 】 【hello world】 */</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="取消掉全部空格"><a href="#取消掉全部空格" class="headerlink" title="取消掉全部空格"></a>取消掉全部空格</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">" hello world "</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串,包含空格</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">replaceAll</span><span class="token punctuation">(</span><span class="token string">" "</span><span class="token punctuation">,</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 去掉空格后的字符串</span> <span class="token comment" spellcheck="true">// helloworld</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="取得字符串长度"><a href="#取得字符串长度" class="headerlink" title="取得字符串长度"></a>取得字符串长度</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"helloworld"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 取得字符串长度</span> <span class="token comment" spellcheck="true">// 10</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 判断字符串对象的内容是否为空字符串(不是null)</span> <span class="token comment" spellcheck="true">// false</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 判断字符串常量的内容是否为空字符串(不是null)</span> <span class="token comment" spellcheck="true">// true</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="实现首字母大写的操作"><a href="#实现首字母大写的操作" class="headerlink" title="实现首字母大写的操作"></a>实现首字母大写的操作</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String str <span class="token operator">=</span> <span class="token string">"wanghao"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 定义字符串</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token function">initcap</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 调用initcap()方法</span> <span class="token comment" spellcheck="true">// Wanghao</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 实现首字母大写的操作 * @param temp 要转换的字符串数据 * @return 将首字母大写后返回 */</span> <span class="token keyword">public</span> <span class="token keyword">static</span> String <span class="token function">initcap</span><span class="token punctuation">(</span>String temp<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 利用substring(0,1)取出字符串的第一位后将其变为大写,再连接剩余的字符串</span> <span class="token keyword">return</span> temp<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> temp<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre>]]></content>
<tags>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>Java 基础知识(1)</title>
<link href="/2020/06/19/java-ji-chu-zhi-shi-1/"/>
<url>/2020/06/19/java-ji-chu-zhi-shi-1/</url>
<content type="html"><![CDATA[<p>[TOC]</p><h1 id="面向对象基础知识(一)"><a href="#面向对象基础知识(一)" class="headerlink" title="面向对象基础知识(一)"></a>面向对象基础知识(一)</h1><ul><li><blockquote><p>类与对象</p><ul><li>定义一个自己的类</li><li>使用类——在主类中使用Book类</li></ul></blockquote></li></ul><h2 id="类与对象"><a href="#类与对象" class="headerlink" title="类与对象"></a>类与对象</h2><h3 id="自定义的类"><a href="#自定义的类" class="headerlink" title="自定义的类"></a>自定义的类</h3><ul><li><strong>通过class标识符进行声明</strong></li></ul><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Book</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 定义一个新的类</span> String title<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 书的名字</span> <span class="token keyword">double</span> price<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 书的价格</span> <span class="token comment" spellcheck="true">/** * 输出对象完整信息 */</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 此方法将由对象调用</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"图书名称:"</span> <span class="token operator">+</span> title <span class="token operator">+</span> <span class="token string">",价格:"</span> <span class="token operator">+</span> price<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="在主类中使用类"><a href="#在主类中使用类" class="headerlink" title="在主类中使用类"></a>在主类中使用类</h3><ul><li><strong>在主类中使用Book类</strong></li></ul><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Book</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 定义一个新的类</span> String title<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 书的名字</span> <span class="token keyword">double</span> price<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 书的价格</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 此方法将由对象调用</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"图书名称:"</span> <span class="token operator">+</span> title <span class="token operator">+</span> <span class="token string">",价格:"</span> <span class="token operator">+</span> price<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Book bk <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Book</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 声明并实例化对象</span> bk<span class="token punctuation">.</span>title <span class="token operator">=</span> <span class="token string">"Java开发"</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 操作属性内容</span> bk<span class="token punctuation">.</span>price <span class="token operator">=</span> <span class="token number">89.9</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 操作属性内容</span> bk<span class="token punctuation">.</span><span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 调用类中的getInfo()方法</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="以分步式的方式实例化对象"><a href="#以分步式的方式实例化对象" class="headerlink" title="以分步式的方式实例化对象"></a>以分步式的方式实例化对象</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Book bk <span class="token operator">=</span> null<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 声明对象</span> bk <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Book</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 实例化对象(开辟了堆内存)</span> bk<span class="token punctuation">.</span>title <span class="token operator">=</span> <span class="token string">"Java开发"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 操作属性内容</span> bk<span class="token punctuation">.</span>price <span class="token operator">=</span> <span class="token number">89.9</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 操作属性内容</span> bk<span class="token punctuation">.</span><span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 调用类中的getInfo()方法</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h2 id="封装性初步分析"><a href="#封装性初步分析" class="headerlink" title="封装性初步分析"></a>封装性初步分析</h2><h3 id="没有封装的代码"><a href="#没有封装的代码" class="headerlink" title="没有封装的代码"></a>没有封装的代码</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Book</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 定义一个新的类</span> String title<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 书的名字</span> <span class="token keyword">double</span> price<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 书的价格</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 此方法将由对象调用</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"图书名称:"</span> <span class="token operator">+</span> title <span class="token operator">+</span> <span class="token string">",价格:"</span> <span class="token operator">+</span> price<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Book book <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Book</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 声明并实例化对象</span> book<span class="token punctuation">.</span>title <span class="token operator">=</span> <span class="token string">"Java开发"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 设置属性内容</span> book<span class="token punctuation">.</span>price <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">89.9</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 设置属性内容</span> book<span class="token punctuation">.</span><span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 调用方法</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="使用private封装属性"><a href="#使用private封装属性" class="headerlink" title="使用private封装属性"></a>使用private封装属性</h3><pre class=" language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">Book</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 定义一个新的类</span> <span class="token keyword">private</span> String title<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 书的名字</span> <span class="token keyword">private</span> <span class="token keyword">double</span> price<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 书的价格</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 此方法将由对象调用</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"图书名称:"</span> <span class="token operator">+</span> title <span class="token operator">+</span> <span class="token string">",价格:"</span> <span class="token operator">+</span> price<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Book book <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Book</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 声明并实例化对象</span> book<span class="token punctuation">.</span>title <span class="token operator">=</span> <span class="token string">"Java开发"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 设置属性内容</span> book<span class="token punctuation">.</span>price <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">89.9</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 设置属性内容</span> book<span class="token punctuation">.</span><span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 调用方法</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h2 id="为封装属性设置取数据操作"><a href="#为封装属性设置取数据操作" class="headerlink" title="为封装属性设置取数据操作"></a>为封装属性设置取数据操作</h2><ul><li>为Book类中的封装属性设置setter、getter操作</li></ul><pre class=" language-java"><code class="language-java"> <span class="token keyword">class</span> <span class="token class-name">Book</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 定义一个新的类</span> <span class="token keyword">private</span> String title<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 书的名字</span> <span class="token keyword">private</span> <span class="token keyword">double</span> price<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 书的价格</span> <span class="token comment" spellcheck="true">/** * 设置或修改title属性内容 * @param t 接收要设置的数据 */</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setTitle</span><span class="token punctuation">(</span>String t<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 设置title属性内容</span> title <span class="token operator">=</span> t<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 设置或修改price属性内容 * @param p 接收要设置的数据 */</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setPrice</span><span class="token punctuation">(</span><span class="token keyword">double</span> p<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 设置price属性内容</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>p <span class="token operator">></span> <span class="token number">0.0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 进行数据验证</span> price <span class="token operator">=</span> p <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 取得title属性内容 * @return title属性数据 */</span> <span class="token keyword">public</span> String <span class="token function">getTitle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 取得title属性内容</span> <span class="token keyword">return</span> title<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 取得price属性内容 * @return price属性数据 */</span> <span class="token keyword">public</span> <span class="token keyword">double</span> <span class="token function">getPrice</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 取得price属性内容</span> <span class="token keyword">return</span> price<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 输出对象完整信息 */</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 此方法将由对象调用</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"图书名称:"</span> <span class="token operator">+</span> title <span class="token operator">+</span> <span class="token string">",价格:"</span> <span class="token operator">+</span> price<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> Book book <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Book</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 声明并实例化对象</span> book<span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span><span class="token string">"Java开发"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 设置属性内容</span> book<span class="token punctuation">.</span><span class="token function">setPrice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">89.9</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 设置属性内容</span> book<span class="token punctuation">.</span><span class="token function">getInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 调用方法</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre>]]></content>
<tags>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>Numpy学习笔记</title>
<link href="/2020/06/19/numpy-xue-xi-bi-ji/"/>
<url>/2020/06/19/numpy-xue-xi-bi-ji/</url>
<content type="html"><![CDATA[<p>[TOC]</p><h1 id="Numpy方法"><a href="#Numpy方法" class="headerlink" title="Numpy方法"></a>Numpy方法</h1><h3 id="np-ceil-x-y-限制元素范围"><a href="#np-ceil-x-y-限制元素范围" class="headerlink" title="np.ceil(x, y) 限制元素范围"></a>np.ceil(x, y) 限制元素范围</h3><ul><li>x 输入的数据</li><li>y float型,每个元素的上限</li></ul><pre class=" language-python"><code class="language-python">housing<span class="token punctuation">[</span><span class="token string">"income_cat"</span><span class="token punctuation">]</span> <span class="token operator">=</span> np<span class="token punctuation">.</span>ceil<span class="token punctuation">(</span>housing<span class="token punctuation">[</span><span class="token string">"median_income"</span><span class="token punctuation">]</span> <span class="token operator">/</span> <span class="token number">1.5</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 每个元素都除1.5</span></code></pre><h3 id="permutation-x-随机生成一个排列或返回一个range"><a href="#permutation-x-随机生成一个排列或返回一个range" class="headerlink" title="permutation(x) 随机生成一个排列或返回一个range"></a>permutation(x) 随机生成一个排列或返回一个range</h3><p>如果x是一个多维数组,则只会沿着它的第一个索引进行混洗。</p><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> npshuffle_index <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>permutation<span class="token punctuation">(</span><span class="token number">60000</span><span class="token punctuation">)</span>X_train<span class="token punctuation">,</span> y_train <span class="token operator">=</span> X_train<span class="token punctuation">[</span>shuffle_index<span class="token punctuation">]</span><span class="token punctuation">,</span> y_train<span class="token punctuation">[</span>shuffle_index<span class="token punctuation">]</span></code></pre><h3 id="numpy-argmax-返回沿轴的最大值的索引"><a href="#numpy-argmax-返回沿轴的最大值的索引" class="headerlink" title="numpy.argmax() 返回沿轴的最大值的索引"></a>numpy.argmax() 返回沿轴的最大值的索引</h3><p>返回沿轴的最大值的索引。</p><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># some_digit_scores 内容</span><span class="token comment" spellcheck="true"># array([[-311402.62954431, -363517.28355739, -446449.5306454 ,</span><span class="token comment" spellcheck="true"># -183226.61023518, -414337.15339485, 161855.74572176,</span><span class="token comment" spellcheck="true"># -452576.39616343, -471957.14962573, -518542.33997148,</span><span class="token comment" spellcheck="true"># -536774.63961222]])</span>np<span class="token punctuation">.</span>argmax<span class="token punctuation">(</span>some_digit_scores<span class="token punctuation">)</span><span class="token comment" spellcheck="true"># Out</span><span class="token comment" spellcheck="true"># 5</span></code></pre><ul><li>a : array_like; 输入数组</li><li>axis : int, optional; 默认情况下,索引是放在平面数组中,否则沿着指定的轴。</li><li>out : array, optional; 如果提供,结果将被插入到这个数组中。它应该是适当的形状和dtype。</li></ul><h3 id="np-linalg-inv-计算矩阵的逆"><a href="#np-linalg-inv-计算矩阵的逆" class="headerlink" title="np.linalg.inv() 计算矩阵的逆"></a>np.linalg.inv() 计算矩阵的逆</h3><pre class=" language-python"><code class="language-python">X_b <span class="token operator">=</span> np<span class="token punctuation">.</span>c_<span class="token punctuation">[</span>np<span class="token punctuation">.</span>ones<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> X<span class="token punctuation">]</span> <span class="token comment" spellcheck="true"># add x0 = 1 to each instance</span>theta_best <span class="token operator">=</span> np<span class="token punctuation">.</span>linalg<span class="token punctuation">.</span>inv<span class="token punctuation">(</span>X_b<span class="token punctuation">.</span>T<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>X_b<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>dot<span class="token punctuation">(</span>X_b<span class="token punctuation">.</span>T<span class="token punctuation">)</span><span class="token punctuation">.</span>dot<span class="token punctuation">(</span>y<span class="token punctuation">)</span></code></pre><ul><li>a : (…, M, M) array_like;被求逆的矩阵</li></ul><h3 id="numpy-dot-a-b-out-None-计算两个数组的点积"><a href="#numpy-dot-a-b-out-None-计算两个数组的点积" class="headerlink" title="numpy.dot(a, b, out=None) 计算两个数组的点积"></a>numpy.dot(a, b, out=None) 计算两个数组的点积</h3><pre class=" language-python"><code class="language-python"><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>dot<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token number">12</span><span class="token comment" spellcheck="true"># Neither argument is complex-conjugated:</span><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>dot<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">2j</span><span class="token punctuation">,</span> <span class="token number">3j</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">2j</span><span class="token punctuation">,</span> <span class="token number">3j</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">13</span><span class="token operator">+</span><span class="token number">0j</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true"># For 2-D arrays it is the matrix product:</span><span class="token operator">>></span><span class="token operator">></span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">>></span><span class="token operator">></span> b <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> a <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">3</span><span class="token operator">*</span><span class="token number">4</span><span class="token operator">*</span><span class="token number">5</span><span class="token operator">*</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> b <span class="token operator">=</span> np<span class="token punctuation">.</span>arange<span class="token punctuation">(</span><span class="token number">3</span><span class="token operator">*</span><span class="token number">4</span><span class="token operator">*</span><span class="token number">5</span><span class="token operator">*</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token number">499128</span><span class="token operator">>></span><span class="token operator">></span> sum<span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token operator">*</span> b<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token number">499128</span></code></pre><ul><li>a : array_like;First argument.</li><li>b : array_like;Second argument.</li></ul><h3 id="numpy-ndarray-T-计算矩阵的转置"><a href="#numpy-ndarray-T-计算矩阵的转置" class="headerlink" title="numpy.ndarray.T() 计算矩阵的转置"></a>numpy.ndarray.T() 计算矩阵的转置</h3><p>与<code>self.transpose()</code>相同,如果<code>self.ndim < 2</code>则返回它自身。</p><pre class=" language-python"><code class="language-python"><span class="token operator">>></span><span class="token operator">></span> x <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> xarray<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> x<span class="token punctuation">.</span>Tarray<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> x <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> xarray<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> x<span class="token punctuation">.</span>Tarray<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h3 id="numpy-random-seed-生成器种子"><a href="#numpy-random-seed-生成器种子" class="headerlink" title="numpy.random.seed() 生成器种子"></a>numpy.random.seed() 生成器种子</h3><p>该方法由<code>RandomState</code>初始化,它可以被重新设置。</p><pre class=" language-python"><code class="language-python">np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>seed<span class="token punctuation">(</span><span class="token number">42</span><span class="token punctuation">)</span>theta <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># random initialization</span></code></pre><ul><li>seed : int or array_like, optional;必须为32位无符号整数。</li></ul><h3 id="numpy-random-randn-从标准正太分布返回样本"><a href="#numpy-random-randn-从标准正太分布返回样本" class="headerlink" title="numpy.random.randn() 从标准正太分布返回样本"></a>numpy.random.randn() 从标准正太分布返回样本</h3><pre class=" language-python"><code class="language-python"><span class="token operator">>></span><span class="token operator">></span> theta <span class="token operator">=</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">4.21509616</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">2.77011339</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><h6 id="参数"><a href="#参数" class="headerlink" title="参数"></a>参数</h6><ul><li>d0, d1, …, dn : int, optional;返回的数组维度,应该都是正值。如果没有给出,将返回一个Python float值。</li></ul><h6 id="返回值"><a href="#返回值" class="headerlink" title="返回值"></a>返回值</h6><ul><li>Z : ndarray or float;一个经过标准正态分布抽样的,<code>(d0, d1, ..., dn)</code>维度的浮点数组。</li></ul><h3 id="numpy-array-创建一个数组"><a href="#numpy-array-创建一个数组" class="headerlink" title="numpy.array() 创建一个数组"></a>numpy.array() 创建一个数组</h3><pre class=" language-python"><code class="language-python">theta_path_bgd <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>theta_path_bgd<span class="token punctuation">)</span>theta_path_sgd <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>theta_path_sgd<span class="token punctuation">)</span>theta_path_mgd <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span>theta_path_mgd<span class="token punctuation">)</span></code></pre><ul><li>object : array_like</li><li>dtype : data-type, optional</li></ul><h3 id="numpy-random-rand-nmbsp-生成给定shap的随机值"><a href="#numpy-random-rand-nmbsp-生成给定shap的随机值" class="headerlink" title="numpy.random.rand()&nmbsp;生成给定shap的随机值"></a>numpy.random.rand()&nmbsp;生成给定shap的随机值</h3><pre class=" language-python"><code class="language-python">m <span class="token operator">=</span> <span class="token number">100</span>X <span class="token operator">=</span> <span class="token number">6</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>rand<span class="token punctuation">(</span>m<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">3</span>y <span class="token operator">=</span> <span class="token number">0.5</span> <span class="token operator">*</span> X<span class="token operator">**</span><span class="token number">2</span> <span class="token operator">+</span> X <span class="token operator">+</span> <span class="token number">2</span> <span class="token operator">+</span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>randn<span class="token punctuation">(</span>m<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>random<span class="token punctuation">.</span>rand<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0.14022471</span><span class="token punctuation">,</span> <span class="token number">0.96360618</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true">#random</span> <span class="token punctuation">[</span> <span class="token number">0.37601032</span><span class="token punctuation">,</span> <span class="token number">0.25528411</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token comment" spellcheck="true">#random</span> <span class="token punctuation">[</span> <span class="token number">0.49313049</span><span class="token punctuation">,</span> <span class="token number">0.94909878</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">#random</span></code></pre><ul><li>d0, d1, …, dn : int, optional;返回的数组维度,必须是正值。如果为空,则返回一个Python float值。</li></ul><h3 id="numpy-linspace-在指定区间返回间隔均匀的样本-start-stop"><a href="#numpy-linspace-在指定区间返回间隔均匀的样本-start-stop" class="headerlink" title="numpy.linspace() 在指定区间返回间隔均匀的样本[start, stop]"></a>numpy.linspace() 在指定区间返回间隔均匀的样本[start, stop]</h3><pre class=" language-python"><code class="language-python">X_new<span class="token operator">=</span>np<span class="token punctuation">.</span>linspace<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>X_new_poly <span class="token operator">=</span> poly_features<span class="token punctuation">.</span>transform<span class="token punctuation">(</span>X_new<span class="token punctuation">)</span>y_new <span class="token operator">=</span> lin_reg<span class="token punctuation">.</span>predict<span class="token punctuation">(</span>X_new_poly<span class="token punctuation">)</span>plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>X<span class="token punctuation">,</span> y<span class="token punctuation">,</span> <span class="token string">"b."</span><span class="token punctuation">)</span>plt<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>X_new<span class="token punctuation">,</span> y_new<span class="token punctuation">,</span> <span class="token string">"r-"</span><span class="token punctuation">,</span> linewidth<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">"Predictions"</span><span class="token punctuation">)</span>plt<span class="token punctuation">.</span>xlabel<span class="token punctuation">(</span><span class="token string">"$x_1$"</span><span class="token punctuation">,</span> fontsize<span class="token operator">=</span><span class="token number">18</span><span class="token punctuation">)</span>plt<span class="token punctuation">.</span>ylabel<span class="token punctuation">(</span><span class="token string">"$y$"</span><span class="token punctuation">,</span> rotation<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> fontsize<span class="token operator">=</span><span class="token number">18</span><span class="token punctuation">)</span>plt<span class="token punctuation">.</span>legend<span class="token punctuation">(</span>loc<span class="token operator">=</span><span class="token string">"upper left"</span><span class="token punctuation">,</span> fontsize<span class="token operator">=</span><span class="token number">14</span><span class="token punctuation">)</span>plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span>save_fig<span class="token punctuation">(</span><span class="token string">"quadratic_predictions_plot"</span><span class="token punctuation">)</span>plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre><ul><li>start : scalar;序列的起始值</li><li>stop : scalar;序列的结束值</li><li>num : int, optional;要生成的样本数量,默认为50个。</li><li>endpoint : bool, optional;若为True则包括结束值,否则不包括结束值,即[start, stop)区间。默认为True。</li><li>dtype : dtype, optional;输出数组的类型,若未给出则从输入数据推断类型。</li></ul><h3 id="meshgrid-从坐标向量返回坐标矩阵"><a href="#meshgrid-从坐标向量返回坐标矩阵" class="headerlink" title="meshgrid() 从坐标向量返回坐标矩阵"></a>meshgrid() 从坐标向量返回坐标矩阵</h3><pre class=" language-python"><code class="language-python"><span class="token operator">>></span><span class="token operator">></span> nx<span class="token punctuation">,</span> ny <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> x <span class="token operator">=</span> np<span class="token punctuation">.</span>linspace<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> nx<span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> y <span class="token operator">=</span> np<span class="token punctuation">.</span>linspace<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> ny<span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> xv<span class="token punctuation">,</span> yv <span class="token operator">=</span> np<span class="token punctuation">.</span>meshgrid<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> xvarray<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> yvarray<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> xv<span class="token punctuation">,</span> yv <span class="token operator">=</span> np<span class="token punctuation">.</span>meshgrid<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> sparse<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># make sparse output arrays</span><span class="token operator">>></span><span class="token operator">></span> xvarray<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">.</span> <span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> yvarray<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token number">0</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span> <span class="token number">1</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><ul><li>x1, x2,…, xn : array_like;代表网格坐标的一维数组。</li><li>indexing : {‘xy’, ‘ij’}, optional;输出的笛卡儿(’xy’,默认)或矩阵(’ij’)索引。</li><li>sparse : bool, optional;如果为True则返回稀疏矩阵以减少内存,默认为False。</li></ul><h3 id="norm-矩阵或向量范数"><a href="#norm-矩阵或向量范数" class="headerlink" title="norm() 矩阵或向量范数"></a>norm() 矩阵或向量范数</h3><pre class=" language-python"><code class="language-python">t1a<span class="token punctuation">,</span> t1b<span class="token punctuation">,</span> t2a<span class="token punctuation">,</span> t2b <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1.5</span><span class="token punctuation">,</span> <span class="token number">1.5</span><span class="token comment" spellcheck="true"># ignoring bias term</span>t1s <span class="token operator">=</span> np<span class="token punctuation">.</span>linspace<span class="token punctuation">(</span>t1a<span class="token punctuation">,</span> t1b<span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span>t2s <span class="token operator">=</span> np<span class="token punctuation">.</span>linspace<span class="token punctuation">(</span>t2a<span class="token punctuation">,</span> t2b<span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span>t1<span class="token punctuation">,</span> t2 <span class="token operator">=</span> np<span class="token punctuation">.</span>meshgrid<span class="token punctuation">(</span>t1s<span class="token punctuation">,</span> t2s<span class="token punctuation">)</span>T <span class="token operator">=</span> np<span class="token punctuation">.</span>c_<span class="token punctuation">[</span>t1<span class="token punctuation">.</span>ravel<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> t2<span class="token punctuation">.</span>ravel<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span>Xr <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">0.3</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0.1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>yr <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> Xr<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token number">0.5</span> <span class="token operator">*</span> Xr<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span>J <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">/</span>len<span class="token punctuation">(</span>Xr<span class="token punctuation">)</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>sum<span class="token punctuation">(</span><span class="token punctuation">(</span>T<span class="token punctuation">.</span>dot<span class="token punctuation">(</span>Xr<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token operator">-</span> yr<span class="token punctuation">.</span>T<span class="token punctuation">)</span><span class="token operator">**</span><span class="token number">2</span><span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>t1<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>N1 <span class="token operator">=</span> np<span class="token punctuation">.</span>linalg<span class="token punctuation">.</span>norm<span class="token punctuation">(</span>T<span class="token punctuation">,</span> ord<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>t1<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>N2 <span class="token operator">=</span> np<span class="token punctuation">.</span>linalg<span class="token punctuation">.</span>norm<span class="token punctuation">(</span>T<span class="token punctuation">,</span> ord<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>reshape<span class="token punctuation">(</span>t1<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>t_min_idx <span class="token operator">=</span> np<span class="token punctuation">.</span>unravel_index<span class="token punctuation">(</span>np<span class="token punctuation">.</span>argmin<span class="token punctuation">(</span>J<span class="token punctuation">)</span><span class="token punctuation">,</span> J<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>t1_min<span class="token punctuation">,</span> t2_min <span class="token operator">=</span> t1<span class="token punctuation">[</span>t_min_idx<span class="token punctuation">]</span><span class="token punctuation">,</span> t2<span class="token punctuation">[</span>t_min_idx<span class="token punctuation">]</span>t_init <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0.25</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><ul><li>x : array_like;输入的数组,如果<code>axis</code>是None,则<code>x</code>必须是1-D或2-D。</li><li>ord : {non-zero int, inf, -inf, ‘fro’, ‘nuc’}, optional;范数的顺序,inf表示numpy的inf对象。</li><li>axis : {int, 2-tuple of ints, None}, optional</li><li>keepdims : bool, optional</li></ul><p>以下范数可以被计算:</p><table><thead><tr><th>ord</th><th>norm for matrices</th><th>norm for vectors</th></tr></thead><tbody><tr><td>None</td><td>Frobenius norm</td><td>2-norm</td></tr><tr><td>‘fro’</td><td>Frobenius norm</td><td>–</td></tr><tr><td>‘nuc’</td><td>nuclear norm</td><td>–</td></tr><tr><td>inf</td><td>max(sum(abs(x), axis=1))</td><td>max(abs(x))</td></tr><tr><td>-inf</td><td>min(sum(abs(x), axis=1))</td><td>min(abs(x))</td></tr><tr><td>0</td><td>–</td><td>sum(x != 0)</td></tr><tr><td>1</td><td>max(sum(abs(x), axis=0))</td><td>as below</td></tr><tr><td>-1</td><td>min(sum(abs(x), axis=0))</td><td>as below</td></tr><tr><td>2</td><td>2-norm (largest sing. value)</td><td>as below</td></tr><tr><td>-2</td><td>smallest singular value</td><td>as below</td></tr><tr><td>other</td><td>–</td><td>sum(abs(x)<strong>ord)</strong>(1./ord)</td></tr></tbody></table><p>对于<code>ord <= 0</code>的值,它严格来说不是数学规范的范数,但它作为数值目的任然有用。</p><h3 id="unravel-index-将平面索引或平面索引数组转换为坐标数组的元组"><a href="#unravel-index-将平面索引或平面索引数组转换为坐标数组的元组" class="headerlink" title="unravel_index() 将平面索引或平面索引数组转换为坐标数组的元组"></a>unravel_index() 将平面索引或平面索引数组转换为坐标数组的元组</h3><pre class=" language-python"><code class="language-python"><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>unravel_index<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">41</span><span class="token punctuation">,</span> <span class="token number">37</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">(</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>unravel_index<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">31</span><span class="token punctuation">,</span> <span class="token number">41</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">,</span> order<span class="token operator">=</span><span class="token string">'F'</span><span class="token punctuation">)</span><span class="token punctuation">(</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>unravel_index<span class="token punctuation">(</span><span class="token number">1621</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span></code></pre><ul><li>indices : array_like;一个整数数组,其元素是索引到维数组dims的平坦版本中。</li><li>dims : tuple of ints;用于分解索引的数组的形状。</li><li>order : {‘C’, ‘F’}, optional;决定<code>indices</code>应该按row-major (C-style) or column-major (Fortran-style) 顺序。</li></ul><h3 id="mean-计算沿指定轴的算术平均值"><a href="#mean-计算沿指定轴的算术平均值" class="headerlink" title="mean() 计算沿指定轴的算术平均值"></a>mean() 计算沿指定轴的算术平均值</h3><pre class=" language-python"><code class="language-python"><span class="token operator">>></span><span class="token operator">></span> a <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>mean<span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token number">2.5</span><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>mean<span class="token punctuation">(</span>a<span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">2</span><span class="token punctuation">.</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">.</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>mean<span class="token punctuation">(</span>a<span class="token punctuation">,</span> axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token number">1.5</span><span class="token punctuation">,</span> <span class="token number">3.5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> a <span class="token operator">=</span> np<span class="token punctuation">.</span>zeros<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">512</span><span class="token operator">*</span><span class="token number">512</span><span class="token punctuation">)</span><span class="token punctuation">,</span> dtype<span class="token operator">=</span>np<span class="token punctuation">.</span>float32<span class="token punctuation">)</span><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">1.0</span><span class="token operator">>></span><span class="token operator">></span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0.1</span><span class="token operator">>></span><span class="token operator">></span> np<span class="token punctuation">.</span>mean<span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token number">0.54999924</span></code></pre><ul><li>a : array_like;包含要求平均值的数组,如果不是数组,则尝试进行转换。</li><li>axis : None or int or tuple of ints, optional;计算平均值的轴,默认计算扁平数组。</li><li>dtype : data-type, optional;用于计算平均值的类型。</li><li>out : ndarray, optional</li></ul>]]></content>
<tags>
<tag> python </tag>
<tag> Machine Learning </tag>
</tags>
</entry>
<entry>
<title>Pandas学习笔记</title>
<link href="/2020/06/18/pandas-xue-xi-bi-ji/"/>
<url>/2020/06/18/pandas-xue-xi-bi-ji/</url>
<content type="html"><![CDATA[<p>[TOC]</p><h2 id="Pandas方法"><a href="#Pandas方法" class="headerlink" title="Pandas方法"></a>Pandas方法</h2><p>使用pandas首先要导入pandas包。</p><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd <span class="token operator">//</span>标准的导入pandas的代码</code></pre><h3 id="读入csv文件"><a href="#读入csv文件" class="headerlink" title="读入csv文件"></a>读入csv文件</h3><pre class=" language-python"><code class="language-python">trainData <span class="token operator">=</span> pd<span class="token punctuation">.</span>read_csv<span class="token punctuation">(</span><span class="token string">"./Data/train.csv"</span><span class="token punctuation">)</span> <span class="token operator">//</span><span class="token punctuation">.</span><span class="token operator">/</span>Data<span class="token operator">/</span> 表示路径,train表示文件名</code></pre><h3 id="标题获取文件中的前5行数据"><a href="#标题获取文件中的前5行数据" class="headerlink" title="标题获取文件中的前5行数据"></a>标题获取文件中的前5行数据</h3><pre class=" language-python"><code class="language-python">trainData<span class="token punctuation">.</span>head<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">//</span><span class="token punctuation">.</span>head<span class="token punctuation">(</span><span class="token punctuation">)</span>是pandas中的一个方法,可以获取文件中的前<span class="token number">5</span>行数据</code></pre><p><img src="https://img-blog.csdnimg.cn/20190521193906796.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg5NTQ4Mg==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><h3 id="info-迅速获取数据描述"><a href="#info-迅速获取数据描述" class="headerlink" title="info() 迅速获取数据描述"></a>info() 迅速获取数据描述</h3><pre class=" language-python"><code class="language-python">trainData<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">//</span> 方法就不具体介绍了 下面给出运行结果<span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span> 运行结果<span class="token operator"><</span><span class="token keyword">class</span> <span class="token string">'pandas.core.frame.DataFrame'</span><span class="token operator">></span>RangeIndex<span class="token punctuation">:</span> <span class="token number">891</span> entries<span class="token punctuation">,</span> <span class="token number">0</span> to <span class="token number">890</span>Data columns <span class="token punctuation">(</span>total <span class="token number">10</span> columns<span class="token punctuation">)</span><span class="token punctuation">:</span>Pclass <span class="token number">891</span> non<span class="token operator">-</span>null int64Name <span class="token number">891</span> non<span class="token operator">-</span>null objectSex <span class="token number">891</span> non<span class="token operator">-</span>null objectAge <span class="token number">714</span> non<span class="token operator">-</span>null float64SibSp <span class="token number">891</span> non<span class="token operator">-</span>null int64Parch <span class="token number">891</span> non<span class="token operator">-</span>null int64Ticket <span class="token number">891</span> non<span class="token operator">-</span>null objectFare <span class="token number">891</span> non<span class="token operator">-</span>null float64Cabin <span class="token number">204</span> non<span class="token operator">-</span>null objectEmbarked <span class="token number">889</span> non<span class="token operator">-</span>null objectdtypes<span class="token punctuation">:</span> float64<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> int64<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span> object<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>memory usage<span class="token punctuation">:</span> <span class="token number">69.7</span><span class="token operator">+</span> KB</code></pre><p><img src="https://img-blog.csdnimg.cn/20190915110156432.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg5NTQ4Mg==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><h3 id="drop"><a href="#drop" class="headerlink" title=".drop( , , )"></a>.drop( , , )</h3><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">#删除name</span>allData<span class="token punctuation">.</span>drop<span class="token punctuation">(</span><span class="token string">"Name"</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token operator">//</span>第一个参数为要删除的内容,第二第三个参数的形式固定</code></pre><h3 id="替换文件中的内容"><a href="#替换文件中的内容" class="headerlink" title="替换文件中的内容"></a>替换文件中的内容</h3><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">#可以考虑替换为出现的次数</span>allData<span class="token punctuation">[</span><span class="token string">'SibSp'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>value_counts<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">//</span> 替换SibSp中缺失的数据为出现的次数<span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span><span class="token operator">/</span>运行结果<span class="token number">0</span> <span class="token number">891</span><span class="token number">1</span> <span class="token number">319</span><span class="token number">2</span> <span class="token number">42</span><span class="token number">4</span> <span class="token number">22</span><span class="token number">3</span> <span class="token number">20</span><span class="token number">8</span> <span class="token number">9</span><span class="token number">5</span> <span class="token number">6</span>Name<span class="token punctuation">:</span> SibSp<span class="token punctuation">,</span> dtype<span class="token punctuation">:</span> int64</code></pre><h3 id="合并两个文件中的内容"><a href="#合并两个文件中的内容" class="headerlink" title="合并两个文件中的内容"></a>合并两个文件中的内容</h3><pre class=" language-python"><code class="language-python">allData <span class="token operator">=</span> pd<span class="token punctuation">.</span>concat<span class="token punctuation">(</span><span class="token punctuation">[</span>trainData<span class="token punctuation">,</span>testData<span class="token punctuation">]</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>ignore_index<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token operator">//</span>第一个参数为两个文件名,第二第三个参数格式相同</code></pre><h3 id="pd-set-option-设置指定的值"><a href="#pd-set-option-设置指定的值" class="headerlink" title="pd.set_option() 设置指定的值"></a>pd.set_option() 设置指定的值</h3><pre class=" language-python"><code class="language-python">pd<span class="token punctuation">.</span>setoption<span class="token punctuation">(</span><span class="token string">'max_rows'</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span> <span class="token operator">//</span><span class="token operator">//</span><span class="token operator">//</span>设置最大的行数</code></pre><h3 id="value-counts-获取每个值出现的次数"><a href="#value-counts-获取每个值出现的次数" class="headerlink" title="value_counts() 获取每个值出现的次数"></a>value_counts() 获取每个值出现的次数</h3><pre class=" language-python"><code class="language-python">trainData<span class="token punctuation">[</span><span class="token string">'Pclass'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>value_counts<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre><h3 id="fillna-用指定方法填充"><a href="#fillna-用指定方法填充" class="headerlink" title="fillna() 用指定方法填充"></a>fillna() 用指定方法填充</h3><pre class=" language-python"><code class="language-python">age <span class="token operator">=</span> trainData<span class="token punctuation">[</span><span class="token string">'Age'</span><span class="token punctuation">]</span>age <span class="token operator">=</span> age<span class="token punctuation">.</span>fillna<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">//</span><span class="token operator">//</span>将年龄用<span class="token number">0</span>来填充</code></pre><h3 id="客串seaborn中的一个方法-distplot-age-画出age的树状图"><a href="#客串seaborn中的一个方法-distplot-age-画出age的树状图" class="headerlink" title="客串seaborn中的一个方法 distplot(age) 画出age的树状图"></a>客串seaborn中的一个方法 distplot(age) 画出age的树状图</h3><pre class=" language-python"><code class="language-python">sns<span class="token punctuation">.</span>distplot<span class="token punctuation">(</span>age<span class="token punctuation">)</span></code></pre><p><img src="https://img-blog.csdnimg.cn/20190915112502222.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg5NTQ4Mg==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><h3 id="describe-简要显示数据的数字特征"><a href="#describe-简要显示数据的数字特征" class="headerlink" title="describe() 简要显示数据的数字特征"></a>describe() 简要显示数据的数字特征</h3><pre class=" language-python"><code class="language-python">allData<span class="token punctuation">[</span><span class="token string">'Fare'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>describe<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre><p><img src="https://img-blog.csdnimg.cn/20190915113352696.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg5NTQ4Mg==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><pre><code>### ~corr() 计算相关系数</code></pre><ul><li>method:可选{‘pearson’, ‘kendall’, ‘spearman’}<ul><li>pearson: standard correlation coefficient</li><li>kendall: Kendall Tau correlation coefficient</li><li>spearman: Spearman rank correlation</li></ul></li><li>min_periods: Minimum number of observations required per pair of columns to have a valid result. Currently only available for pearson and spearman correlation </li></ul><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># 计算标准相关系数</span>corr_matrix <span class="token operator">=</span> housing<span class="token punctuation">.</span>corr<span class="token punctuation">(</span><span class="token punctuation">)</span>corr_matrix<span class="token punctuation">[</span><span class="token string">"median_house_value"</span><span class="token punctuation">]</span><span class="token punctuation">.</span>sort_values<span class="token punctuation">(</span>ascending<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">#输出:</span><span class="token comment" spellcheck="true"># median_house_value 1.000000</span><span class="token comment" spellcheck="true"># median_income 0.687160</span><span class="token comment" spellcheck="true"># total_rooms 0.135097</span><span class="token comment" spellcheck="true"># housing_median_age 0.114110</span><span class="token comment" spellcheck="true"># households 0.064506</span><span class="token comment" spellcheck="true"># total_bedrooms 0.047689</span><span class="token comment" spellcheck="true"># population -0.026920</span><span class="token comment" spellcheck="true"># longitude -0.047432</span><span class="token comment" spellcheck="true"># latitude -0.142724</span><span class="token comment" spellcheck="true"># Name: median_house_value, dtype: float64</span></code></pre>]]></content>
<tags>
<tag> python </tag>
<tag> Machine Learning </tag>
</tags>
</entry>
<entry>
<title>python基础</title>
<link href="/2020/05/24/python-ji-chu/"/>
<url>/2020/05/24/python-ji-chu/</url>
<content type="html"><![CDATA[<h1 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h1><p>[TOC]</p><h2 id="列表是什么?"><a href="#列表是什么?" class="headerlink" title="列表是什么?"></a>列表是什么?</h2><p><strong>列表由一系列特定顺序排列的元素组成的。在python中用 [] 来表示列表,并用 , 来分隔其中的元素。</strong></p><pre class=" language-python"><code class="language-python">bicycles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'trek'</span><span class="token punctuation">,</span> <span class="token string">'cannondale'</span><span class="token punctuation">,</span> <span class="token string">'specialized'</span><span class="token punctuation">]</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span></code></pre><h3 id="访问列表元素"><a href="#访问列表元素" class="headerlink" title="访问列表元素"></a>访问列表元素</h3><p>访问bicycles中的第一个元素</p><p><code>print(bicycles[0])</code></p><p>可以使用title()方法让元素的格式更整洁</p><p><code>print(bicycles[0].title())</code></p><p><strong>注意:在python中,第一个元素的索引为0,而不是1</strong></p><h3 id="使用列表中的各个值"><a href="#使用列表中的各个值" class="headerlink" title="使用列表中的各个值"></a>使用列表中的各个值</h3><p>可以像其他变量一样使用列表中的各个值。例如:</p><pre class=" language-python"><code class="language-python">message <span class="token operator">=</span> <span class="token string">"My first bicycle was a "</span> <span class="token operator">+</span> bicycles<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"."</span><span class="token keyword">print</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token comment" spellcheck="true">### My first bicycle was a Trek.</span></code></pre><h3 id="修改、添加和删除元素"><a href="#修改、添加和删除元素" class="headerlink" title="修改、添加和删除元素"></a>修改、添加和删除元素</h3><p>在python中创建的大多数列表都是动态的,这意味着可以随意地对列表进行修改、删除和添加。</p><p>例如:</p><ol><li><p>修改元素的值</p><pre class=" language-python"><code class="language-python"> bicycles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'trek'</span><span class="token punctuation">,</span> <span class="token string">'cannondale'</span><span class="token punctuation">,</span> <span class="token string">'specialized'</span><span class="token punctuation">]</span> <span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span> bicycles<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'trak'</span> <span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">### ['trek', 'cannondale', 'specialized']</span> <span class="token comment" spellcheck="true">### ['trak', 'cannondale', 'specialized']</span></code></pre></li><li><p>添加元素</p><ol><li><p>在列表的末尾添加元素</p><p><strong>使用append()方法</strong></p><pre class=" language-python"><code class="language-python">bicycles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'trek'</span><span class="token punctuation">,</span> <span class="token string">'cannondale'</span><span class="token punctuation">,</span> <span class="token string">'specialized'</span><span class="token punctuation">]</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span>bicycles<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">'ducati'</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span><span class="token comment" spellcheck="true">### ['trek', 'cannondale', 'specialized']</span><span class="token comment" spellcheck="true">### ['trek', 'cannondale', 'specialized', 'ducati']</span></code></pre></li><li><p>在列表中插入元素</p><p><strong>使用insert()方法</strong></p><pre class=" language-python"><code class="language-python">bicycles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'trek'</span><span class="token punctuation">,</span> <span class="token string">'cannondale'</span><span class="token punctuation">,</span> <span class="token string">'specialized'</span><span class="token punctuation">]</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span>bicycles<span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">'ducati'</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span></code></pre></li></ol></li></ol><pre><code> ### ['trek', 'cannondale', 'specialized'] ### ['ducati', 'trek', 'cannondale', 'specialized'] ```</code></pre><ol start="3"><li><p>删除元素</p><ol><li><p>如果要知道删除元素的位置,可以使用del语句删除元素</p><pre class=" language-python"><code class="language-python">bicycles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'trek'</span><span class="token punctuation">,</span> <span class="token string">'cannondale'</span><span class="token punctuation">,</span> <span class="token string">'specialized'</span><span class="token punctuation">]</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span><span class="token keyword">del</span> bicycles<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span><span class="token comment" spellcheck="true">### ['trek', 'cannondale', 'specialized']</span><span class="token comment" spellcheck="true">### ['cannondale', 'specialized']</span></code></pre><ol start="2"><li><p>使用pop方法删除元素</p><p>有时候,你需要将元素从列表中删除,并接着使用它的值。</p><pre class=" language-python"><code class="language-python">bicycles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'trek'</span><span class="token punctuation">,</span> <span class="token string">'cannondale'</span><span class="token punctuation">,</span> <span class="token string">'specialized'</span><span class="token punctuation">]</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span>popped_bicycles <span class="token operator">=</span> bicycles<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>popped_bicycles<span class="token punctuation">)</span><span class="token comment" spellcheck="true">### ['trek', 'cannondale', 'specialized']</span><span class="token comment" spellcheck="true">### specialized</span></code></pre><p>实际上,可以使用pop()来删除列表中的任何位置的元素,只需要在括号中指定要删除的元素的索引即可。</p></li></ol></li></ol></li></ol><h1 id="组织列表"><a href="#组织列表" class="headerlink" title="组织列表"></a>组织列表</h1><h2 id="使用sort-方法对列表进行排序"><a href="#使用sort-方法对列表进行排序" class="headerlink" title="使用sort()方法对列表进行排序"></a>使用sort()方法对列表进行排序</h2><p>在创建的列表中,元素的排列顺序通常是无法预测的,所以可以用sort()方法进行排序。</p><pre class=" language-python"><code class="language-python">bicycles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'trek'</span><span class="token punctuation">,</span> <span class="token string">'cannondale'</span><span class="token punctuation">,</span> <span class="token string">'specialized'</span><span class="token punctuation">]</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span>bicycles<span class="token punctuation">.</span>sort<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span><span class="token comment" spellcheck="true">### ['trek', 'cannondale', 'specialized']</span><span class="token comment" spellcheck="true">### ['cannondale', 'specialized', 'trek']</span></code></pre><h2 id="使用sorted-进行临时排序"><a href="#使用sorted-进行临时排序" class="headerlink" title="使用sorted()进行临时排序"></a>使用sorted()进行临时排序</h2><p>要保留元素原来的排列顺序,同时以特定的顺序将他们呈现出来,可以使用函数sorted()。</p><pre class=" language-python"><code class="language-python">bicycles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'trek'</span><span class="token punctuation">,</span> <span class="token string">'cannondale'</span><span class="token punctuation">,</span> <span class="token string">'specialized'</span><span class="token punctuation">]</span><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Here is the original list:"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"\nHere is the sorted list:"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>sorted<span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"\nHere is the original list again:"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>bicycles<span class="token punctuation">)</span><span class="token comment" spellcheck="true">##########</span><span class="token comment" spellcheck="true">## Here is the original list:</span><span class="token comment" spellcheck="true">## ['trek', 'cannondale', 'specialized']</span><span class="token comment" spellcheck="true">## Here is the sorted list:</span><span class="token comment" spellcheck="true">## ['cannondale', 'specialized', 'trek']</span><span class="token comment" spellcheck="true">## Here is the original list again:</span><span class="token comment" spellcheck="true">## ['trek', 'cannondale', 'specialized']</span></code></pre><h1 id="遍历整个列表"><a href="#遍历整个列表" class="headerlink" title="遍历整个列表"></a>遍历整个列表</h1><pre class=" language-python"><code class="language-python">bicycles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'trek'</span><span class="token punctuation">,</span> <span class="token string">'cannondale'</span><span class="token punctuation">,</span> <span class="token string">'specialized'</span><span class="token punctuation">]</span><span class="token keyword">for</span> bicycle <span class="token keyword">in</span> bicycles<span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>bicycle<span class="token punctuation">)</span><span class="token comment" spellcheck="true">## trek</span><span class="token comment" spellcheck="true">## cannondale</span><span class="token comment" spellcheck="true">## specialized</span></code></pre><p>上面这段代码,我们定义了一个for循环,这行代码让Python从列表bicycles中取出一个名字,并将其存储在bicycle中。然后将其打印出来。输出其中的所有的名字。</p><h1 id="创建数值列表"><a href="#创建数值列表" class="headerlink" title="创建数值列表"></a>创建数值列表</h1><h2 id="使用函数range"><a href="#使用函数range" class="headerlink" title="使用函数range()"></a>使用函数range()</h2><pre class=" language-python"><code class="language-python"><span class="token keyword">for</span> value <span class="token keyword">in</span> range<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 1</span><span class="token comment" spellcheck="true">## 2</span><span class="token comment" spellcheck="true">## 3</span><span class="token comment" spellcheck="true">## 4</span></code></pre><p>这个示例打印了1—4,这里不包含5.</p><pre class=" language-python"><code class="language-python">numbers <span class="token operator">=</span> list<span class="token punctuation">(</span>range<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>numbers<span class="token punctuation">)</span><span class="token comment" spellcheck="true">## [2, 4, 6, 8, 10]</span></code></pre><p>这个示例表示创建列表,列表从2开始,然后不断加2,直到达到或超过最终值,因此最后一个输出结果为10.</p><h1 id="切片"><a href="#切片" class="headerlink" title="切片"></a>切片</h1><p>要创建切片,可以指定要使用的第一个元素和最后一个元素的索引。例如:要输出列表的前三个元素,可指定索引0:3.</p><pre class=" language-python"><code class="language-python">bicycles <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'trek'</span><span class="token punctuation">,</span> <span class="token string">'cannondale'</span><span class="token punctuation">,</span> <span class="token string">'specialized'</span><span class="token punctuation">,</span> <span class="token string">'qin'</span><span class="token punctuation">]</span><span class="token keyword">for</span> bicycle <span class="token keyword">in</span> bicycles<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>bicycle<span class="token punctuation">)</span><span class="token comment" spellcheck="true">## trek</span><span class="token comment" spellcheck="true">## cannondale</span><span class="token comment" spellcheck="true">## specialized</span></code></pre><p>上面的例子中使用for循环遍历了bicycles列表中的前三个元素,切片可以指定列表中的任意位置。</p><h1 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h1><p>列表非常适用于存储在程序运行期间可能变化的数据集。列表是可以修改的,这对处理网站的用户列表至关重要。然而,有时候需要创建一系列不可修改的元素,元组可以实现这种操作。</p><p><strong>元组的定义方法是圆括号 ()</strong></p><pre class=" language-python"><code class="language-python">dimensions <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>dimensions<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>dimensions<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 200</span><span class="token comment" spellcheck="true">## 50</span></code></pre><p>元组的值不可修改</p><p><code>dimensions[0] = 250</code></p><p>这句话会报错。</p><pre class=" language-python"><code class="language-python">Traceback <span class="token punctuation">(</span>most recent call last<span class="token punctuation">)</span><span class="token punctuation">:</span> File <span class="token string">"D:/Program Files (x86)/PychramProjects/python/python_space.py"</span><span class="token punctuation">,</span> line <span class="token number">48</span><span class="token punctuation">,</span> <span class="token keyword">in</span> <span class="token operator"><</span>module<span class="token operator">></span> dimensions<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">250</span>TypeError<span class="token punctuation">:</span> <span class="token string">'tuple'</span> object does <span class="token operator">not</span> support item assignment</code></pre><p><strong>虽然不能修改元组的元素的值,但是可以给存储的元组的变量赋值。因此,如果要修改前诉矩阵的尺寸,可以重新定义整个元组</strong></p><pre class=" language-python"><code class="language-python">dimensions <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>dimensions<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>dimensions<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>dimensions <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>dimensions<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>dimensions<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## 200</span><span class="token comment" spellcheck="true">## 50</span><span class="token comment" spellcheck="true">## 400</span><span class="token comment" spellcheck="true">## 200</span></code></pre><h1 id="字典(与-Json类似)"><a href="#字典(与-Json类似)" class="headerlink" title="字典(与 Json类似)"></a>字典(与 Json类似)</h1><p>在python中,字典是一系列键—值对。每个键都与一个值相对应,可以使用键来访问值。和C++中的map很相似。下面是一个简单的字典。</p><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># 字典的练习</span>alien_0 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'color'</span><span class="token punctuation">:</span> <span class="token string">'green'</span><span class="token punctuation">,</span> <span class="token string">'points'</span><span class="token punctuation">:</span> <span class="token string">'5'</span><span class="token punctuation">}</span><span class="token keyword">print</span><span class="token punctuation">(</span>alien_0<span class="token punctuation">[</span><span class="token string">'color'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>alien_0<span class="token punctuation">[</span><span class="token string">'points'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">## green</span><span class="token comment" spellcheck="true">## 5</span></code></pre><p>同时,也可以在已经建好的字典里面添加键值对。例如:</p><pre class=" language-python"><code class="language-python">alien_0 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'color'</span><span class="token punctuation">:</span> <span class="token string">'green'</span><span class="token punctuation">,</span> <span class="token string">'points'</span><span class="token punctuation">:</span> <span class="token string">'5'</span><span class="token punctuation">}</span><span class="token keyword">print</span><span class="token punctuation">(</span>alien_0<span class="token punctuation">)</span>alien_0<span class="token punctuation">[</span><span class="token string">'x_position'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span>alien_0<span class="token punctuation">[</span><span class="token string">'y_position'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">25</span><span class="token keyword">print</span><span class="token punctuation">(</span>alien_0<span class="token punctuation">)</span><span class="token comment" spellcheck="true">## {'color': 'green', 'points': '5'}</span><span class="token comment" spellcheck="true">## {'color': 'green', 'points': '5', 'x_position': 0, 'y_position': 25}</span></code></pre><p><strong>字典中的值是可以修改的。</strong></p><h3 id="使用-del-函数删除键值对"><a href="#使用-del-函数删除键值对" class="headerlink" title="使用 del 函数删除键值对"></a>使用 del 函数删除键值对</h3><pre class=" language-python"><code class="language-python"><span class="token keyword">del</span> alien_0<span class="token punctuation">[</span><span class="token string">'points'</span><span class="token punctuation">]</span></code></pre>]]></content>
<tags>
<tag> python </tag>
</tags>
</entry>
<entry>
<title>Hello World</title>
<link href="/2020/05/24/hello-world/"/>
<url>/2020/05/24/hello-world/</url>
<content type="html"><![CDATA[<p>Welcome to <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="noopener">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" target="_blank" rel="noopener">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="noopener">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="gutter"><pre><span class="line">1</span><br></pre></td><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" target="_blank" rel="noopener">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="gutter"><pre><span class="line">1</span><br></pre></td><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" target="_blank" rel="noopener">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="gutter"><pre><span class="line">1</span><br></pre></td><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" target="_blank" rel="noopener">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="gutter"><pre><span class="line">1</span><br></pre></td><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" target="_blank" rel="noopener">Deployment</a></p>]]></content>
</entry>
</search>