python记录 trees

log()函数:

log()方法返回x的自然对数,对于x>0。

以下是log()方法的语法:

1
2
3
import math
 
math.log( x )

注意:此函数是无法直接访问的,所以我们需要导入math模块,然后需要用math的静态对象来调用这个函数。
参数

  •     x -- 这是一个数值表达式。

返回值

此方法返回x的自然对数,对于x>0。


例子

下面的例子显示了log()方法的用法。

?
1
2
3
4
5
6
7
#!/usr/bin/python
import math  # This will import math module
 
print "math.log(100.12) : " , math.log( 100.12 )
print "math.log(100.72) : " , math.log( 100.72 )
print "math.log(119L) : " , math.log( 119L )
print "math.log(math.pi) : " , math.log(math.pi)

当我们运行上面的程序,它会产生以下结果:

?
1
2
3
4
math.log( 100.12 ) : 4.60636946656
math.log( 100.72 ) : 4.61234438974
math.log( 119L ) : 4.77912349311
math.log(math.pi) : 1.14472988585


另外,可以通过log(x[, base])来设置底数,如 log(x, 10) 表示以10为底的对数。


example函数:
for example in dataset: --------for循环,在数据dataset(可以是列表、元组、集合、字典等)中 逐个取值存入 变量 example中,然后运行循环体。

set函数:

python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.

sets 支持 x in set, len(set),和 for x in set。作为一个无序的集合,sets不记录元素位置或者插入点。因此,sets不支持 indexing, slicing, 或其它类序列(sequence-like)的操作。

1
2
3
4
>>> x =  set ( 'spam' )
>>> y =  set ([ 'h' , 'a' , 'm' ])
>>> x, y
( set ([ 'a' 'p' 's' 'm' ]),  set ([ 'a' 'h' 'm' ]))

1
2
3
4
5
6
>>> x & y  # 交集
set ([ 'a' 'm' ])
>>> x | y  # 并集
set ([ 'a' 'p' 's' 'h' 'm' ])
>>> x - y  # 差集
set ([ 'p' 's' ])

去除海量列表里重复元素,用hash来解决也行,只不过感觉在性能上不是很高,用set解决还是很不错的,示例如下:

1
2
3
4
5
6
7
>>> a = [11,22,33,44,11,22]
>>> b =  set (a)
>>> b
set ([33, 11, 44, 22])
>>> c = [i  for  in  b]
>>> c
[33, 11, 44, 22]

count函数:

count() 方法用于统计某个元素在列表中出现的次数。

语法

count()方法语法:

list.count(obj)

参数

  • obj -- 列表中统计的对象。

返回值

返回元素在列表中出现的次数。

实例

以下实例展示了 count()函数的使用方法:

#!/usr/bin/python

aList = [123, 'xyz', 'zara', 'abc', 123];

print "Count for 123 : ", aList.count(123);
print "Count for zara : ", aList.count('zara');

以上实例输出结果如下:

Count for 123 :  2
Count for zara :  1

del函数

del()用于list列表操作,删除一个或者连续几个元素。

>>> a = [-1, 3, 'aa', 85] # 定义一个list
>>> a
[-1, 3, 'aa', 85]
>>> del a[0]      # 删除第0个元素
>>> a
[3, 'aa', 85]
>>> del a[2:4]   # 删除从第2-3个元素。
>>> a
[3, 'aa']
>>> del a       # 删除整个list
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

index()函数

一般用处是在序列中检索参数并返回第一次出现的索引,没找到就会报错,比如:

>>> t=tuple('Allen')
>>> t
('A', 'l', 'l', 'e', 'n')
>>> t.index('a')
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    t.index('a')
ValueError: tuple.index(x): x not in tuple
>>> t.index('e')
3
>>> t.index('l')
1
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

但参数可能会出现很多次,要如何做呢?

index()函数的完整语法是这样的:

str.index(str, beg=0, end=len(string))

str – 指定检索的字符串 
beg – 开始索引,默认为0。 
end – 结束索引,默认为字符串的长度。

所以我们可以重新设置开始索引来继续寻找,如:

>>> t.index('l',2)
2
   
   
  • 1
  • 2

因为第一个’l’的出现位置是1,所以我们将开始索引加1继续寻找,果然,在索引为2的位置又找到了’l’。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值