第1章 数据结构和算法(2)

1、从序列中移除重复项且保持元素间顺序不变

# coding=utf-8
def dedupe(items):
    seen = set()
    for item in items:
        if item not in seen:
            yield item  # 可以使函数可迭代
            seen.add(item)


a = [1, 5, 2, 1, 9, 1, 5, 10]
print(list(dedupe(a)))

>>[1, 5, 2, 9, 10]

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

collections模块中的Counter类正是为此类问题所设计的。它甚至有一个非常方便的most_common()方法可以直接告诉我们答案。

# coding=utf-8
from collections import Counter
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'
]
word_counts = Counter(words)
top_three = word_counts.most_common(3)
print(top_three)

3、通过公共键对字典列表排序

利用operator模块中的itemgetter函数对这类结构进行排序是非常简单的实现对公共键对字典列表排序。

# coding=utf-8
from operator import itemgetter

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

rows_by_fname = sorted(rows, key=itemgetter('fname'))
rows_by_uid = sorted(rows, key=itemgetter('uid'))

4、将名称映射到序列的元素中

相比普通的元组,collections.namedtuple()(具名元组)只增加了极小的开销就提供了这些便利。实际上collections.namedtuple()是一个工厂方法,它返回的是Python中标准元组类型的子类。我们提供给它一个类型名称以及相应的字段,它就返回一个可实例化的类、为你已经定义好的字段传入值等。

# coding=utf-8
from collections import namedtuple

Stock = namedtuple('Stock', ['name', 'shares', 'price'])


def compute_cost(records):
    total = 0.0
    for rec in records:
        s = Stock(*rec)
        total += s.shares * s.price
    return total


records = [['apple', 0.4, 5], ['banana', 0.6, 3]]
result = compute_cost(records)

5、将多个映射合并为单个映射

一种简单的方法是利用collections模块中的ChainMap类来解决这个问题。

# coding=utf-8
from collections import ChainMap

a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}
c = ChainMap(a,b)
print(c['x']) # Outputs 1 (from a)
print(c['y']) # Outputs 2 (from b)
print(c['z']) # Outputs 3 (from a)

如果有重复的键,那么这里会采用第一个映射中所对应的值。因此,例子中的c[‘z’]总是引用字典a中的值,而不是字典b中的值。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容