好記性不如爛筆頭

 帶關鍵字的格式化

>>> 
>>> print "Hello %(name)s !" % {'name':'James'}
Hello James !
>>> 
>>> print "Hello {name} !".format(name="James")
Hello James !
>>> 

  

使用dict.__missing__() 避免出現KeyError

If a subclass of dict defines a method __missing__() and key is not present, 
the d[key] operation calls that method with the key(key as argument). 

The d[key] operation then returns or raises whatever is returned or raised by the __missing__(key) call. 

 

>>> 
>>> class Counter(dict):
...     def __missing__(self, key):
...         return 0
... 
>>> c = Counter()
>>> print c['num']
0
>>> c['num'] += 1
>>> print c['num']
1
>>> 
>>> c
{'num': 1}
>>>

  

__getattr__ 調用默認方法

>>> 
>>> class A(object):
...     def __init__(self,num):
...         self.num = num
...         print 'init...'
...     def mydefault(self, *args, **kwargs):
...         print 'default func...'
...         print args
...         print kwargs
...     def __getattr__(self,name):
...             print 'No %(name)s found, goto default...' % {'name':name}
...         return self.mydefault
... 
>>> a1 = A(9)
init...
>>> a1.fn1()
No fn1 found, goto default...
default func...
()
{}
>>> a1.fn2(1,2)
No fn2 found, goto default...
default func...
(1, 2)
{}
>>> a1.fn3(name='standby',age=18)
No fn3 found, goto default...
default func...
()
{'age': 18, 'name': 'standby'}
>>> 
>>> 

  

obj.xxx = aaa 		觸發類的 __setattr__ 
obj.xxx       		觸發類的 __getattr__ 
obj['xxx'] = 'vvv'	觸發類的 __setitem__
obj['xxx']			觸發類的 __getitem__


with app1.app_context():    觸發	__enter__  __exit__

  

__new__ 和 __init__ 的執行順序

>>> 
>>> class B(object):
...     def fn(self):
...         print 'B fn'
...     def __init__(self):
...         print "B INIT"
... 
>>> class A(object):
...     def fn(self):
...         print 'A fn'
...     def __new__(cls,a):
...             print "NEW", a
...             if a>10:
...                 return super(A, cls).__new__(cls)
...             return B()
...     def __init__(self,a):
...         print "INIT", a
... 
>>> 
>>> a1 = A(5)
NEW 5
B INIT
>>> a1.fn()
B fn
>>> a2=A(20)
NEW 20
INIT 20
>>> a2.fn()
A fn
>>> 

  

 類繼承之 __class__

>>> 
>>> class A(object):
...     def show(self):
...         print 'base show'
... 
>>> class B(A):
...     def show(self):
...         print 'derived show'
... 
>>> obj = B()
>>> obj.show()
derived show
>>> 
>>> obj.__class__
<class '__main__.B'>
>>> 
>>> obj.__class__ = A
>>> obj.__class__
<class '__main__.A'>
>>> obj.show()
base show
>>> 

  

對象方法 __call__

>>> 
>>> class A(object):
...     def obj_func(self, *args, **kwargs):
...             print args
...             print kwargs
...     def __call__(self, *args, **kwargs):
...             print 'Object method ...'
...             return self.obj_func(*args, **kwargs)
... 
>>> a1=A()
>>> a1(9,name='standby',city='beijing')
Object method ...
(9,)
{'city': 'beijing', 'name': 'standby'}
>>> 

補充:html

>>> 
>>> class test(object):
...     def __init__(self, value):
...         self.x = value
...     def __call__(self, value):
...         return self.x * value
... 
>>> a = test(4)
>>> print a(5)
20
>>> 

 補充python

- 什麼後面能夠加括號?(只有4種表現形式)
		- 函數 		執行函數 
		- 類 		執行類的__init__方法
		- 方法           obj.func 
		- 對象 		前提:類裏有 __call__ 方法
					obj()  直接執行類的 __call__方法

 

關於類的繼承

>>> 
>>> class Parent(object):
...     x = 1
... 
>>> class Child1(Parent):
...     pass
... 
>>> class Child2(Parent):
...     pass
... 
>>> Child1.x = 2
>>> Parent.x = 3
>>> print Parent.x, Child1.x, Child2.x
3 2 3
>>> 

 

 類屬性和對象屬性

類屬性

>>> 
>>> class Student:
...     score = []
... 
>>> stu1 = Student()
>>> stu2 = Student()
>>> stu1.score.append(99)
>>> stu1.score.append(96)
>>> stu2.score.append(98)
>>> 
>>> 
>>> stu2.score
[99, 96, 98]
>>> 
>>>


對象屬性
>>> 
>>> class Student:
...     def __init__(self):;
...         self.lst = []
... 
>>> stu1 = Student()
>>> stu2 = Student()
>>> 
>>> 
>>> stu1.lst.append(1)
>>> stu1.lst.append(2)
>>> stu2.lst.append(9)
>>> 
>>> stu1.lst
[1, 2]
>>> 
>>> stu2.lst
[9]
>>>

 

一行代碼實現列表偶數位加3後求和

>>> a = [1,2,3,4,5,6]
>>> [item+3 for item in a if a.index(item)%2==0]
[4, 6, 8]
>>> result = sum([item+3 for item in a if a.index(item)%2==0])
>>> result
18
>>>

 

字符串鏈接

>>> 
>>> name = 'hi ' 'standby' ' !'
>>> name
'hi standby !'
>>>

  

Python解釋器中的 '_'

_ 即Python解釋器上一次返回的值express

>>> 
>>> range(5)
[0, 1, 2, 3, 4]
>>> _
[0, 1, 2, 3, 4]
>>> 

  

嵌套列表推導式

>>> 
>>> [(i, j) for i in range(3) for j in range(i)]
[(1, 0), (2, 0), (2, 1)]
>>> 

  

Python3 中的unpack

>>> 
>>> first, second, *rest, last = range(10)
>>> first
0
>>> second
1
>>> last
9
>>> rest
[2, 3, 4, 5, 6, 7, 8]
>>> 

  

 

關於__setattr__  __getattr__  __getitem__  __setitem__  參考:http://www.cnblogs.com/standby/p/7045718.html編程

 

Python把經常使用數字緩存在內存裏 *****

>>> 
>>> a = 1
>>> b = 1
>>> a is b
True
>>> 
>>> 
>>> a = 256
>>> b = 256
>>> a is b
True
>>> 
>>> a = 257
>>> b = 257
>>> a is b
False
>>>
>>> a = 300
>>> b = 300
>>> a is b
False
>>> 

注意:在[-5,256]之間的數字用在內存中的id號是相同的緩存

Python爲了提升運行效率而將這些經常使用數字緩存到內存裏了,因此他們的id號是相同的;閉包

另外,對a,b,c,....等的賦值也只是一種引用而已app

>>> 
>>> id(9)
10183288
>>> num = 9
>>> id(num)
10183288
>>> 

 

Python對於短字符串會使用同一個空間,可是對於長字符串會從新開闢空間

>>> 
>>> a = 'I love PythonSomething!'
>>> b = 'I love PythonSomething!'
>>> c = [1, 2, 3]
>>> d = [1, 2, 3]
>>> 
>>> a is b
False
>>> c is d
False
>>> 
>>> id(a)
139848068316272
>>> id(b)
139848068316336
>>> id(c)
139848068310152
>>> id(d)
139848068309936
>>> 

 

字符串 * 操做

>>> 
>>> def func(a):
...     a = a + '2'
...     a = a*2
...     return a
... 
>>> 
>>> func("hello")
'hello2hello2'
>>> 

  

Python浮點數比較

>>> 
>>> 0.1
0.10000000000000001
>>> 0.2
0.20000000000000001
>>> 0.1 + 0.2
0.30000000000000004
>>> 
>>> 0.3
0.29999999999999999
>>> 
>>> 0.1 + 0.2 == 0.3 
False
>>> 

  

Python裏的 '~' 取反

>>> 
>>> 5
5
>>> ~5
-6
>>> ~~5
5
>>> ~~~5
-6
>>> ~~~~5
5
>>> 

~5 即對5取反,獲得的是 -6 , 爲何?python2.7

參考:http://www.javashuo.com/article/p-rotslhii-gs.html 和 http://blog.csdn.net/u011080472/article/details/51280919ide

    - 原碼就是符號位加上真值的絕對值;函數式編程

    - 反碼的表示方法是:正數的反碼就是其自己;負數的反碼是在其原碼的基礎上, 符號位不變,其他各個位取反;

    - 補碼的表示方式是:正數的補碼就是其自己;負數的補碼是在其原碼的基礎上, 符號位不變, 其他各位取反, 最後+1 (即在反碼的基礎上+1)

  真值 原碼 反碼 補碼
5 +000 0101 0000 0101 0000 0101 0000 0101
-5 -000 0101 1000 0101 1111 1010 1111 1011

 

 

 

對5取反即對 0000 0101 取反, 獲得 1111 1010,那這個值的十進制是多少呢?

由於 負數在計算機中是以補碼形式表示的, 因此實際上就是求哪一個值的補碼是 1111 1010

按照上面的規則反向計算:

1111 1010  減1 獲得其反碼錶示:1111 1001

在保證符號位不變,其他各位取反:1000 0110 就是該值的原碼,對應真值就是 -000 0110 ,對應十進制就是 -6 。

 

那麼對 -6 取反,獲得的是多少呢?

對-6取反即對 -6 的補碼取反,就是對1111 1010取反,獲得 0000 0101,很明顯是一個正數。

而正數原碼==正數反碼==正數補碼,因此該值的原碼就是 0000 0101,真值就是 +000 0101,對應十進制就是 5。

 

bool()

>>> 
>>> bool('True')
True
>>> bool('False')
True
>>> bool('')
False
>>> bool()
False
>>> 
>>> 
>>> bool(1)
True
>>> bool(0)
False
>>> 

  

等價於

>>> 
>>> True==False==False
False
>>> 
>>> True==False and False==False
False
>>> 

  

>>> 
>>> 1 in [0,1]
True
>>> 1 in [0,1] == True
False
>>> 
>>> (1 in [0,1]) == True
True
>>> 
>>> 1 in ([0,1] == True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
>>> 
>>> 
>>> 
>>> (1 in [0,1]) and ([0,1] == True)
False
>>>

Note that comparisons, membership tests, and identity tests,

all have the same precedence and have a left-to-right chaining feature as described in the Comparisons section.

參考:https://stackoverflow.com/questions/31354429/why-is-true-is-false-false-false-in-python

 

while 結合 break

>>> 
>>> i = 0
>>> while i < 5:
...     print(i)
...     i += 1
...     if i == 3:
...         break
... else:
...     print(0)
... 
0
1
2
>>> 

  

set給list去重

>>> 
>>> nums = set([1,1,2,3,3,3,4])
>>> 
>>> nums
set([1, 2, 3, 4])
>>> 
>>> type(nums)
<type 'set'>
>>> 
>>> len(nums)
4
>>> 
>>> 
>>> li = list(nums)
>>> li
[1, 2, 3, 4]
>>> 
>>> type(li)
<type 'list'>
>>> 

 

函數是第一類對象(First-Class Object)

在 Python 中萬物皆爲對象,函數也不例外,

函數做爲對象能夠賦值給一個變量、能夠做爲元素添加到集合對象中、

可做爲參數值傳遞給其它函數,還能夠當作函數的返回值,這些特性就是第一類對象所特有的。

函數能夠嵌套,函數中裏面嵌套的函數不能在函數外面訪問,只能是在函數內部使用:

def get_length(text):
    def clean(t):
        return t[1:]
    res = clean(text)
    return len(res)

print(get_length('standby'))

  

Python裏的高階函數

函數接受一個或多個函數做爲輸入或者函數輸出(返回)的值是函數時,咱們稱這樣的函數爲高階函數。

Python內置函數中,典型的高階函數是 map 函數,map 接受一個函數和一個迭代對象做爲參數,

調用 map 時,依次迭代把迭代對象的元素做爲參數調用該函數。

def foo(text):
    return len(text)

li = map(foo, ["the","zen","of","python"])
print(li)        # <map object at 0x0000000001119FD0>
li = list(li)
print(li)        # [3, 3, 2, 6]

 

lambda應用場景 

    - 函數式編程

有一個列表: list1 = [3,5,-4,-1,0,-2,-6],須要按照每一個元素的絕對值升序排序,如何作?

# 使用lambda的方式
>>>
>>> list1
[3, 5, -4, -1, 0, -2, -6]
>>>
>>> sorted(list1, key=lambda i : abs(i))
[0, -1, -2, 3, -4, 5, -6]
>>>

# 不使用lambda的方式
>>>
>>> def foo(x):
...     return abs(x)
...
>>> sorted(list1, key=foo)
[0, -1, -2, 3, -4, 5, -6]
>>>

如何把一個字典按照value進行排序?

>>>
>>> dic = {'a': 9, 'c': 3, 'b': 1, 'd': 7, 'f': 12}
>>> dic
{'a': 9, 'f': 12, 'c': 3, 'd': 7, 'b': 1}
>>>
>>> from collections import Iterable
>>> isinstance(dic.items(),Iterable)
True
>>>
>>> dic.items()
dict_items([('a', 9), ('f', 12), ('c', 3), ('d', 7), ('b', 1)])
>>>
>>> sorted(dic.items(), key=lambda x:x[1])
[('b', 1), ('c', 3), ('d', 7), ('a', 9), ('f', 12)]
>>>

  - 閉包

# 不用lambda的方式
>>>
>>> def my_add(n):
...     def wrapper(x):
...         return x+n
...     return wrapper
...
>>> add_3 = my_add(3)
>>> add_3(7)
10
>>>

# 使用lambda的方式
>>>
>>> def my_add(n):
...     return lambda x:x+n
...
>>> add_3 = my_add(3)
>>> add_3(7)
10
>>>

  

方法和函數的區別

#!/usr/bin/python3

from types import MethodType,FunctionType

class Foo(object):
    def __init__(self):
        pass
    def func(self):
        print('func...')

obj = Foo()
print(obj.func)  # 自動傳遞 self 
# <bound method Foo.func of <__main__.Foo object at 0x7f86121505f8>>
print(Foo.func)
# <function Foo.func at 0x7f861214e488>

print(isinstance(obj.func,MethodType))         # True
print(isinstance(obj.func,FunctionType))       # False

print(isinstance(Foo.func,MethodType))         # False
print(isinstance(Foo.func,FunctionType))       # True

 

時間戳轉換成年月日時分秒

>>> from datetime import datetime
>>> ts=1531123200
>>> date_str = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
>>> date_str
'2018-07-09 16:00:00'
>>> 
In [11]: import time                                                                                                                                                                                                                      

In [12]: ts = int(time.time())                                                                                                                                                                                                            

In [13]: ts                                                                                                                                                                                                                               
Out[13]: 1559549982

In [14]: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))                                                                                                                                                                           
Out[14]: '2019-06-03 16:19:42'

In [15]: 

年月日時分秒轉換成時間戳

>>> date_str
'2018-07-09 16:00:00'
>>> 
>>> date_struct=time.strptime(date_str,'%Y-%m-%d %H:%M:%S')
>>> date_struct
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=9, tm_hour=16, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=190, tm_isdst=-1)
>>> 
>>> int(time.mktime(date_struct))
1531123200
>>> 
In [16]: d                                                                                                                                                                                                                                
Out[16]: '2019-06-03 16:19:42'

In [17]: time.mktime(time.strptime(d, "%Y-%m-%d %H:%M:%S"))                                                                                                                                                                               
Out[17]: 1559549982.0

In [18]: 

獲取當前月初和月末時間戳

>>> import time
>>> import datetime
>>> 
>>> start_st = datetime.datetime.now()
>>> start_st
datetime.datetime(2018, 7, 17, 16, 45, 39, 95228)
>>> startts = int(time.mktime((start_st.year, start_st.month-1, 1, 0, 0, 0, 0, 0, -1)))
>>> startts    # 當前月初時間戳
1527782400
>>> 
>>> stopts = int(time.mktime((start_st.year, start_st.month, 1, 0, 0, 0, 0, 0, -1))) - 1
>>> stopts
1530374399     # 當前月末時間戳
>>>

IP地址轉換成數字,從而進行比較,適用於地址庫

>>> ips = ['8.8.8.8','202.106.0.20']
>>> 
>>> map(lambda ip:sum([256**j*int(i) for j,i in enumerate(ip.split('.')[::-1])]), ips)
[134744072, 3395944468]
>>> 

 

使用yield逐行讀取多個文件併合並

#!/usr/bin/python2.7

def get_line_by_yield():
    with open('total.txt','r') as rf_total, open('extra.txt','r') as rf_extra:
        for line in rf_total:
            extra = rf_extra.readline()
            lst = line.strip().split() + extra.strip().split()
            yield lst

with open('new_total.txt','w') as wf:
    for lst in get_line_by_yield():
        wf.write('%s\n' % ''.join(map(lambda i: str(i).rjust(20), lst)))

逐行讀取

def read_line(path):
    with open(path,'r') as rf:
        for line in rf:
            yield line

  

 

檢查進程是否 running

In [16]: import signal

In [17]: from os import kill

In [18]: kill(17335, 0)    # 17335 是進程ID,第二個參數傳0/signal.SIG_DFL 返回值如果None則表示正在運行

In [19]: kill(17335, 15)   # 給進程傳遞15/signal.SIGTERM,即終止該進程

In [20]: kill(17335, 0)    # 再次檢查發現該進程已經再也不running,則raise一個OSError
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-20-cbb7c9624124> in <module>()
----> 1 kill(17335, 0)

OSError: [Errno 3] No such process

In [21]: 
  1 In [12]: import signal
  2 
  3 In [13]: signal.SIGKILL
  4 Out[13]: 9
  5 
  6 In [14]: signal.SIGTERM
  7 Out[14]: 15
  8 
  9 In [15]: signal.__dict__.items()
 10 Out[15]: 
 11 [('SIGHUP', 1),
 12  ('SIG_DFL', 0),
 13  ('SIGSYS', 31),
 14  ('SIGQUIT', 3),
 15  ('SIGUSR1', 10),
 16  ('SIGFPE', 8),
 17  ('SIGPWR', 30),
 18  ('SIGTSTP', 20),
 19  ('ITIMER_REAL', 0L),
 20  ('SIGCHLD', 17),
 21  ('SIGCONT', 18),
 22  ('SIGIOT', 6),
 23  ('SIGBUS', 7),
 24  ('SIGXCPU', 24),
 25  ('SIGPROF', 27),
 26  ('SIGCLD', 17),
 27  ('SIGUSR2', 12),
 28  ('default_int_handler', <function signal.default_int_handler>),
 29  ('pause', <function signal.pause>),
 30  ('SIGKILL', 9),
 31  ('NSIG', 65),
 32  ('SIGTRAP', 5),
 33  ('SIGINT', 2),
 34  ('SIGIO', 29),
 35  ('__package__', None),
 36  ('getsignal', <function signal.getsignal>),
 37  ('SIGILL', 4),
 38  ('SIGPOLL', 29),
 39  ('SIGABRT', 6),
 40  ('SIGALRM', 14),
 41  ('__doc__',
 42   'This module provides mechanisms to use signal handlers in Python.\n\nFunctions:\n\nalarm() -- cause SIGALRM after a specified time [Unix only]\nsetitimer() -- cause a signal (described below) after a specified\n               float time and the timer may restart then [Unix only]\ngetitimer() -- get current value of timer [Unix only]\nsignal() -- set the action for a given signal\ngetsignal() -- get the signal action for a given signal\npause() -- wait until a signal arrives [Unix only]\ndefault_int_handler() -- default SIGINT handler\n\nsignal constants:\nSIG_DFL -- used to refer to the system default handler\nSIG_IGN -- used to ignore the signal\nNSIG -- number of defined signals\nSIGINT, SIGTERM, etc. -- signal numbers\n\nitimer constants:\nITIMER_REAL -- decrements in real time, and delivers SIGALRM upon\n               expiration\nITIMER_VIRTUAL -- decrements only when the process is executing,\n               and delivers SIGVTALRM upon expiration\nITIMER_PROF -- decrements both when the process is executing and\n               when the system is executing on behalf of the process.\n               Coupled with ITIMER_VIRTUAL, this timer is usually\n               used to profile the time spent by the application\n               in user and kernel space. SIGPROF is delivered upon\n               expiration.\n\n\n*** IMPORTANT NOTICE ***\nA signal handler function is called with two arguments:\nthe first is the signal number, the second is the interrupted stack frame.'),
 43  ('SIG_IGN', 1),
 44  ('getitimer', <function signal.getitimer>),
 45  ('SIGURG', 23),
 46  ('SIGPIPE', 13),
 47  ('SIGWINCH', 28),
 48  ('__name__', 'signal'),
 49  ('SIGTERM', 15),
 50  ('SIGVTALRM', 26),
 51  ('ITIMER_PROF', 2L),
 52  ('SIGRTMIN', 34),
 53  ('SIGRTMAX', 64),
 54  ('ITIMER_VIRTUAL', 1L),
 55  ('set_wakeup_fd', <function signal.set_wakeup_fd>),
 56  ('setitimer', <function signal.setitimer>),
 57  ('signal', <function signal.signal>),
 58  ('SIGSEGV', 11),
 59  ('siginterrupt', <function signal.siginterrupt>),
 60  ('SIGXFSZ', 25),
 61  ('SIGTTIN', 21),
 62  ('SIGSTOP', 19),
 63  ('ItimerError', signal.ItimerError),
 64  ('SIGTTOU', 22),
 65  ('alarm', <function signal.alarm>)]
 66 
 67 In [16]: dict((k, v) for v, k in reversed(sorted(signal.__dict__.items()))
 68     ...:     if v.startswith('SIG') and not v.startswith('SIG_'))
 69 Out[16]: 
 70 {1: 'SIGHUP',
 71  2: 'SIGINT',
 72  3: 'SIGQUIT',
 73  4: 'SIGILL',
 74  5: 'SIGTRAP',
 75  6: 'SIGABRT',
 76  7: 'SIGBUS',
 77  8: 'SIGFPE',
 78  9: 'SIGKILL',
 79  10: 'SIGUSR1',
 80  11: 'SIGSEGV',
 81  12: 'SIGUSR2',
 82  13: 'SIGPIPE',
 83  14: 'SIGALRM',
 84  15: 'SIGTERM',
 85  17: 'SIGCHLD',
 86  18: 'SIGCONT',
 87  19: 'SIGSTOP',
 88  20: 'SIGTSTP',
 89  21: 'SIGTTIN',
 90  22: 'SIGTTOU',
 91  23: 'SIGURG',
 92  24: 'SIGXCPU',
 93  25: 'SIGXFSZ',
 94  26: 'SIGVTALRM',
 95  27: 'SIGPROF',
 96  28: 'SIGWINCH',
 97  29: 'SIGIO',
 98  30: 'SIGPWR',
 99  31: 'SIGSYS',
100  34: 'SIGRTMIN',
101  64: 'SIGRTMAX'}
102 
103 In [17]: 
signal數字和代號的映射關係
1 def check_if_process_is_alive(self):
2         try:
3             kill(self.current_pid, 0)
4             kill(self.parent_pid, 0)
5         except:
6             # do something...
7             exit(0)
應用

  參考:https://stackoverflow.com/questions/13399734/how-to-find-out-when-subprocess-has-terminated-after-using-os-kill

 

多指標排序問題

In [26]: lst = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]

In [27]: import operator

In [28]: sorted(lst, key=operator.itemgetter(1))
Out[28]: [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]

In [29]: sorted(lst, key=operator.itemgetter(1,2))  # 先根據第二個域排序,而後再根據第三個域排序
Out[29]: [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]

In [30]: 

  

兩個純數字列表元素個數相等,按序相加求和,獲得一個新的列表

length = len(lst1)
lst = reduce(lambda x,y:[x[i]+y[i] for i in range(length)], [lst1,lst2], [0]*length)

或者直接使用numpy.array

補充reduce+lambda合併多個列表

In [15]: lst = [[1,2,3],['a','c'],['hello','world'],[2,2,2,111]]

In [16]: reduce(lambda x,y: x+y, lst)
Out[16]: [1, 2, 3, 'a', 'c', 'hello', 'world', 2, 2, 2, 111]

In [17]: 

擴展現例1:

