python – 使用掩码生成所有可能的数字
发布时间:2020-12-20 12:22:16 所属栏目:Python 来源:网络整理
导读:假设我有一个字符串: a = "123**7*9" 我需要生成所有可能的组合: 12300709...12399799 如何用Python做到这一点? 解决方法 您可以使用 itertools.product 和字符串格式: from itertools import product strs = "123**7*9" c = strs.count("*") #count the
假设我有一个字符串:
a = "123**7*9" 我需要生成所有可能的组合: 12300709...12399799 如何用Python做到这一点? 解决方法
您可以使用
itertools.product 和字符串格式:
>>> from itertools import product >>> strs = "123**7*9" >>> c = strs.count("*") #count the number of "*"'s >>> strs = strs.replace("*","{}") #replace '*'s with '{}' for formatting >>> for x in product("0123456789",repeat=c): ... print strs.format(*x) #use `int()` to get an integer 12300709 12300719 12300729 12300739 12300749 12300759 12300769 12300779 12300789 12300799 .... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |