Skip to content

Commit 0c181ec

Browse files
wklkenwklken
authored andcommitted
第一版本,基本文件加入
1 parent bfba28f commit 0c181ec

17 files changed

+2431
-0
lines changed

README.md

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,183 @@ stackoverflow-py-top-qa
22
=======================
33

44
stackoverflow上Python相关回答整理翻译
5+
6+
查看了下前面(vote前15页,挑了下,vote都是100+的样子,大概200个)的问题,[链接](http://stackoverflow.com/questions/tagged/python?page=1&sort=votes&pagesize=15)
7+
8+
第一页的前几个比较长,目测都有中文翻译版本,大家可以网上搜下
9+
10+
目前进度 50% ,翻译完100个问题,还有100+的样子,后续逐步翻译加入
11+
12+
相对来说都比较简单
13+
14+
总结整理,复习印证(注意,合并了每个问题的多个答案,但是时间仓促,疏漏难免,感兴趣问题直接点链接看原文吧)
15+
16+
如果有兴趣,可以提交你的翻译
17+
18+
19+
20+
### 目录
21+
> 基础
22+
23+
* 控制流
24+
-rw-r--r-- 1 ken staff 5425 Sep 21 11:48 qa-control-flow.md
25+
* 字符串
26+
-rw-r--r-- 1 ken staff 7987 Sep 21 11:06 qa-string.md
27+
* 文件
28+
-rw-r--r-- 1 ken staff 6385 Sep 21 11:09 qa-file.md
29+
* 数学相关
30+
-rw-r--r-- 1 ken staff 1310 Sep 21 11:48 qa-math.md
31+
32+
33+
> 基本数据结构
34+
35+
* 列表
36+
-rw-r--r-- 1 ken staff 8035 Sep 21 11:06 qa-list.md
37+
* 元组
38+
-rw-r--r-- 1 ken staff 0 Sep 15 17:13 qa-tuple.md
39+
* 字典
40+
-rw-r--r-- 1 ken staff 4480 Sep 15 17:36 qa-dict.md
41+
42+
> 进阶
43+
44+
* 函数
45+
-rw-r--r-- 1 ken staff 1849 Sep 21 11:43 qa-func.md
46+
* 内置函数
47+
-rw-r--r-- 1 ken staff 3091 Sep 21 11:48 qa-built-in.md
48+
* 异常
49+
-rw-r--r-- 1 ken staff 3485 Sep 21 11:43 qa-exception.md
50+
* 模块
51+
-rw-r--r-- 1 ken staff 4368 Sep 21 11:48 qa-std-modules.md
52+
-rw-r--r-- 1 ken staff 660 Sep 21 11:13 qa-modules.md
53+
-rw-r--r-- 1 ken staff 1030 Sep 21 11:48 qa-datetime.md
54+
55+
* oop
56+
-rw-r--r-- 1 ken staff 9772 Sep 21 11:13 qa-oop.md
57+
58+
> 其他
59+
60+
* pip/easy_install
61+
-rw-r--r-- 1 ken staff 1729 Sep 15 17:36 qa-pip-easy_install.md
62+
63+
* 其他
64+
-rw-r--r-- 1 ken staff 3156 Sep 21 11:48 qa-others.md
65+
66+
67+
### 已翻译问题链接(去重)
68+
qa-built-in.md:问题 - http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print
69+
qa-built-in.md:重复问题 - http://stackoverflow.com/questions/107705/python-output-buffering
70+
qa-built-in.md:问题 - http://stackoverflow.com/questions/1835018/python-check-if-an-object-is-a-list-or-tuple-but-not-string
71+
qa-built-in.md:问题 - http://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python
72+
qa-built-in.md:问题 - http://stackoverflow.com/questions/402504/how-to-determine-the-variable-type-in-python
73+
qa-built-in.md:另外一个相同的问题 - http://stackoverflow.com/questions/2225038/python-determine-the-type-of-an-object
74+
qa-control-flow.md:问题 - http://stackoverflow.com/questions/73663/terminating-a-python-script
75+
qa-control-flow.md:问题 - http://stackoverflow.com/questions/26595/is-there-any-difference-between-foo-is-none-and-foo-none
76+
qa-control-flow.md:问题 - http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops
77+
qa-control-flow.md:问题 - http://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python
78+
qa-control-flow.md:问题 - http://stackoverflow.com/questions/9284350/why-does-1-in-1-0-true-evaluate-to-false
79+
qa-control-flow.md:问题 - http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python
80+
qa-control-flow.md:问题 - http://stackoverflow.com/questions/2710940/python-if-x-is-not-none-or-if-not-x-is-none
81+
qa-control-flow.md:问题 - http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them
82+
qa-datetime.md:问题 - http://stackoverflow.com/questions/1697815/how-do-you-convert-a-python-time-struct-time-object-into-a-datetime-object
83+
qa-datetime.md:问题 - http://stackoverflow.com/questions/415511/how-to-get-current-time-in-python
84+
qa-dict.md:问题 - http://stackoverflow.com/questions/1747817/python-create-a-dictionary-with-list-comprehension
85+
qa-dict.md:问题 - http://stackoverflow.com/questions/1323410/has-key-or-in
86+
qa-dict.md:问题 - http://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary
87+
qa-dict.md:问题 - http://stackoverflow.com/questions/1024847/add-to-a-dictionary-in-python
88+
qa-dict.md:问题 - http://stackoverflow.com/questions/1305532/convert-python-dict-to-object
89+
qa-dict.md:问题 - http://stackoverflow.com/questions/38987/how-can-i-merge-union-two-python-dictionaries-in-a-single-expression
90+
qa-dict.md:问题 - http://stackoverflow.com/questions/209840/map-two-lists-into-a-dictionary-in-python
91+
qa-dict.md:问题 - http://stackoverflow.com/questions/72899/in-python-how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary
92+
qa-exception.md:问题 - http://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python
93+
qa-exception.md:问题 - http://stackoverflow.com/questions/2052390/how-do-i-manually-throw-raise-an-exception-in-python
94+
qa-exception.md:问题 - http://stackoverflow.com/questions/6470428/catch-multiple-exceptions-in-one-line-except-block
95+
qa-exception.md:问题 - http://stackoverflow.com/questions/944592/best-practice-for-python-assert
96+
qa-file.md:问题 - http://stackoverflow.com/questions/273192/python-best-way-to-create-directory-if-it-doesnt-exist-for-file-write
97+
qa-file.md:问题 - http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python
98+
qa-file.md:问题 - http://stackoverflow.com/questions/275018/how-can-i-remove-chomp-a-newline-in-python
99+
qa-file.md:类似问题 - http://stackoverflow.com/questions/761804/trimming-a-string-in-python
100+
qa-file.md:问题 - http://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python
101+
qa-file.md:问题 - http://stackoverflow.com/questions/466345/converting-string-into-datetime
102+
qa-file.md:问题 - http://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory
103+
qa-file.md:问题 - http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-with-python
104+
qa-file.md:问题 - http://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array
105+
qa-file.md:问题 - http://stackoverflow.com/questions/4706499/how-do-you-append-to-file-in-python
106+
qa-file.md:问题 - http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python
107+
qa-file.md:问题 - http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python
108+
qa-file.md:重复问题 - http://stackoverflow.com/questions/120656/directory-listing-in-python
109+
qa-file.md:问题 - http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python
110+
qa-func.md:问题 - http://stackoverflow.com/questions/251464/how-to-get-the-function-name-as-string-in-python
111+
qa-func.md:问题 - http://stackoverflow.com/questions/3061/calling-a-function-from-a-string-with-the-functions-name-in-python
112+
qa-func.md:问题 - http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters
113+
qa-list.md:问题 - http://stackoverflow.com/questions/509211/the-python-slice-notation
114+
qa-list.md:问题 - http://stackoverflow.com/questions/53513/python-what-is-the-best-way-to-check-if-a-list-is-empty
115+
qa-list.md:问题 - http://stackoverflow.com/questions/1720421/merge-two-lists-in-python
116+
qa-list.md:问题 - http://stackoverflow.com/questions/518021/getting-the-length-of-an-array-in-python
117+
qa-list.md:问题 - http://stackoverflow.com/questions/2612802/how-to-clone-a-list-in-python
118+
qa-list.md:问题 - http://stackoverflow.com/questions/252703/python-append-vs-extend
119+
qa-list.md:问题 - http://stackoverflow.com/questions/306400/how-do-i-randomly-select-an-item-from-a-list-using-python
120+
qa-list.md:问题 - http://stackoverflow.com/questions/627435/how-to-remove-an-element-from-a-list-by-index-in-python
121+
qa-list.md:问题 - http://stackoverflow.com/questions/930397/how-to-get-the-last-element-of-a-list
122+
qa-list.md:问题 - http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python
123+
qa-list.md:问题 - http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order
124+
qa-list.md:问题 - http://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating-in-python
125+
qa-list.md:问题 - http://stackoverflow.com/questions/176918/in-python-how-do-i-find-the-index-of-an-item-given-a-list-containing-it
126+
qa-list.md:问题 - http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python
127+
qa-math.md:问题 - http://stackoverflow.com/questions/1476/how-do-you-express-binary-literals-in-python
128+
qa-math.md:问题 - http://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python
129+
qa-math.md:问题 - http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python
130+
qa-modules.md:问题 - http://stackoverflow.com/questions/448271/what-is-init-py-for
131+
qa-modules.md:问题 - http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path
132+
qa-oop.md:问题 - http://stackoverflow.com/questions/2709821/python-self-explained
133+
qa-oop.md:问题 - http://stackoverflow.com/questions/70528/why-are-pythons-private-methods-not-actually-private
134+
qa-oop.md:问题 - http://stackoverflow.com/questions/38238/what-are-class-methods-in-python-for
135+
qa-oop.md:问题 - http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init
136+
qa-oop.md:来自 - http://mail.python.org/pipermail/tutor/2008-April/061426.html
137+
qa-oop.md:问题 - http://stackoverflow.com/questions/510972/getting-the-class-name-of-an-instance-in-python
138+
qa-oop.md:问题 - http://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python
139+
qa-oop.md:问题 - http://stackoverflow.com/questions/735975/static-methods-in-python
140+
qa-oop.md:问题 - http://stackoverflow.com/questions/68645/static-class-variables-in-python
141+
qa-oop.md:问题 - http://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python
142+
qa-oop.md:问题 - http://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons-in-python
143+
qa-others.md:问题 - http://stackoverflow.com/questions/170921/should-i-learn-python-2-before-3-or-start-directly-from-python-3
144+
qa-others.md:问题 - http://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv
145+
qa-others.md:问题 - http://stackoverflow.com/questions/990754/how-to-leave-a-python-virtualenv
146+
qa-others.md:问题 - http://stackoverflow.com/questions/193161/what-is-the-best-project-structure-for-a-python-application
147+
qa-others.md:问题 - http://stackoverflow.com/questions/13311094/counter-in-collections-module-python
148+
qa-pip-easy_install.md:问题 - http://stackoverflow.com/questions/2720014/upgrading-all-packages-with-pip
149+
qa-pip-easy_install.md:问题 - http://stackoverflow.com/questions/1231688/how-do-i-remove-packages-installed-with-pythons-easy-install
150+
qa-pip-easy_install.md:问题 - http://stackoverflow.com/questions/122327/how-do-i-find-the-location-of-my-python-site-packages-directory
151+
qa-pip-easy_install.md:问题 - http://stackoverflow.com/questions/1550226/python-setup-py-uninstall
152+
qa-pip-easy_install.md:问题 - http://stackoverflow.com/questions/739993/get-a-list-of-installed-python-modules
153+
qa-std-modules.md:问题 - http://stackoverflow.com/questions/712791/json-and-simplejson-module-differences-in-python
154+
qa-std-modules.md:问题 - http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python
155+
qa-std-modules.md:问题 - http://stackoverflow.com/questions/4480075/argparse-optional-positional-arguments
156+
qa-std-modules.md:问题 - http://stackoverflow.com/questions/842059/is-there-a-portable-way-to-get-the-current-username-in-python
157+
qa-std-modules.md:问题 - http://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python
158+
qa-string.md:问题 - http://stackoverflow.com/questions/493819/python-join-why-is-it-string-joinlist-instead-of-list-joinstring
159+
qa-string.md:问题 - http://stackoverflow.com/questions/6797984/how-to-convert-string-to-lowercase-in-python
160+
qa-string.md:问题 - http://stackoverflow.com/questions/931092/reverse-a-string-in-python
161+
qa-string.md:问题 - http://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-method
162+
qa-string.md:问题 - http://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-in-python
163+
qa-string.md:问题 - http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format
164+
qa-string.md:问题 - http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary
165+
qa-string.md:问题 - http://stackoverflow.com/questions/227459/ascii-value-of-a-character-in-python
166+
qa-string.md:问题 - http://stackoverflow.com/questions/1059559/python-strings-split-with-multiple-separators
167+
qa-string.md:问题 - http://stackoverflow.com/questions/1185524/how-to-trim-whitespace-including-tabs
168+
qa-string.md:问题 - http://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python
169+
qa-string.md:问题 - http://stackoverflow.com/questions/1504717/python-vs-is-comparing-strings-is-fails-sometimes-why
170+
qa-string.md:问题 - http://stackoverflow.com/questions/339007/python-nicest-way-to-pad-zeroes-to-string
171+
172+
173+
------
174+
175+
176+
To Be Continue ...
177+
178+
wklken
179+
180+
181+
182+
Blog: http://wklken.me
183+
184+
2013-09-21 于深圳

contents/qa-built-in.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
2+
3+
### 如何flush Python的print输出
4+
5+
问题 [链接](http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print)
6+
重复问题 [链接](http://stackoverflow.com/questions/107705/python-output-buffering)
7+
8+
默认print输出到sys.stdout
9+
10+
import sys
11+
sys.stdout.flush()
12+
13+
参考
14+
[http://docs.python.org/reference/simple_stmts.html#the-print-statement](http://docs.python.org/2/reference/simple_stmts.html#the-print-statement)
15+
[http://docs.python.org/library/sys.html](http://docs.python.org/2/library/sys.html)
16+
[http://docs.python.org/library/stdtypes.html#file-objects](http://docs.python.org/2/library/stdtypes.html#file-objects)
17+
18+
### Python如何检查一个对象是list或者tuple,但是不是一个字符串
19+
20+
问题 [链接](http://stackoverflow.com/questions/1835018/python-check-if-an-object-is-a-list-or-tuple-but-not-string)
21+
22+
原来的做法是
23+
24+
assert isinstance(lst, (list, tuple))
25+
26+
有没有更好的做法
27+
28+
我认为下面的方式是你需要的
29+
30+
assert not isinstance(lst, basestring)
31+
32+
原来的方式,你可能会漏过很多像列表,但并非list/tuple的
33+
34+
### Python中检查类型的权威方法
35+
36+
问题 [链接](http://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python)
37+
38+
检查一个对象是否是给定类型或者对象是否继承于给定类型?
39+
40+
比如给定一个对象o,如何判断是不是一个str
41+
42+
检查是否是str
43+
44+
type(o) is str
45+
46+
检查是否是str或者str的子类
47+
48+
isinstance(o, str)
49+
50+
下面的方法在某些情况下有用
51+
52+
issubclass(type(o), str)
53+
type(o) in ([str] + str.__subclasses__())
54+
55+
注意,你或许想要的是
56+
57+
isinstance(o, basestring)
58+
59+
因为unicode字符串可以满足判定(unicode 不是str的子类,但是str和unicode都是basestring的子类)
60+
61+
可选的,isinstance可以接收多个类型参数,只要满足其中一个即True
62+
63+
isinstance(o, (str, unicode))
64+
65+
### 如何判断一个变量的类型
66+
67+
问题 [链接](http://stackoverflow.com/questions/402504/how-to-determine-the-variable-type-in-python)
68+
69+
使用type
70+
71+
>>> i = 123
72+
>>> type(i)
73+
<type 'int'>
74+
>>> type(i) is int
75+
True
76+
>>> i = 123456789L
77+
>>> type(i)
78+
<type 'long'>
79+
>>> type(i) is long
80+
True
81+
>>> i = 123.456
82+
>>> type(i)
83+
<type 'float'>
84+
>>> type(i) is float
85+
True
86+
87+
另外一个相同的问题 [链接](http://stackoverflow.com/questions/2225038/python-determine-the-type-of-an-object)
88+
89+
>>> type( [] ) == list
90+
True
91+
>>> type( {} ) == dict
92+
True
93+
>>> type( "" ) == str
94+
True
95+
>>> type( 0 ) == int
96+
True
97+
98+
>>> class Test1 ( object ):
99+
pass
100+
>>> class Test2 ( Test1 ):
101+
pass
102+
>>> a = Test1()
103+
>>> b = Test2()
104+
>>> type( a ) == Test1
105+
True
106+
>>> type( b ) == Test2
107+
True
108+
>>> type( b ) == Test1
109+
False
110+
>>> isinstance( b, Test1 )
111+
True
112+
>>> isinstance( b, Test2 )
113+
True
114+
>>> isinstance( a, Test1 )
115+
True
116+
>>> isinstance( a, Test2 )
117+
False
118+
>>> isinstance( [], list )
119+
True
120+
>>> isinstance( {}, dict )
121+
True

0 commit comments

Comments
 (0)