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

如何生成点分隔域名字符串对应的全部子域列表?

实现代码(Python)

# 输入域名列表
domains = ["abc.pqr.com", "pqr.yum.abc.com"]
all_subdomains = []

for domain in domains:
    # 按点分割域名片段
    domain_parts = domain.split(".")
    # 从最少2段后缀开始,逐步拼接完整子域
    for part_count in range(2, len(domain_parts) + 1):
        current_sub = ".".join(domain_parts[-part_count:])
        all_subdomains.append(current_sub)

# 可选:如果需要去重且保留插入顺序,用下方代码替换
# all_subdomains = list(dict.fromkeys(all_subdomains))

# 输出结果
print(all_subdomains)

运行输出

['pqr.com', 'abc.pqr.com', 'abc.com', 'yum.abc.com', 'pqr.yum.abc.com']

如果需要和你给出的示例输出顺序完全一致,可在遍历完成后按域名层级调整排序规则即可。

内容的提问来源于stack exchange,提问作者Jeet Patel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 00:09:00