lst= [[{u'timestamp': 1545214320, u'value': 222842128},
  {u'timestamp': 1545214380, u'value': 224080288},
  {u'timestamp': 1545214440, u'value': 253812496},
  {u'timestamp': 1545214500, u'value': 295170240},
  {u'timestamp': 1545214560, u'value': 221196224},
  {u'timestamp': 1545214620, u'value': 252992096}],
 [{u'timestamp': 1545214320, u'value': 228121600},
  {u'timestamp': 1545214380, u'value': 225682656},
  {u'timestamp': 1545214440, u'value': 256428064},
  {u'timestamp': 1545214500, u'value': 292691424},
  {u'timestamp': 1545214560, u'value': 241462336},
  {u'timestamp': 1545214620, u'value': 250864528}],
 [{u'timestamp': 1545214320, u'value': 232334304},
  {u'timestamp': 1545214380, u'value': 230452032},
  {u'timestamp': 1545214440, u'value': 246094880},
  {u'timestamp': 1545214500, u'value': 260281088},
  {u'timestamp': 1545214560, u'value': 233277120},
  {u'timestamp': 1545214620, u'value': 258726192}]]

# 要求:把上述列表合併
# 方法一:使用Python內置函數
In [83]: reduce(lambda x,y:[ { 'timestamp':x[i]['timestamp'], 'value':x[i]['value']+y[i]['value'] } for i in range(6) ], a)
Out[83]: 
[{'timestamp': 1545214320, 'value': 683298032},
 {'timestamp': 1545214380, 'value': 680214976},
 {'timestamp': 1545214440, 'value': 756335440},
 {'timestamp': 1545214500, 'value': 848142752},
 {'timestamp': 1545214560, 'value': 695935680},
 {'timestamp': 1545214620, 'value': 762582816}]

In [84]:

# 方法二:笨辦法
 In [87]: b = a.pop(0)

In [88]: 

In [88]: for i in a:
    ...:     for idx in range(len(i)):
    ...:         b[idx]['value'] += i[idx]['value']
    ...:         

In [89]: b
Out[89]: 
[{u'timestamp': 1545214320, u'value': 683298032},
 {u'timestamp': 1545214380, u'value': 680214976},
 {u'timestamp': 1545214440, u'value': 756335440},
 {u'timestamp': 1545214500, u'value': 848142752},
 {u'timestamp': 1545214560, u'value': 695935680},
 {u'timestamp': 1545214620, u'value': 762582816}]

In [90]: 

擴展現例2:

In [48]: a
Out[48]: 
[{'A078102C949EC2AB': [1, 2, 3, 4]},
 {'457D37015E77700E': [2, 2, 2, 2]},
 {'5095060C4552175D': [3, 3, 3, 3]}]

In [49]: reduce(lambda x,y: dict(x.items()+y.items()), a)
Out[49]: 
{'457D37015E77700E': [2, 2, 2, 2],
 '5095060C4552175D': [3, 3, 3, 3],
 'A078102C949EC2AB': [1, 2, 3, 4]}

In [50]: 

 

awk指定字段求和

awk -F '=' '{count+=$4} END{print count}' file.log

 

找出在列表1中但不在列表2中的元素

list(set(lst1).difference(set(lst2)))

  

解析url,把字段轉換成字典

# 方法一
In [5]: url = 'index?name=standby&age=18&city=beijing'

In [6]: parameter = url.split('?')[1]

In [7]: parameter
Out[7]: 'name=standby&age=18&city=beijing'

In [8]: dict(map(lambda x:x.split('='),parameter.split('&')))
Out[8]: {'age': '18', 'city': 'beijing', 'name': 'standby'}

In [9]: 


# 方法二
In [9]: import urlparse

In [10]: query = urlparse.urlparse(url).query

In [11]: query
Out[11]: 'name=standby&age=18&city=beijing'

In [12]: dict([(k, v[0]) for k, v in urlparse.parse_qs(query).items()])
Out[12]: {'age': '18', 'city': 'beijing', 'name': 'standby'}

In [13]: 

 

fromkeys使用的陷阱

In [1]: a = dict.fromkeys(['k1','k2','k3'],{})

In [2]: a
Out[2]: {'k1': {}, 'k2': {}, 'k3': {}}

