python Cookbook

1.10 移除序列中的重复项

 

#从序列中移除重复项且保持元素间顺序
#可哈希时,通过集合set和生成器yield
def dedupe(items):
    seen=set()#集合
    for item in items:
        if item not in seen:
            yield item
            seen.add(item)
#    return seen

# a=[-1,-25,-3,4,8,8,8,7,9]
# print(list(dedupe(a)))#元组tuple()转化为列表list[]

#不可哈希

def deupe2(items,key=None):
    seen=set()
    for item in items:
        val=item if key is None else key(item)#key指定一个函数用来转化hash对象
        if val not in seen:
            yield item
            seen.add(val)
a=[{'x':1,'y':2},{'x':1,'y':3},{'x':1,'y':2},{'x':2,'y':4}]
print(list(deupe2(a,key=lambda d:(d['x'],d['y']))))
print(list(deupe2(a,key=lambda d:(d['x']))))
#构建集合去除重复项,但顺序会打乱
b=[1,5,2,1,9,5,10]
print(set(b))

1.11对切片索引命名

#切片 slice()函数会创建一个切片对象
item=[0,1,2,3,4,5,6]
a=slice(2,4)
print(item[a])
print(item[2:4])
item[a]=[10,11]
print(item)
# del item[a]
# print(item)

a=slice(5,10,2)
print(a.start)
print(a.stop)
print(a.step)
#indices(size)将切片映射到特定大小的序列上
s='helloworld'
print(a.indices(len(s)))
for i in range(*a.indices(len(s))):
    print(s[i])

1.2找出序列中出现次数最多的元素

#找出序列中出现次数最多的元素
#list[],tuple(),dictionary{}
from collections import Counter
word=['i','am','not','happy','because','your','stupid','annoying','me','i','not','i']
word_count=Counter(word)
top_three=word_count.most_common(3)
print(top_three)
#counter是一个字典,在元素和次数之间做了映射
print(word_count['am'])
#增加
moreword=['why','your','not','looking','in','my','eye']
for word in moreword:
    word_count[word]+=1
print(word_count['your'])
#updata增加
word_count.update(moreword)

#1.13通过公共键对字典列表排序operator模块
rows=[{'fname':'Brain','lname':'Jones','uid':1003},
      {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
      {'fname': 'Jone', 'lname': 'Cleese', 'uid': 1001},
      {'fname': 'Big', 'lname': 'Jones', 'uid': 1004},]
from operator import itemgetter
row_byfname=sorted(rows,key=itemgetter('fname'))
print(row_byfname)
row_byuid=sorted(rows,key=itemgetter('uid'))
print(row_byuid)
#itemgetter还可以接受多个值
row_bylname=sorted(rows,key=itemgetter('lname','uid'))
print(row_bylname)
#itemgetter同样支持min.max
min_byuid=min(rows,key=itemgetter('uid'))
print(min_byuid)

 

posted on 2019-05-23 16:15  yaqiong1112  阅读(251)  评论(0编辑  收藏  举报