1.将十六进制字符串转换为十进制数值
搜索关键字:
http://search.aol.com/aol/search?q=python+hex+to+decimal
>>> print int('82a', 16)
参考链接:
- http://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python
- http://www.quora.com/How-can-I-convert-hex-to-decimal-in-Python
- http://stackoverflow.com/questions/9210525/how-do-i-convert-hex-to-decimal-in-python
2.Python的内建find方法
http://search.aol.com/aol/search?q=python+str+find
string.find(s, sub[, start[, end]])
Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices.
参考链接:
- http://www.thehelloworldprogram.com/python/python-string-methods/
- http://stackoverflow.com/questions/674764/examples-for-string-find-in-python
- http://stackoverflow.com/questions/2294493/how-to-get-the-position-of-a-character-in-python
- https://docs.python.org/2/library/string.html#string.find
3.Python中的in方法和str.find()的比较
确定搜索关键字:
http://search.aol.com/aol/search?q=Python+str+in+vs.+str.find
结论:推荐使用in方法,因为(一方面便于读写;另一方面效率更高)
参考链接:
- http://stackoverflow.com/questions/18437798/find-vs-in-operation-in-string-python
- http://stackoverflow.com/questions/12905513/python-in-keyword-efficiency
- http://www.hacksparrow.com/python-check-if-a-character-or-substring-is-in-a-string.html
4.Python中遍历的一些回顾
http://search.aol.com/aol/search?q=Python+traverse+step
参考链接:
- http://stackoverflow.com/questions/2990121/how-do-i-loop-through-a-python-list-by-twos
- http://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks
5.在Python中如何判断一个数的奇偶性
if number % 2 == 0: print 'even number' else: print 'odd number'
参考链接:
- http://stackoverflow.com/questions/13636640/python-checking-odd-even-numbers-and-changing-outputs-on-number-size
- http://stackoverflow.com/questions/14275118/python-remove-odd-number-from-a-list
6.Python的len()函数的效率
http://search.aol.com/aol/search?q=python+len%28%29+efficiency
结论:Python的len()函数是时间复杂度为O(1)的操作,非常快。
参考链接:
- http://stackoverflow.com/questions/699177/python-do-python-lists-keep-a-count-for-len-or-does-it-count-for-each-call
- http://stackoverflow.com/questions/8778691/profiled-performance-of-lenset-vs-set-len-in-python-3
- http://stackoverflow.com/questions/1115313/cost-of-len-function
拓展链接:
- https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt
- http://wiki.python.org/moin/TimeComplexity
7.Python的tuple(元组)
https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences
- Tuple 是不可变 list。 一旦创建了一个 tuple 就不能以任何方式改变它。
- 定义 tuple 与定义 list 的方式相同, 除了整个元素集是用小括号包围的而不是方括号。
- Tuple 的元素与 list 一样按定义的次序进行排序。 Tuples 的索引与 list 一样从 0 开始, 所以一个非空 tuple 的第一个元素总是 t[0]。
- 负数索引与 list 一样从 tuple 的尾部开始计数。
- 与 list 一样,分片 (slice) 也可以使用。注意当分割一个 list 时, 会得到一个新的 list ;当分割一个 tuple 时,会得到一个新的 tuple。
- Tuple 没有方法:没有 append 或 extend 方法、没有 remove 或 pop 方法、没有 index 方法、可以使用 in 来查看一个元素是否存在于 tuple 中。
参考链接:
拓展链接:
Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange
8.Python的几个常用的内置方法
- https://docs.python.org/2/library/string.html
- https://docs.python.org/2.7/library/stdtypes.html#str.split
9.Python的快速赋值
解包(unpack),批量赋(不同)值:
Jan, Mar, May, Jul, Aug, Oct, Dec = range(0, 7)
批量赋(相同的)值:
Jan = Mar = May = Jul = Aug = Oct = Dec = range(0,7)
搜索关键字:
http://search.aol.com/aol/search?q=Python+assign+multiple+variables+to+same+value+at+once
- http://stackoverflow.com/questions/10857654/python-assigning-multiple-variables-to-same-list-value
- http://stackoverflow.com/questions/22587239/if-python-assignments-dont-return-a-value-how-can-we-do-a-b-c-42
- https://docs.python.org/2/tutorial/introduction.html#numbers
- http://www.faqs.org/docs/diveintopython/odbchelper_multiassign.html
- http://stackoverflow.com/questions/16348815/python-assigning-multiple-variables-to-same-value-list-behavior
- http://stackoverflow.com/questions/5272995/python-assign-multiple-variables-at-once
- http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/variables.html
10.如何将一个list等分?
方法一:
Here’s a generator that yields the chunks you want:
def chunks(l, n): """ Yield successive n-sized chunks from l. """ for i in xrange(0, len(l), n): yield l[i:i+n] import pprint pprint.pprint(list(chunks(range(10, 75), 10))) #[[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]]
方法二:
If you want something super simple:
def chunks(l, n): if n < 1: n = 1 return [l[i:i + n] for i in range(0, len(l), n)]
参考链接:
http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python
11.Python中set的初始化
搜索关键字:
http://search.aol.com/aol/search?q=python+inital+an+empty+set
知识纲要:
[] = empty list
() = empty tuple
{} = empty dict
set() = empty set
No, there’s no literal syntax for the empty set. You have to write set(). 即,Python中set的初始化没有别的好方法,只能用 set() 方法。
set中的一些方法:
set.add(elem) #Add element elem to the set.向set中添加元素 set.discard(elem) #Remove element elem from the set if it is present.移除元素 set.clear() #Remove all elements from the set.移除所有元素 union() / intersection() / difference() / symmetric_difference() #一些其他方法
参考链接:
- http://stackoverflow.com/questions/6130374/empty-set-literal-in-python
- http://stackoverflow.com/questions/17373161/using-curly-braces-to-initialize-set
《“Python的一些小知识点_7”》 有 1 条评论
Getting the first three bytes of an IP address
https://stackoverflow.com/questions/23258092/getting-the-first-three-bytes-of-an-ip-address
`
>>> lhost = “192.168.1.23”
>>> lhost[:lhost.rfind(“.”)] + “.0-255”
‘192.168.1.0-255’
`