def split_num_str(my_str):
num = [x for x in my_str if x.isdigit()]
num = "".join(num)
if not num:
num = None
my_str = [x for x in my_str if x.isalpha()]
my_str = ''.join(my_str)
if not my_str:
my_str = None
return num, my_str
m = map(split_num_str, ['01123absd', 'bsdf', '02454'])
print m
您可以使用正则表达式来执行此操作。我们想要的是:
请注意,
regex
将创建命名组,它还将编译一次,以便在每次调用时更高效。在最后一行的调用给出以下输出:
^{pr2}$如果您不想使用regex,这是一个解决方案:
结果=
[('01123', 'absd'), (None, 'bsdf'), ('02454', None)]
与jramirez answer相似,只是稍微短一点:
仍然使用
^{pr2}$filter
稍微短一点:输出:
相关问题 更多 >
编程相关推荐