In [3]: a['k1']['2018-10-10'] = 'hi'

In [4]: a
Out[4]: 
{'k1': {'2018-10-10': 'hi'},
 'k2': {'2018-10-10': 'hi'},
 'k3': {'2018-10-10': 'hi'}}

In [5]: 

In [5]: a = dict.fromkeys(['k1','k2','k3'],[])

In [6]: a['k1'].append(999)

In [7]: a
Out[7]: {'k1': [999], 'k2': [999], 'k3': [999]}

In [8]: 

In [8]: a = dict.fromkeys(['k1','k2','k3'],0)

In [9]: a['k1'] += 9

In [10]: a
Out[10]: {'k1': 9, 'k2': 0, 'k3': 0}

In [11]: 

  

dateutil庫解析時間對象

In [76]: import datetime                                                                                                                                                                          

In [77]: datetime.datetime.strptime('2019-04-10','%Y-%m-%d')                                                                                                                                      
Out[77]: datetime.datetime(2019, 4, 10, 0, 0)

In [78]: import dateutil                                                                                                                                                                          

In [79]: dateutil.parser.parse('2019-04-10')                                                                                                                                                      
Out[79]: datetime.datetime(2019, 4, 10, 0, 0)

In [80]: dateutil.parser.parse('2019/04/10')                                                                                                                                                      
Out[80]: datetime.datetime(2019, 4, 10, 0, 0)

In [81]: dateutil.parser.parse('04/10/2019')                                                                                                                                                      
Out[81]: datetime.datetime(2019, 4, 10, 0, 0)

In [82]: dateutil.parser.parse('2019-Apr-10')                                                                                                                                                     
Out[82]: datetime.datetime(2019, 4, 10, 0, 0)

In [83]: 

 

合併多個字典

# Python2.7
# 這種方式對資源的一種浪費
# 注意這種方式在Python3中會報錯:TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'

In [7]: lst
Out[7]: 
[{'k1': [1, 1, 1, 1, 1, 1]},
 {'k3': [3, 3, 3, 4, 4, 4]},
 {'k5': [5, 5, 5, 6, 6, 6]}]

In [8]: reduce(lambda x,y: dict(x.items()+y.items()), lst)
Out[8]: {'k1': [1, 1, 1, 1, 1, 1], 'k3': [3, 3, 3, 4, 4, 4], 'k5': [5, 5, 5, 6, 6, 6]}

In [9]: 


# Python3.6
In [67]: lst                                                                                                                                                                                                                              
Out[67]: 
[{'k1': [1, 1, 1, 1, 1, 1]},
 {'k3': [3, 3, 3, 4, 4, 4]},
 {'k5': [5, 5, 5, 6, 6, 6]}]

In [68]: reduce(lambda x,y: {**x,**y}, lst)                                                                                                                                                                                               
Out[68]: {'k1': [1, 1, 1, 1, 1, 1], 'k3': [3, 3, 3, 4, 4, 4], 'k5': [5, 5, 5, 6, 6, 6]}

In [69]:


# 另外補充兩種兼容Py2和Py3的方法:
# 1. 使用字典的構造函數
reduce(lambda x,y: dict(x, **y), lst)
# 2. 笨辦法
{k: v for d in lst for k, v in d.items()}

  

zip的反操做/unzip

In [2]: lst                                                                                                                                                                                                                               
Out[2]: 
[[1560239100, 16],
 [1560239400, 11],
 [1560239700, 14],
 [1560240000, 18],
 [1560240300, 18],
 [1560240600, 12],
 [1560240900, 19],
 [1560241200, 13],
 [1560241500, 16],
 [1560241800, 16]]

In [3]: tss,vals = [ list(tpe) for tpe in zip(*[ i for i in lst ]) ]                                                                                                                                                                      

In [4]: tss                                                                                                                                                                                                                               
Out[4]: 
[1560239100,
 1560239400,
 1560239700,
 1560240000,
 1560240300,
 1560240600,
 1560240900,
 1560241200,
 1560241500,
 1560241800]

In [5]: vals                                                                                                                                                                                                                              
Out[5]: [16, 11, 14, 18, 18, 12, 19, 13, 16, 16]

In [6]: 
相關文章
相關標籤/搜索