您的位置:首页 > 编程语言 > Python开发

Python语法点滴

2016-04-14 14:29 375 查看

格式化字符串转换成字典

问题: “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
[b]string.index(str, beg=0, end=len(string)) 跟find()方法一样,只不过如果str不在 string中会报一个异常.
[b]string.join(seq) 以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
string.lower() string.upper() [b]string.strip([obj])

string.split(str="", num=string.count(str)) 以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串
string.title() 返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())
[/b][/b][/b]
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.htmlnamedtuple
>>> 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)
二维数组拉平成一维数组


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: