Python的一些小知识点_7


1.将十六进制字符串转换为十进制数值

搜索关键字:

http://search.aol.com/aol/search?q=python+hex+to+decimal

>>> print int('82a', 16)

py_int

参考链接:

 

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.

参考链接:

 

3.Python中的in方法和str.find()的比较

确定搜索关键字:

http://search.aol.com/aol/search?q=Python+str+in+vs.+str.find

结论:推荐使用in方法,因为(一方面便于读写;另一方面效率更高)

参考链接:

 

4.Python中遍历的一些回顾

http://search.aol.com/aol/search?q=Python+traverse+step

参考链接:

 

5.在Python中如何判断一个数的奇偶性
if number % 2 == 0:
    print 'even number'
else:
    print 'odd number'
参考链接:

 

6.Python的len()函数的效率

http://search.aol.com/aol/search?q=python+len%28%29+efficiency

结论:Python的len()函数是时间复杂度为O(1)的操作,非常快。

参考链接:
拓展链接:

 

7.Python的tuple(元组)

https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences

  1. Tuple 是不可变 list。 一旦创建了一个 tuple 就不能以任何方式改变它。
  2. 定义 tuple 与定义 list 的方式相同, 除了整个元素集是用小括号包围的而不是方括号。
  3. Tuple 的元素与 list 一样按定义的次序进行排序。 Tuples 的索引与 list 一样从 0 开始, 所以一个非空 tuple 的第一个元素总是 t[0]。
  4. 负数索引与 list 一样从 tuple 的尾部开始计数。
  5. 与 list 一样,分片 (slice) 也可以使用。注意当分割一个 list 时, 会得到一个新的 list ;当分割一个 tuple 时,会得到一个新的 tuple。
  6. Tuple 没有方法:没有 append 或 extend 方法、没有 remove 或 pop 方法、没有 index 方法、可以使用 in 来查看一个元素是否存在于 tuple 中。
参考链接:
拓展链接:

Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange

 

8.Python的几个常用的内置方法

 

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

 

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() #一些其他方法
参考链接:
拓展链接:
,

《“Python的一些小知识点_7”》 有 1 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注