python cookbook学习笔记02

python cookbook一些知识点

一、python数据结构与算法

  1. 字典取最大值
prices = {
    'ACME': 45.23,
    'AAPL': 612.78,
    'IBM': 205.55,
    'HPQ': 37.20,
    'FB': 10.75
}
print(min(zip(prices.values(), prices.keys())))
print(sorted(zip(prices.values(), prices.keys())))
print(max(prices, key=lambda k: prices[k]))
  1. 字典合并
a = {
    'x': 1,
    'y': 2,
    'z': 3
}
b = {
    'w': 10,
    'x': 11,
    'y': 2
}
c = a.items() & b.items()
c1 = a.items() | b.items()
c2 = a.items() ^ b.items()
print(c)
print(c1)
print(c2)
  1. 切片用slice的好处
str1 = "1223334445jsjofijwflsjfijejfljsljslfjsoijfsl"
l_str = list(str1)
ss = slice(10, 23)
print(l_str[ss])
l_str[ss] = list("0" * 12)
print(l_str)
str1 = "1223334445jsjofijwflsjfijejfljsljslfjsoijfsl"
l_str = list(str1)
ss = slice(10, 23)
# print(l_str[ss])
l_str[ss] = list("0" * 13)
print(l_str)
print(l_str[ss])
ss = slice(5, 20, 3)
print(ss.start)
print(ss.step)
print(ss.stop)
  1. 计算出现词频
words = [
    'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
    'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
    'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
    'my', 'eyes', "you're", 'under'
]
from collections import Counter
w = Counter(words)
w_three = w.most_common(3)
print(w_three)
print(w["look"])

[('eyes', 8), ('the', 5), ('look', 4), ('into', 3)]
4
  1. 字典的排序
rows = [
    {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
    {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
    {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
    {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
]
print(sorted(rows, key=lambda s: s["uid"]))
from operator import itemgetter
print(sorted(rows, key=itemgetter("uid")))
max(rows, key=itemgetter('uid'))

  1. groupby
rows = [
    {'address': '5412 N CLARK', 'date': '07/01/2012'},
    {'address': '5148 N CLARK', 'date': '07/04/2012'},
    {'address': '5800 E 58TH', 'date': '07/02/2012'},
    {'address': '2122 N CLARK', 'date': '07/03/2012'},
    {'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
    {'address': '1060 W ADDISON', 'date': '07/02/2012'},
    {'address': '4801 N BROADWAY', 'date': '07/01/2012'},
    {'address': '1039 W GRANVILLE', 'date': '07/04/2012'}
]
from operator import itemgetter
from itertools import groupby
rows.sort(key=itemgetter("date"))
for date, items in groupby(rows, key=itemgetter('date')):
    print(date)
    # print(items)
    for i in items:
        print("  ", i)

打印结果:
07/01/2012
   {'address': '5412 N CLARK', 'date': '07/01/2012'}
   {'address': '4801 N BROADWAY', 'date': '07/01/2012'}
07/02/2012
   {'address': '5800 E 58TH', 'date': '07/02/2012'}
   {'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'}
   {'address': '1060 W ADDISON', 'date': '07/02/2012'}
07/03/2012
   {'address': '2122 N CLARK', 'date': '07/03/2012'}
07/04/2012
   {'address': '5148 N CLARK', 'date': '07/04/2012'}
   {'address': '1039 W GRANVILLE', 'date': '07/04/2012'}
  1. 列表递推式,filter
mylist = [1, 4, -5, 10, -7, 2, 3, -1]
列表递推占内存
print([n for n in mylist if n % 2 == 0])
生成器
print((n for n in mylist if n % 2 == 0))
def fn(a):
    if a > 0:
        return a
ret = filter(fn, mylist)
print(list(ret))
print([n if n < 0 else 0 for n in mylist])
[0, 0, -5, 0, -7, 0, 0, -1]
  1. 字典递推式
prices = {
    'ACME': 45.23,
    'AAPL': 612.78,
    'IBM': 205.55,
    'HPQ': 37.20,
    'FB': 10.75
}
内存大速度快
d = {k: v for k, v in prices.items() if v > 200}
内存小速度慢
p1 = dict((key, value) for key, value in prices.items() if value > 200)
print(d)
print(p1)
  1. 两个字典中完成操作
a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}

print(a.items() & b.items())
print(a.items() | b.items())
print(a.items() ^ b.items())

from collections import ChainMap

c = ChainMap(a, b)
print(c)
print(list(c.keys()))
print(list(c.values()))
结果:
['z', 'y', 'x']
[3, 2, 1]

你可能感兴趣的:(python cookbook学习笔记02)