"""固定顺序输出；集合仅用于成员判断，不依赖迭代顺序。"""
def main():
    events = [("/orders", 500), ("/health", 200), ("/orders", 200)]
    paths = [path for path, _ in events]
    unique = set(paths)
    counts = {}
    for path in paths:
        counts[path] = counts.get(path, 0) + 1
    assert counts == {"/orders": 2, "/health": 1}
    assert "/health" in unique
    # tuple 的不可变性只约束它保存的引用。
    record = ("/orders", [])
    record[1].append(500)
    assert record == ("/orders", [500])
    try:
        hash(record)
    except TypeError:
        print("unhashable tuple member")
    bad = ["/orders", "/orders", "/health"]
    # 遍历时原地删除会跳过元素。
    for path in bad:
        if path == "/orders":
            bad.remove(path)
    good = [path for path in paths if path != "/orders"]
    assert bad == ["/orders", "/health"] and good == ["/health"]
    # 比较确定的搜索工作，不把一次计时当作普遍性能结论。
    inputs = list(range(100)) + list(range(100))
    slow = []
    comparisons = 0
    for value in inputs:
        exists = False
        for known in slow:
            comparisons += 1
            if known == value:
                exists = True
                break
        if not exists:
            slow.append(value)
    fast = list(dict.fromkeys(inputs))  # 保留首次出现的顺序。
    assert slow == fast == list(range(100))
    assert comparisons == 10000
    print("dedup input/keys/list comparisons:", len(inputs), len(fast), comparisons)
    print("counts:", counts)
    print("filter:", bad, good)


if __name__ == "__main__":
    main()
