新內容記錄:

1.js替換輸入的中文逗號爲英文逗號
onkeyup='javascript:this.value=this.value.replace(",",",");

2.快速的ip查詢庫:ip2Regionjavascript

https://gitee.com/lionsoul/ip2region

 

3.Counter(計數器):用於追蹤值的出現次數java

#統計詞頻
colors = ['red', 'blue', 'red', 'green', 'blue', 'blue']
result = {}
for color in colors:
    if result.get(color)==None:
        result[color]=1
    else:
        result[color]+=1
print (result)
#{'red': 2, 'blue': 3, 'green': 1}

下面咱們看用Counter怎麼實現:git

from collections import Counter
colors = ['red', 'blue', 'red', 'green', 'blue', 'blue']
c = Counter(colors)
print (dict(c))

顯然代碼更加簡單了,也更容易讀和維護了函數

4.使用sorted函數this

用 operator 函數進行多級排序 spa

from operator import itemgetter, attrgetter 
students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10),]
sorted(students, key=itemgetter(2))  
sorted(students, key=itemgetter(1,2))  # sort by grade then by age 
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]  
相關文章
相關標籤/搜索