python学习笔记(排序、计算表达式 、all()、any()、 枚举函数 、zip打包函数、 set() 函数、 过滤器filter、 打印时间、 等分list)

    技术2022-07-11  123

    目录

    一些实用函数排序:计算表达式all()、any()枚举函数zip打包函数set() 函数过滤器filter打印时间等分list

    一些实用函数

    排序:

    sorted()排序函数

    >>> a = [1,4,2,3,1] # 降序 >>> sorted(a,reverse=True) [4, 3, 2, 1, 1] >>> a = [{'name':'xiaoming','age':18,'gender':'male'}, {'name':'xiaohong','age':20,'gender':'female'}] # 按 age升序 >>> sorted(a,key=lambda x: x['age'],reverse=False) [{'name': 'xiaoming', 'age': 18, 'gender': 'male'}, {'name': 'xiaohong', 'age': 20, 'gender': 'female'}]

    lambda函数 lambda argument_list: expression表示的是一个函数。这个函数叫做lambda函数。 上述例子中使用lambda函数将sorted的关键词定义为age

    计算表达式

    计算字符串型表达式的值

    >>> s = "1 + 3 +5" >>> eval(s) 9 >>> eval('[1,3,5]*3')

    all()、any()

    如果可迭代对象的所有元素都为真,那么返回 True,否则返回False #有0,所以不是所有元素都为真

    >>> all([1,0,3,6]) False #所有元素都为真 >>> all([1,2,3]) True

    接受一个可迭代对象,如果可迭代对象里至少有一个元素为真,那么返回True,否则返回False

    # 没有一个元素为真 >>> any([0,0,0,[]]) False # 至少一个元素为真 >>> any([0,0,1]) True

    枚举函数

    enumerate(sequence, [start=0])

    >>> s = ['a','b','c'] >>> for i in enumerate(s): ... print(i) ... (0, 'a') (1, 'b') (2, 'c') >>> type(enumerate(s)) <class 'enumerate'> >>> for i,v in enumerate(s): ... print(i,v) ... 0 a 1 b 2 c

    zip打包函数

    >>> x = [3,2,1] >>> y= [4,5,6] >>> zip(x,y) <zip object at 0x7fbe7cb54a00> >>> type(zip(x,y)) <class 'zip'> >>> for i in zip(x,y): ... print(i) ... (3, 4) (2, 5) (1, 6) >>> z= [7,8,9] >>> for i in zip(x,y,z): ... print(i) ... (3, 4, 7) (2, 5, 8) (1, 6, 9)

    set() 函数

    set()创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。

    >>>x = set('runoob') >>>y = set('google') >>>x, y (set(['b', 'r', 'u', 'o', 'n']), set(['e', 'o', 'g', 'l'])) # 重复的被删除 >>>x & y # 交集 set(['o']) >>>x | y # 并集 set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u']) >>>x - y # 差集 set(['r', 'b', 'u', 'n']) >>>

    过滤器filter

    filter(function, iterable)

    function – 判断函数,可以自己写判断函数。 iterable – 可迭代对象。

    函数通过 lambda 表达式设定过滤条件,保留 lambda 表达式为True的元素:

    >>> lst = [1,22,33,1231,5,7] >>> fil = filter(lambda x:x>10,lst] >>> fil = filter(lambda x:x>10,lst) >>> fil <filter object at 0x7fbe7ca7dd00> >>> for i in fil: ... print(i) ... 22 33 1231

    打印时间

    使用time模块打印当前时间

    # 导入time模块 >>> import time # 打印当前时间,返回浮点数 >>> seconds = time.time() >>> seconds 1593590373.5088582 # 浮点数转时间结构体 >>> local_time = time.localtime(seconds) >>> local_time time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=15, tm_min=59, tm_sec=33, tm_wday=2, tm_yday=183, tm_isdst=0) """ otm_year: 年 otm_mon: 月 otm_mday: 日 otm_hour: 小时 otm_min:分 otm_sec: 分 otm_sec: 秒 otm_wday: 一周中索引([0,6], 周一的索引:0) otm_yday: 一年中索引([1,366]) otm_isdst: 1 if summer time is in effect, 0 if not, and -1 if unknown """ # 时间结构体转时间字符串 >>> str_time = time.asctime(local_time) >>> str_time 'Wed Jul 1 15:59:33 2020' # 时间结构体转指定格式的时间字符串 >>> format_time = time.strftime('%Y年%m月%d日 %H:%M:%S',local_time) >>> format_time '2020年07月01日 15:59:33' # 时间字符串转时间结构体 >>> time.strptime(format_time,'%Y年%m月%d日 %H:%M:%S') time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=15, tm_min=59, tm_sec=33, tm_wday=2, tm_yday=183, tm_isdst=0) # 年的日历图 >>> import calendar >>> from datetime import date >>> mydate=date.today() >>> calendar.calendar(2020) # 月的日历图 >>> import calendar >>> from datetime import date >>> mydate = date.today() >>> calendar.month(mydate.year, mydate.month) # 判断是否为闰年 >>> import calendar >>> from datetime import date >>> mydate = date.today() >>> is_leap = calendar.isleap(mydate.year) >>> ("{}是闰年" if is_leap else "{}不是闰年\n").format(mydate.year) '2020是闰年'

    等分list

    from math import ceil def divide_iter(lst, n): if n <= 0: yield lst return i, div = 0, ceil(len(lst) / n) while i < n: yield lst[i * div : (i + 1) * div] i += 1 for group in divide_iter([1,2,3,4,5],2): print(group) [1, 2, 3] [4, 5]

    ceil()函数 对于浮点数直接进一。

    Processed: 0.009, SQL: 9