新内容记录:

1.js替换输入的中文逗号为英文逗号
onkeyup='javascript:this.value=this.value.replace(",",",");

2.快速的ip查询库:ip2Region

https://gitee.com/lionsoul/ip2region

 

3.Counter(计数器):用于追踪值的出现次数

#统计词频
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怎么实现:

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

显然代码更加简单了,也更容易读和维护了

4.使用sorted函数

用 operator 函数进行多级排序 

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)]  

 

 


 

posted @ 2019-06-12 09:54  武老师  阅读(202)  评论(0编辑  收藏  举报