You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Stripe API对比客户参数并去除重复客户?

移除Stripe客户列表中的重复项

要实现对比客户参数并移除重复项,核心是在添加客户到filtered_customers前,检查该客户是否已经存在于列表中。以下是两种常见实现方式:

方式一:按客户ID去重(推荐,ID是Stripe客户的唯一标识)

利用集合快速查询的特性,跟踪已添加的客户ID,避免重复:

import stripe

stripe.api_key = "sk_test_51OQA44ESuCeG...hZce"

customers = stripe.Customer.list(limit=100) 
filtered_customers = []
existing_customer_ids = set()  # 用于存储已添加的客户ID

for customer in customers: 
    customer_id = customer["id"]
    if customer_id is not None and customer_id not in existing_customer_ids:
        filtered_customers.append(customer)
        existing_customer_ids.add(customer_id)

方式二:按其他参数去重(如邮箱、手机号)

如果需要基于邮箱、手机号等业务字段去重,只需修改跟踪的字段和判断逻辑:

import stripe

stripe.api_key = "sk_test_51OQA44ESuCeG...hZce"

customers = stripe.Customer.list(limit=100) 
filtered_customers = []
existing_emails = set()  # 改为跟踪邮箱

for customer in customers: 
    customer_email = customer.get("email")
    if customer_email and customer_email not in existing_emails:
        filtered_customers.append(customer)
        existing_emails.add(customer_email)

说明

  • 使用集合而非列表做存在性检查,是因为集合的in操作时间复杂度为O(1),远快于列表的O(n),在客户数量较多时效率更高。
  • 若需要同时对比多个参数(如邮箱+手机号),可以将多个字段组合成元组存入集合,例如existing_keys = set(),然后用key = (customer.get("email"), customer.get("phone"))作为判断依据。

内容的提问来源于stack exchange,提问作者RadFresh Ant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 18:32:11