随笔分类 - Python基础
摘要:from multiprocessing import Pool def double(x): print(x**2) return x**2 if __name__ == '__main__': pool = Pool() pool.map(double, list(range(11))) pri
阅读全文
摘要:expr_str="[1,2,3]" my_list=eval(expr_str) print(repr(my_list),type(my_list)) # [1,2,3] print(repr(expr_str),type(expr_str)) #'[1,2,3]' import ast # 用i
阅读全文
摘要:import operator somelist = [(1,5,8),(6,2,4),(9,7,5)] somelist.sort(key=operator.itemgetter(0)) print(somelist) # [(1, 5, 8), (6, 2, 4), (9, 7, 5)] som
阅读全文
摘要:dict.fromkeys(data,0) 默认字典,把data里面的值作为key,赋值给0 Counter对象的most_common(3)取出出现频率最高的3个 读取文件, 用re.split("\W+",txt)非字符进行分割,再用Counter进行处理
阅读全文
摘要:题目 经timeit测试 列表生成式比 filter(lambda x:x>=0,data) 快 python2 的dict的iteritems()方法, pyhton3可以看看有没有
阅读全文
摘要:def selection_sort(alist): n = len(alist) # 需要进行n-1次选择操作 a = 0 for i in range(n-1): # 记录最小位置 min_index = i exit = i # 从i+1位置到末尾选择出最小数据 for j in range(
阅读全文
摘要:# Start with a list of numbers that ain't sorted numbers=[0,5,1,4,2,8] # Keep track of whether any swaps were made on the previous i teration # If no
阅读全文
摘要:#_*_ encoding: utf-8 _*_ @author: ty hery 2018/10/6 # 发送邮件失败了 import smtplib #加载smtplib模块 from email.mime.text import MIMEText from email.utils import
阅读全文
摘要:1, 速度很快, 唯一缺陷是计数长度列表和排序的最大数字相等, 如果排序中的数字实在太大了, 创建的列表太长了比如2的32次方 import random def count_sort(li, max_count =21): count = [0 for _ in range(max_count+1
阅读全文
摘要:如果要使用classonlymethod ,则需要先定义好一个classonlymethod 类。 首先我们需要明白无论是classonlymethod还是classmethod,本质都是一个类,而classonlymethod继承了classmethod。 classonlymethodz作用:只
阅读全文
摘要:import unittest class MyTestCase(unittest.TestCase): def test_something(self): self.assertEqual(0, False) if __name__ == '__main__': unittest.main() 输
阅读全文
摘要:class Foo: def __init__(self,name): self.name = name def __getitem__(self,item): print('getitem') print(item) return self.__dict__.get[item] def __set
阅读全文
摘要:class People(object): color = 'yellow' #这就是一个公有属性,可以在类里和类外调用 __age = 30 #这就是一个私有属性,只能在类里调用。 def think(self): self.color = "black" print ("i am a %s" %
阅读全文
摘要:class People(object): color = "yellow" __age = 30 #私有属性 def think(self): self.color = "black" print("I am a %s" %self.color) print(self.__age) def __t
阅读全文
摘要:#_*_ encoding: utf-8 _*_ @author: ty hery 2018/9/6 def printStar(intNum): s = "*" spaceLength = intNum blockCount = int(intNum / 2 + 1) for i in range
阅读全文
摘要:这种变形的特点: 1,在类外部无法直接obj.__AttrName 2,在类内部可以直接使用:obj.__AttrName 3,子类无法覆盖父类__开头的属性 0, class A: '类的封装啊' __x = 1 # _A__x = 1 print('你去吃啥饭0000') def __init_
阅读全文
摘要:问题:1.)上面的__init__函数中,加*句我不写,为啥不可以?不理解里面怎么传递。。。初始化的时候不是先在内部调用了__init__,生成了L了吗? 下面函数不能用这里的L,甚至是 a,b这些变量? 2.)如果要让 init 下的变量传到别的函数,是不是一定要用self.var这种形式? 我自
阅读全文
摘要:'''一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法。 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用。 这有利于组织代码,把某些应该属于某个类的函数给放到那个类里去,同时有利于命名空间的整洁。 既然@staticmethod
阅读全文
摘要:class TSSS(): def f1(self): print('from TSSS') class SSS(TSSS): def f1(self): print('from SSS') class SS(): def f1(self): print('from SS') class S(SSS
阅读全文
摘要:def test1(): l = [] for i in range(1000): l = l + [i] def test2(): l = [] for i in range(1000): l.append(i) def test3(): l = [i for i in range(1000)]
阅读全文