格式化字符串转换成字典

问题:

“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. hdu3415 Max Sum of Max-K-sub-sequence

       Max Sum of Max-K-sub-sequence Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64 ...

  2. [C++STDlib基础]关于单字符的操作——C++标准库头文件<cctype>

    网上实例 总结 /* _STD_BEGIN using _CSTD isalnum; using _CSTD isalpha; using _CSTD iscntrl; using _CSTD isd ...

  3. 用Bottle开发web程序(一)

    Bottle Bottle是一个轻量级的web app框架.相较与django等框架,bottle几乎没有任何依赖,而且只有一个文件.而相对于python默认的SimpleHTTPServer,功能更 ...

  4. Future 和 ExecutorCompletionService 对比和使用

    当我们通过Executor提交一组并发执行的任务,并且希望在每一个任务完成后能立即得到结果,有两种方式可以采取: 方式一: 通过一个list来保存一组future,然后在循环中轮训这组future,直 ...

  5. .NET基础——运算符

    这一篇我们来讲解C#中的运算符 1. C#中的算术运算符 5个算数运算符:+  -  *  /  %     它们都是二元运算符,*  /  % 的运算优先级相同,并且高于 +  - ,+  - 的运 ...

  6. .net Mongo Driver 1.0与2.0的对比与2.0的优化

    前言 最近闲的时间有点多,所以还是写博客吧. 有人说Mongo 2.0的写法难以把控,好多地方不知道咋用,所以坚持用1.0(不愿意去尝试2.0),我感觉不可理解.所以写篇博客比较下. Mongo C# ...

  7. 结构-行为-样式-angularJs 指令解决IE下无PlaceHolder的问题

    最近项目开发的时候遇到一个头疼的问题,在测试IE兼容性的时候,发现placeholder在IE下无效.查网上说也是有各种解决方案,但是都不是我想要的,于是决定自己写一个.思路:placeHolder是 ...

  8. UIView类绘图出现错误提示

    一:问题: Jan 16 15:49:53  CUBOT Band Ⅲ[2082] <Error>: CGContextSetLineWidth: invalid context 0x0. ...

  9. [Python]-类型转换

    1.字符串到数值的转换:int(s [,base ]) 将表达式s转换为一个整数 ,s可以是整数,与数字有关的字符串,布尔类型long(s [,base ]) 将表达式s转换为一个长整数 s可以是整数 ...

  10. Python基础之字符串

    字符串内置处理函数 1.capitalize() 描述: 将字符串的第一个字母变成大写,其他字母变小写. 示例: a= "hello world" print (a.capital ...