随笔分类 -  python基础零碎知识点

摘要:#!/usr/bin/python3 # _*_ Coding: UTF-8 _*_ from __future__ import division import collections import copy import math import operator import pickle im 阅读全文
posted @ 2020-06-24 15:10 bin-y 阅读(167) 评论(0) 推荐(0)
摘要:1.生成器 1.带yield就是生成器,因为生成器是用yield返回结果,而不是return。 2.列表有列表推导式,生成器当然也有生成器表达式。如:(for i in range(10)) 是小括号就是生成器,中括号就是列表推导式 生成器返回按需产生结果的一个对象,而不是一次构建一个结果列表 本质 阅读全文
posted @ 2019-05-31 20:52 bin-y 阅读(266) 评论(0) 推荐(0)
摘要:def func(): a=1 def bibao(): a+=1 return a return bibao c=func() c() 这是因为在执行代码 c = foo()时,python会导入全部的闭包函数体bar()来分析其的局部变量,python规则指定所有在赋值语句左面的变量都是局部变量 阅读全文
posted @ 2019-05-31 10:10 bin-y 阅读(80) 评论(0) 推荐(0)
摘要:def shuffle(self, x, random=None): if random is None: randbelow = self._randbelow for i in reversed(range(1, len(x))): # pick an element in x[:i+1] wi 阅读全文
posted @ 2019-05-30 12:08 bin-y 阅读(145) 评论(0) 推荐(0)
摘要:dic={'a':1,'b':2,'c':3} print(dic.items()) #dict_items([('a', 1), ('b', 2), ('c', 3)]) print(set(dic.items())) #{('c', 3), ('a', 1), ('b', 2)} items() 阅读全文
posted @ 2019-05-30 10:27 bin-y 阅读(415) 评论(0) 推荐(0)
摘要:都知道sorted本身的排序规则是从小到大 如果我们想要重新定义一个排序规则,可以设置参数如: key=lambda x:abs(x),这样他们就按照绝对值的大小进行排序 如果我们想要定义两个排序规则呢? 阅读全文
posted @ 2019-05-29 22:23 bin-y 阅读(2621) 评论(0) 推荐(0)