python数据分析-第三章

  • 二分收索
import bisect 
c = [1, 2, 2,2,3,4,5]
bisect.bisect(c, 2)
  • enumerate
    遍历一个序列,同时需要序号
some_list = ['foo', 'bar', 'baz']
mapping = {
    
    }
for i, v in enumerate(some_list):
	mapping[v] = i

out: {
    
    'foo': 0, 'bar': 1, 'baz': 2}
  • zip
    将列表,元组或其他序列的元素配对,新建一个元组构成列表
seq1 = ['foo', 'bar', 'baz']
seq2 = ['one', 'two', 'three']
zipped = zip(seq1, seq2)
print(list(zipped))
[out] [('foo', 'one'), ('bar', 'two'), ('baz', 'three')]
# 从序列生成字典
for key, value in zip(seq1, seq2):
	mapping[key] = value
  • 字典默认值
    方法一:setdefault
words = ['foo', 'bar', 'baz']
by_letter = {
    
    }
for word in words:
	letter = word[0]
	by_letter.setdefault(letter, []).append(word)
print(by_letter)
{
    
    'f': ['foo'], 'b': ['bar', 'baz']}

方法二: defaultdict

from collections import defaultdict
by_letter = defaultdict(list)
for word in words:
    by_letter[word[0]].append(word)
defaultdict(<class 'list'>, {
    
    'f': ['foo'], 'b': ['bar', 'baz']})
  • 推导式
# 公式
# [expr for val in collections if condition]

words = ['foo', 'bar', 'baz']
print([x.upper() for x in words if x[0] == 'b'])
['BAR', 'BAZ']

# 嵌套
ls = [x for word in words for x in word if word[0] == 'b']
print(ls)
['b', 'a', 'r', 'b', 'a', 'z']
  • lambda
  1. 快速构造函数对象
string = ['foo', 'card', 'b', 'apple', 'interme', 'get', 'ababab']
string.sort(key=lambda x: len(set(list(x))))
print(string)
['b', 'foo', 'ababab', 'get', 'card', 'apple', 'interme']
  1. 部分参数lamde
some_dict = {
    
    'a':1, 'b':2, 'c':3}
def add_num(x, y):
	return x + y

add_five = lambda y: add_num(5, y)
print([add_five(x) for x in some_dict.values()])
[6, 7, 8]

# 采用partial函数实现
from functools import partial
add_six = partial(add_num, 6)
print([add_six(x) for x in some_dict.values()])
  • 生成器
  1. iter生成
some_dict = {
    
    'a':1, 'b':2, 'c':3}
print(list(iter(some_dict)))
['a', 'b', 'c']
  1. yield 迭代函数
# yield相当于进化版return
def squares(n = 10):
    print("squars")
    for i in range(n):
        yield i ** 2

gen = squares()
print('suquars() do not run')
for x in gen:
    print(x, end=' ')

out:
suquars() do not run
squars
0 1 4 9 16 25 36 49 64 81
  • groupby函数itertools
import itertools
first_letter = lambda x: x[0]
string = ['foo', 'fxx', 'bet', 'apple', 'bat', 'ababa']
string.sort(key=first_letter)
for letter, names in itertools.groupby(string, first_letter):
    print(letter, list(names))
a ['apple', 'ababa']
b ['bet', 'bat']
f ['foo', 'fxx']

猜你喜欢

转载自blog.csdn.net/CPriLuke/article/details/106745032