Python如何将缺失数字与字母拼接为无多余符号的连续字符串
代码修改方案
需要从两处修正原有代码即可得到目标输出:
- 补全原参考字母表中缺失的J、U两个字母,避免漏检测j、u的缺失
- 废弃将结果列表直接转为字符串的操作,改为将列表内元素直接拼接为连续字符串
修改后的完整代码如下:
def split(word): return [char for char in word] a = "8hypotheticall024y6wxz" # 补全缺失的J、U字母 alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" alph = alp.lower() b = split(alph) c = set(b)-set(a) c = sorted(c) # 直接拼接字符列表为字符串 c = ''.join(c) res = [int(i) for i in a if i.isdigit()] num_list = [0,1,2,3,4,5,6,7,8,9] l = set(num_list)-set(res) l = sorted(l) # 将数字转成字符串后拼接 l = ''.join(str(i) for i in l) print(l + c)
运行上述代码输出结果为:13579bdfgjkmnqrsuv,符合预期。
内容的提问来源于stack exchange,提问作者HARSH BHATT
相关产品推荐
相关产品推荐

