随笔分类 - PYTHON
python3 生成随机数组
摘要:import random [num for num in random.sample(range(10), 10)]
阅读全文
python extend append
摘要:append,将对象直接添加到原数组尾部,不加修改,直接添加 extend,添加的对象被解析为iterable对象,将其中元素逐一添加到原数组尾部 文档中解析分别是: extend: append object to end append: extend list by appending elem
阅读全文
Walking Robot Simulation
摘要:A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands: -2: turn left 90 de
阅读全文
python remove与del 的区别
摘要:在leetcode中遇到一个题目涉及到数组的删除元素的操作:remove和del 题目大意是将一个数组中的重复数据删除 提交的代码如下(python3): 根据代码执行及找到的资料总结如下: 1、del删除的是对数据对象的引用,并不触及到数据本身,removeDuplicates2中使用del(el
阅读全文
python any()和all()
摘要:#any(x)判断x对象是否为空对象,如果都为空、0、false,则返回false,如果不都为空、0、false,则返回true #all(x)如果all(x)参数x对象的所有元素不为0、''、False或者x为空对象,则返回True,否则返回False
阅读全文
python3无穷大,无穷小
摘要:无穷大 float('inf') 无穷小 float('-inf')
阅读全文
python3 数据库操作
摘要:#!/usr/bin/python3 #coding: utf-8 import sqlite3 conn = sqlite3.connect('/tmp/python_db') cursor = conn.cursor() try: cursor.execute('create table user(id varchar(32) primary key, name varchar...
阅读全文
python3 学习中的遇到一些难点
摘要:1、python语句过长时,使用 / + enter,但在[] {} () 2、数字类型:int bool(True False) float complex(1+2j) 3、python可以同时为多个变量 赋值 4、一个变量可以通过赋值指向不同类型的对象 5、/运算返回一个浮点数,//返回整数 6
阅读全文
Linux下如果python2和python3同时存在使用pip3
摘要:Linux下如果python2和python3同时存在 安装pip sudo apt-get install python-pip sudo apt-get install python3-pip 使用时指明: sudo pip2 install sth sudo pip3 install sth
阅读全文
Python 使用正则表达式
摘要:当使用正则表达式时,re 模块内部会干两件事情 1、编译正则表达式,如其字串本身不合法,报错 2、用编译后的正则表达式去匹配字符串 re_telephone = re.compile(r'^(\d{3})-{\d{3,8}}$') re_telephone.match('010-12345').gr
阅读全文
Python 的装饰器
摘要:Python 在语言级别提供了装饰器模式的实现,代码中Python内置的 functools.wraps 会完成包括函数名属性处理替换
阅读全文
Python 单例模式
摘要:class Singleton(object): def __new__(cls, *args, **kw): if not hasattr(cls, '_instance'): orig = super(Singleton, cls) cls._instance = orig.__new__(cls, *args, **k...
阅读全文
PYTHON基本运算符
摘要:算术运算符:+ - * / % ** // 比较操作符:== != <> > < >= <= 赋值运算符:= += -= *= /= **= //= 位运算符:& | ^ ~ << >> 逻辑运算符:and or not 成员运算符:in not in 标识运算符:is is not
阅读全文