Python语法点滴
格式化字符串转换成字典问题: “a=1,b=2,c=3” 解答: s = 'a=1,b=2,c=3' http://www.360doc.com/content/15/0331/11/5711743_459502912.shtml |
string: string.capitalize() 把字符串的第一个字符大写 string.startswith(obj) string.endswith(obj) string.find(str, beg=0, end=len(string)) 检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1 string.index(str, beg=0, end=len(string)) 跟find()方法一样,只不过如果str不在 string中会报一个异常. string.join(seq) 以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串 string.lower() string.upper() string.strip([obj]) string.split(str="", num=string.count(str)) 以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串 string.title() 返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle()) |
List 函数: len() max() min() list(seq) 方法: list.append(obj) list.extend(seq) list.count(obj) list.index(obj) list.insert(index, obj) list.pop(obj=list[-1]) list.remove(obj) list.reverse() == list[::-1] Tuple只有对应的函数,最后一个为tuple(seq) |
字典 radiansdict.get(key, default=None) 返回指定键的值,如果值不在字典中返回default值 radiansdict.update(dict2) 把字典dict2的键/值对更新到dict里 del adict[key] |
set set.add(key) set.remove(key) set1 & set2 set1 | set2 |
函数
原因解释如下: Python函数在定义的时候,默认参数 所以,定义默认参数要牢记一点:默认参数必须指向不变对象! 可变参数:在函数内部,参数
所以,对于任意函数,都可以通过类似 |
enumerate
In [15]: range(4) |
map reduce filter
reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是:
|
排序sorted(iterable, cmp=None, key=None, reverse=False)
>>>L = [('b',2),('a',1),('c',3),('d',4)] >>>print sorted(L, key=lambda x:x[1])) 注:效率key>cmp(key比cmp快)在Sorting Keys中:我们看到,此时排序过的L是仅仅按照第二个关键字来排的,如果我们想用第二个关键字 字典排序按value排序 |
获取对象信息
isinstance()
|
collectionshttps://docs.python.org/2/library/collections.html namedtuple
OrderedDict
Counter
|
多进程 import signalfrom multiprocessing import cpu_count, Pool CPU_COUNT = cpu_count()def init_worker(): 多线程 import Queue
|
logging import logging.handlerslog = logging.getLogger() formatter = logging.Formatter("%(asctime)s [%(name)s] %(levelname)s: %(message)s") fh = logging.handlers.WatchedFileHandler(os.path.join(root_dir, 'pe_analyzer.log')) log.setLevel(logging.INFO) log.info('imp_x.shape: %s, ops_x.shape: %s, imp_y.shape: %s' % (imp_x.shape, ops_x.shape, imp_y.shape)) |
二维数组拉平成一维数组 |
Python语法点滴的更多相关文章
- 对 Python 语法不够了解导致的 bug
对 Python 语法不够了解导致的 bug. `in` '20' in '11264,6144,4096,3072,2048,1024,300,30' Out[7]: True a_list = ' ...
- python 笔记2:python语法基础
python语法学习笔记: 1 输入输出 input(),print(). name = input('input your name : ')print('hello ,'+name)print(& ...
- python语法快速入门(1)
http://www.runoob.com/python/python-tutorial.html Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节.类似于PHP和Perl语言 ...
- python语法笔记(四)
1.对象的属性 python一切皆对象,每个对象都可能有多个属性.python的属性有一套统一的管理方案. 属性的__dict__系统 对象的属性可能来自于其类定义,叫做类属性:还可能 ...
- python语法-[with来自动释放对象]
python语法-[with来自动释放对象] http://www.cnblogs.com/itech/archive/2011/01/13/1934779.html 一 with python中的w ...
- wxpython 支持python语法高亮的自定义文本框控件的代码
在研发闲暇时间,把开发过程中比较重要的一些代码做个珍藏,下面的代码内容是关于wxpython 支持python语法高亮的自定义文本框控件的代码,应该是对大家也有用. import keywordimp ...
- Python语法的转义字符
Python语法的转义字符 转义字符 说 明 \ 续行符 \n 换行符 \0 空 \t 水平制表符,用于横向跳到下一制表位 \'' 双引号 \' 单引号 \\ 一个反斜杠 \f 换页 \0dd 八进 ...
- Python语法教程总结规范
Python语法易错点记录 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...
- 初试Python语法小试牛刀之冒泡排序
Python很火,心里很慌,没吃过猪肉,也要见见猪走路. 看了几天Python的语法,大概初步了解了一点点,https://www.liaoxuefeng.com/wiki/0014316089557 ...
随机推荐
- aix 禁止root远程登录
Aix禁止root远程登录 aix用户扩展信息大都在/etc/security/user这个文本文件里.你可以修改: login=false 用户不能登录系统 rlogin=false 用户不能从远程 ...
- this和$(this)
this指的是javascript对象而$(this)就是就jquery对象 jQuery.prototype.test=function(){ this.css("color", ...
- Oracle wm_concat(列转行函数)实际使用
接触到了一个开发需求.其中是要把NC单据表体行的字段拼成一个字符串.例如: id name work age 1 王一 搬运工 20 2 李二 清洁工 21 3 张三 洗脚工 22 出现结果字符串为: ...
- Eclipse连接MySQL出现Server time zone is unrecognized错误
错误代码: The server time zone value '?й???????' is unrecognized or represents more than one time zone. ...
- CMD常用指令
列出所有端口使用情况 netstat -ano 只查看端口5060使用情况 netstat -ano|findstr "5060" 查看进程8612的信息 tasklist|fin ...
- D3.js
html代码: <div id="id"> <p>Apple</p> <p id="aa">Pear</p ...
- C++引用(&)详解
C++引用详解 引用的概念 引用:就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样. 引用的声明方法:类型标识符 &引用名=目标变量名: 如下:定义引用ra,它是变量a的引 ...
- 自己通过centos6.5配置NFS 成功后的笔记,希望对需要的人有点点帮助吧!
环境介绍: 服务器:centos 172.16.250.170 客户端:centos 172.16.250.172 先用rpm -qa ...
- 定位(position)
position :属性规定元素的定位类型 语法: position : static | absolute | fixed | relative JavaScript语法:object.style. ...
- php7.0 和 php7.1新特性
PHP7.1 新特性 1.可为空(Nullable)类型 类型现在允许为空,当启用这个特性时,传入的参数或者函数返回的结果要么是给定的类型,要么是 null .可以通过在类型前面加上一个问号来使之成为 ...