python基础(不定期更新)

1、python 寻找list中最大值、最小值并返回其所在位置

c = [1,2,4,0]
c.index(min(c))
c.index(max(c))

2、 获取N天、N小时、N分钟前的时间

from datetime import datetime

from datetime import timedelta

dd = datetime.now() - timedelta(days=days)(timedelta的参数可以是days、hours、minutes)

3、Python判断两个list相等

python判断list相等,可以将它们换成set来比较。忽略掉位置因素。

4、python2中为什么在进行类定义时最好要加object,不叫又怎样。

class Person:
    name = "zhengtong"


class Animal(object):
    name = "chonghong"



if __name__ == "__main__":
    x = Person()
    print "Person", dir(x)

    y = Animal()
    print "Animal", dir(y)


output:
Person ['__doc__', '__module__', 'name']
Animal ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']

可以发现Person类,只拥有了doc、module和自己定义的name变量。
Animal类继承了object对象,拥有了很多可操作的对象。
这些都是类中的高级特性。

猜你喜欢

转载自blog.csdn.net/qq_35462323/article/details/82775395
今日推荐