-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.html
More file actions
406 lines (277 loc) · 13.8 KB
/
index.html
File metadata and controls
406 lines (277 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>黄哥Python培训</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta property="og:type" content="website">
<meta property="og:title" content="黄哥Python培训">
<meta property="og:url" content="http://pythonpeixun.github.io/index.html">
<meta property="og:site_name" content="黄哥Python培训">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="黄哥Python培训">
<link rel="alternate" href="/atom.xml" title="黄哥Python培训" type="application/atom+xml">
<link rel="icon" href="/favicon.png">
<link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div id="container">
<div id="wrap">
<header id="header">
<div id="banner"></div>
<div id="header-outer" class="outer">
<div id="header-title" class="inner">
<h1 id="logo-wrap">
<a href="/" id="logo">黄哥Python培训</a>
</h1>
</div>
<div id="header-inner" class="inner">
<nav id="main-nav">
<a id="main-nav-toggle" class="nav-icon"></a>
<a class="main-nav-link" href="/">主页</a>
<a class="main-nav-link" href="/archives">归档</a>
</nav>
<nav id="sub-nav">
<a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
<a id="nav-search-btn" class="nav-icon" title="Search"></a>
</nav>
<div id="search-form-wrap">
<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit"></button><input type="hidden" name="sitesearch" value="http://pythonpeixun.github.io"></form>
</div>
</div>
</div>
</header>
<div class="outer">
<section id="main">
<article id="python-实例对象加法和字符串表示" class="article article-type-python" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/08/23/实例对象加法和字符串表示/" class="article-date">
<time datetime="2016-08-23T00:22:03.000Z" itemprop="datePublished">2016-08-23</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/08/23/实例对象加法和字符串表示/">实例对象加法和字符串表示</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p><a href="https://www.zhihu.com/question/49891849/answer/118302072" target="_blank" rel="external">有人提问</a><br>黄哥修改的代码</p>
<h2 id="Python-类有特殊的方法-add-str-等。"><a href="#Python-类有特殊的方法-add-str-等。" class="headerlink" title="Python 类有特殊的方法__add__, __str__ 等。"></a>Python 类有特殊的方法__add__, __str__ 等。</h2><pre><code>object.__add__(self, other)
object.__sub__(self, other)
object.__mul__(self, other)
object.__floordiv__(self, other)
object.__mod__(self, other)
object.__divmod__(self, other)
object.__pow__(self, other[, modulo])
object.__lshift__(self, other)
object.__rshift__(self, other)
object.__and__(self, other)
object.__xor__(self, other)
object.__or__(self, other)
</code></pre><p>上面的方法被调用时,实现了相应的二元算术运算符。(+, -, <em>, //, %, divmod(), pow(), *</em>, <<, >>, &, ^, |)<br>实例说明:<br>表达式 x + y ,如果x的类有一个__add__方法,相当于x.__add__(y) 被调用。</p>
<h2 id="类定义了-str-方法,当调用str-内置函数或Python-2的print-语句时,"><a href="#类定义了-str-方法,当调用str-内置函数或Python-2的print-语句时," class="headerlink" title="类定义了__str__ 方法,当调用str()内置函数或Python 2的print 语句时,"></a>类定义了__str__ 方法,当调用str()内置函数或Python 2的print 语句时,</h2><p>这个方法就会被调用。</p>
<pre><code># coding:utf-8
class Point(object):
"""黄哥修改"""
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
if isinstance(other, tuple):
self.x += other[0]
self.y += other[1]
return self
elif isinstance(other, Point):
self.x += other.x
self.y += other.y
return self
def __radd__(self, other):
return self.__add__(other)
def __str__(self):
return "Point({0}, {1})".format(self.x, self.y)
__repr__ = __str__
p1 = Point(1, 2)
p2 = Point(3, 4)
p3 = Point(2, 2)
print p1 + p2 + p3
print sum([p1 + p2 + (3, 5)], p3)
print str(p3)
</code></pre><p><a href="https://github.com/pythonpeixun/article/blob/master/shanghai_weekend.md" target="_blank" rel="external">Python上海周末培训班</a></p>
<p><a href="https://pythonpeixun.github.io/2016/08/11/216%E5%B0%8F%E6%97%B6%E5%AD%A6%E4%BC%9APython/">216小时学会Python</a></p>
</div>
<footer class="article-footer">
<a data-url="http://pythonpeixun.github.io/2016/08/23/实例对象加法和字符串表示/" data-id="cis6rozy70002bxs6gezx7cba" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-Python列表元素分组" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/08/18/Python列表元素分组/" class="article-date">
<time datetime="2016-08-18T03:58:59.000Z" itemprop="datePublished">2016-08-18</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/08/18/Python列表元素分组/">Python列表元素分组</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p><a href="https://www.zhihu.com/question/49735791" target="_blank" rel="external">知乎上有人问</a><br>Python中的列表按相同元素分割?<br>比如列表[0,0,0,1,1,2,3,3,3,2,3,3,0,0]分割成[0,0,0],[1,1],[2],[3,3,3],[2],[3,3],[0,0]</p>
<p>如何解决这样的问题呢?<br>1、首先说明,如果这样的题目都不能写出代码,原因在基本的循环判断没有搞清楚。<br>2、黄哥在<a href="https://github.com/pythonpeixun/article/blob/master/python/how_to_learn_program2.md" target="_blank" rel="external">如何训练自己的编程思路</a> 文章所说的,做习题,要像开发项目的流程一样<br>(需求->需求分析->设计->编码->测试->交付等),<br>拿到一个习题,还没有进行分析,就匆匆忙忙敲代码,<br>这个学习方法,是不好的学习方法。</p>
<p>3、如果做这个习题,看黄哥的讲解,先要分析,分析看出是按照相邻元素是不是相同来分组。<br>设计解决方法,大问题化解为小问题,先化解为找出元素不相同的那个元素的索引,有这个索引后,再进行分组。</p>
<p>4、编码</p>
<p>代码一:</p>
<pre><code># coding:utf-8
def group_by_element(lst):
'''by 黄哥 基本思路是先取得不同元素起始的索引值,
再按照这个索引值取切片
'''
index = []
result = []
for i, _ in enumerate(lst):
if i < len(lst) - 1 and lst[i + 1] != lst[i]:
index.append(i + 1)
result.append(lst[:index[0]])
for i, item in enumerate(index):
if i < len(index) - 1:
result.append(lst[index[i]:index[i + 1]])
result.append(lst[item:])
return result
if __name__ == '__main__':
lst = [0, 0, 0, 1, 1, 2, 3, 3, 3, 2, 3, 3, 0, 0]
group = group_by_element(lst)
print group
</code></pre><p>代码二:</p>
<pre><code># coding:utf-8
def group_by_element(lst):
'''by 黄哥 基本思路是先取得不同元素起始的索引值,
再按照这个索引值,用生成器分组。
'''
index = []
for i, _ in enumerate(lst):
if i < len(lst) - 1 and lst[i + 1] != lst[i]:
index.append(i + 1)
def take(lst, n):
for i in range(n):
yield next(lst)
if not hasattr(lst, 'next'):
lst = iter(lst)
begin = 0
for item in index:
x = list(take(lst, item - begin ))
begin = item
yield x
yield list(lst)
if __name__ == '__main__':
lst = [0, 0, 0, 1, 1, 2, 3, 3, 3, 2, 3, 3, 0, 0]
group = group_by_element(lst)
print list(group)
</code></pre><p>思路三:<br>基本思路用一个二维list [[]], 遍历list,判断前后元素是不是相等,如果相等添加到result[-1],<br>如果不相等,需要添加一个空[]。</p>
<pre><code># coding:utf-8
def group_by_element(lst):
'''by 黄哥 基本思路用一个二维list [[]], 遍历list
判断前后元素是不是相等,如果相等添加到result[-1],如果
不相等,需要添加一个空[]
'''
result = [[]]
length = len(lst)
for i in range(length):
if i < length - 1:
if lst[i] == lst[i + 1]:
result[-1].append(lst[i])
else:
result[-1].append(lst[i])
result.append([])
result[-1].append(lst[i])
return result
if __name__ == '__main__':
lst = [0, 0, 0, 1, 1, 2, 3, 3, 3, 2, 3, 3, 0, 0]
group = group_by_element(lst)
print group
</code></pre><p><a href="https://github.com/pythonpeixun/article/blob/master/python/hours_216.md" target="_blank" rel="external">216小时学会Python</a> </p>
<p><a href="https://github.com/pythonpeixun/article/blob/master/python_shiping.md" target="_blank" rel="external">点击黄哥python培训试看视频播放地址</a></p>
</div>
<footer class="article-footer">
<a data-url="http://pythonpeixun.github.io/2016/08/18/Python列表元素分组/" data-id="cis6rozy40001bxs6rxuuz333" class="article-share-link">Share</a>
</footer>
</div>
</article>
<article id="post-216小时学会Python" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/08/11/216小时学会Python/" class="article-date">
<time datetime="2016-08-11T03:13:25.000Z" itemprop="datePublished">2016-08-11</time>
</a>
</div>
<div class="article-inner">
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/08/11/216小时学会Python/">216小时学会Python</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>216小时是怎么来的,学习三个月,周一到周五学习2小时,周六、周日每天学习4小时。</p>
<p>学习途径:跟着黄哥的视频学习,写作业,答疑。</p>
<p>目标:三个月可以到达自己写代码解决问题的能力。</p>
<pre><code>第一目标是:通过学习python学会编程 。
第二目标:会写爬虫,网页采集。
第三目标:学会web开发。
</code></pre><p><a href="https://github.com/pythonpeixun/article/blob/master/index.md" target="_blank" rel="external">黄哥python远程视频培训</a></p>
<p>咨询qq:1465376564</p>
<p><a href="https://github.com/pythonpeixun/article/blob/master/python/how_to_learn_program2.md" target="_blank" rel="external">如何训练自己的编程思路</a></p>
<p><a href="https://github.com/pythonpeixun/article/blob/master/python_shiping.md" target="_blank" rel="external">部分免费Python免费视频</a></p>
<p><a href="https://github.com/pythonpeixun/article/blob/master/python/thanks.md" target="_blank" rel="external">感恩!感谢黄哥Python培训学员的支持和肯定。</a></p>
</div>
<footer class="article-footer">
<a data-url="http://pythonpeixun.github.io/2016/08/11/216小时学会Python/" data-id="cis6rozxy0000bxs67rajqevo" class="article-share-link">Share</a>
</footer>
</div>
</article>
</section>
<aside id="sidebar">
<div class="widget-wrap">
<h3 class="widget-title">Archives</h3>
<div class="widget">
<ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">August 2016</a></li></ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">Recent Posts</h3>
<div class="widget">
<ul>
<li>
<a href="/2016/08/23/实例对象加法和字符串表示/">实例对象加法和字符串表示</a>
</li>
<li>
<a href="/2016/08/18/Python列表元素分组/">Python列表元素分组</a>
</li>
<li>
<a href="/2016/08/11/216小时学会Python/">216小时学会Python</a>
</li>
</ul>
</div>
</div>
</aside>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info" class="inner">
© 2016 黄哥 qq:1465376564<br>
Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
</div>
</div>
</footer>
</div>
<nav id="mobile-nav">
<a href="/" class="mobile-nav-link">主页</a>
<a href="/archives" class="mobile-nav-link">归档</a>
</nav>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
<script src="/fancybox/jquery.fancybox.pack.js"></script>
<script src="/js/script.js"></script>
</div>
</body>
</html>