格式化字符串转换成字典

问题:

“a=1,b=2,c=3”
要转换成字典:
{ ‘a’:1,
‘b’:2,
‘c’:3}
有什么方便快捷的方法呢?

解答:

s = 'a=1,b=2,c=3'
dict((l.split('=') for l in s.split(',')))
如果 要把value转换成int型的
d = dict(((lambda i: (i[0], int(i[1])))(l.split('=')) for l in s.split(',')))

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
函数
def add_end(L=[]):
L.append('END')
return L
>>> add_end()
['END']
>>> add_end()
['END', 'END']

原因解释如下:

Python函数在定义的时候,默认参数L的值就被计算出来了,即[],因为默认参数L也是一个变量,它指向对象[],每次调用该函数,如果改变了L的内容,则下次调用时,默认参数的内容就变了,不再是函数定义时的[]了。

所以,定义默认参数要牢记一点:默认参数必须指向不变对象!

可变参数:在函数内部,参数numbers接收到的是一个tuple

def calc(*numbers):
sum = 0
for n in numbers:
sum = sum + n * n
return sum
>>> nums = [1, 2, 3]
>>> calc(*nums)
关键字参数
def func(a, b, c=0, *args, **kw):
print 'a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw
>>> args = (1, 2, 3, 4)
>>> kw = {'x': 99}
>>> func(*args, **kw)
a = 1 b = 2 c = 3 args = (4,) kw = {'x': 99}

所以,对于任意函数,都可以通过类似func(*args, **kw)的形式调用它,无论它的参数是如何定义的。

enumerate
>>> for i, value in enumerate(['A', 'B', 'C']):
... print i, value
...
0 A
1 B
2 C

In [15]: range(4)
Out[15]: [0, 1, 2, 3]

map reduce filter
>>> def f(x):
... return x * x
...
>>> map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
[1, 4, 9, 16, 25, 36, 49, 64, 81]

reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是:

reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素

def is_odd(n):
return n % 2 == 1 filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15])
# 结果: [1, 5, 9, 15]
排序sorted(iterable, cmp=None, key=None, reverse=False)

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>print sorted(L, cmp=lambda x,y:cmp(x[1],y[1]))
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

>>>print sorted(L, key=lambda x:x[1]))
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

注:效率key>cmp(key比cmp快)在Sorting Keys中:我们看到,此时排序过的L是仅仅按照第二个关键字来排的,如果我们想用第二个关键字
排过序后再用第一个关键字进行排序呢?
>>> L = [('d',2),('a',4),('b',3),('c',2)]
>>> print sorted(L, key=lambda x:(x[1],x[0]))
>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]


字典排序按value排序
sorted(dict.items(), lambda x, y: cmp(x[1], y[1]))
#降序
sorted(dict.items(), lambda x, y: cmp(x[1], y[1]), reverse=True)

获取对象信息
>>> import types
>>> type('abc')==types.StringType
True
>>> type(u'abc')==types.UnicodeType
True
>>> type([])==types.ListType
True
>>> type(str)==types.TypeType
True
isinstance()
>>> isinstance('a', str)
True
>>> isinstance(u'a', unicode)
True
>>> isinstance('a', unicode)
False
collections

https://docs.python.org/2/library/collections.html

namedtuple
>>> from collections import namedtuple
>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(1, 2)
>>> p.x
1
>>> p.y
2
OrderedDict
>>> from collections import OrderedDict
>>> d = dict([('a', 1), ('b', 2), ('c', 3)])
>>> d # dict的Key是无序的
{'a': 1, 'c': 3, 'b': 2}
>>> od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> od # OrderedDict的Key是有序的
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
Counter

Counter实际上也是dict的一个子类

多进程

import signalfrom multiprocessing import cpu_count, Pool


CPU_COUNT = cpu_count()def init_worker():
signal.signal(signal.SIGINT, signal.SIG_IGN)# 需要返回结果用mappool = Pool(processes=CPU_COUNT, initializer=init_worker, maxtasksperchild=400)
return_list = pool.map(func, iterable_object)
pool.close()
pool.join()#不需要返回结果用apply_asyncpool = Pool(processes=CPU_COUNT, initializer=init_worker, maxtasksperchild=400)
for filename in train_label2['md5']:
pool.apply_async(cp_test_file, (filename,))
pool.close()
pool.join()

多线程

import Queue
import threading

class WorkManager(object):
def __init__(self, start_id, end_id, thread_num=20):
self.work_queue = Queue.Queue()
self.threads = []
self.__init_work_queue(start_id, end_id)
self.__init_thread_pool(thread_num)

def __init_thread_pool(self, thread_num):
for i in range(thread_num):
self.threads.append(Work(self.work_queue))

def __init_work_queue(self, start_id, end_id):
for i in range(start_id, end_id + 1):
# 任务入队,Queue内部实现了同步机制
self.work_queue.put((get_sig_1, i))

def wait_allcomplete(self):
for item in self.threads:
if item.isAlive():
item.join()

class Work(threading.Thread):
def __init__(self, work_queue):
threading.Thread.__init__(self)
self.work_queue = work_queue
self.start()

def run(self):
# 死循环,从而让创建的线程在一定条件下关闭退出
while True:
try:
do, args = self.work_queue.get(block=False) # 任务异步出队,Queue内部实现了同步机制
do(args)
self.work_queue.task_done() # 通知系统任务完成
except:
break
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'))
fh.setFormatter(formatter)
log.addHandler(fh)
ch = logging.StreamHandler()
ch.setFormatter(formatter)
log.addHandler(ch)



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))
log.info('train_label.shape: %s', train_label.shape)

二维数组拉平成一维数组

Python语法点滴的更多相关文章

  1. 对 Python 语法不够了解导致的 bug

    对 Python 语法不够了解导致的 bug. `in` '20' in '11264,6144,4096,3072,2048,1024,300,30' Out[7]: True a_list = ' ...

  2. python 笔记2:python语法基础

    python语法学习笔记: 1 输入输出 input(),print(). name = input('input your name : ')print('hello ,'+name)print(& ...

  3. python语法快速入门(1)

    http://www.runoob.com/python/python-tutorial.html Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节.类似于PHP和Perl语言 ...

  4. python语法笔记(四)

    1.对象的属性     python一切皆对象,每个对象都可能有多个属性.python的属性有一套统一的管理方案. 属性的__dict__系统     对象的属性可能来自于其类定义,叫做类属性:还可能 ...

  5. python语法-[with来自动释放对象]

    python语法-[with来自动释放对象] http://www.cnblogs.com/itech/archive/2011/01/13/1934779.html 一 with python中的w ...

  6. wxpython 支持python语法高亮的自定义文本框控件的代码

    在研发闲暇时间,把开发过程中比较重要的一些代码做个珍藏,下面的代码内容是关于wxpython 支持python语法高亮的自定义文本框控件的代码,应该是对大家也有用. import keywordimp ...

  7. Python语法的转义字符

    Python语法的转义字符 转义字符 说 明 \ 续行符 \n 换行符 \0 空  \t 水平制表符,用于横向跳到下一制表位 \'' 双引号 \' 单引号 \\ 一个反斜杠 \f 换页 \0dd 八进 ...

  8. Python语法教程总结规范

    Python语法易错点记录 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...

  9. 初试Python语法小试牛刀之冒泡排序

    Python很火,心里很慌,没吃过猪肉,也要见见猪走路. 看了几天Python的语法,大概初步了解了一点点,https://www.liaoxuefeng.com/wiki/0014316089557 ...

随机推荐

  1. aix 禁止root远程登录

    Aix禁止root远程登录 aix用户扩展信息大都在/etc/security/user这个文本文件里.你可以修改: login=false 用户不能登录系统 rlogin=false 用户不能从远程 ...

  2. this和$(this)

    this指的是javascript对象而$(this)就是就jquery对象 jQuery.prototype.test=function(){ this.css("color", ...

  3. Oracle wm_concat(列转行函数)实际使用

    接触到了一个开发需求.其中是要把NC单据表体行的字段拼成一个字符串.例如: id name work age 1 王一 搬运工 20 2 李二 清洁工 21 3 张三 洗脚工 22 出现结果字符串为: ...

  4. Eclipse连接MySQL出现Server time zone is unrecognized错误

    错误代码: The server time zone value '?й???????' is unrecognized or represents more than one time zone. ...

  5. CMD常用指令

    列出所有端口使用情况 netstat -ano 只查看端口5060使用情况 netstat -ano|findstr "5060" 查看进程8612的信息 tasklist|fin ...

  6. D3.js

    html代码: <div id="id"> <p>Apple</p> <p id="aa">Pear</p ...

  7. C++引用(&)详解

    C++引用详解 引用的概念 引用:就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样. 引用的声明方法:类型标识符 &引用名=目标变量名: 如下:定义引用ra,它是变量a的引 ...

  8. 自己通过centos6.5配置NFS 成功后的笔记,希望对需要的人有点点帮助吧!

         环境介绍:            服务器:centos  172.16.250.170            客户端:centos  172.16.250.172     先用rpm -qa ...

  9. 定位(position)

    position :属性规定元素的定位类型 语法: position : static | absolute | fixed | relative JavaScript语法:object.style. ...

  10. php7.0 和 php7.1新特性

    PHP7.1 新特性 1.可为空(Nullable)类型 类型现在允许为空,当启用这个特性时,传入的参数或者函数返回的结果要么是给定的类型,要么是 null .可以通过在类型前面加上一个问号来使之成为 ...