为了账号安全,请及时绑定邮箱和手机立即绑定

将数字与Word分开并在Python中返回新列表

将数字与Word分开并在Python中返回新列表

qq_笑_17 2021-04-05 20:16:23
new_list = []pattern = re.compile("([0-9]+)([a-zA-Z]+)")for i, x in enumerate(my_list):    if (re.match(r"([0-9]+)([a-zA-Z]+)", x)):        result = pattern.match(x)        #result = result.group(1) + ' ' + result.group(2)        new_list.append(result.group(1))        new_list.append(result.group(2))        i = i + 2        new_list = new_list + my_list[i:]return new_list电流输出:Input: ['2fee', 'lmao', '222wow']Output: ['2', 'fee', '222', '222wow', '222', 'wow']所需的输出:['2', 'fee', 'lmao', '222', 'wow']如何获得此输出?非常感谢你
查看完整描述

2 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

itertools.groupby搭配使用str.isdigit:


from itertools import groupby


L = ['2fee', 'lmao', '222wow']


res = [''.join(j) for strng in L for _, j in groupby(strng, key=str.isdigit)]


# ['2', 'fee', 'lmao', '222', 'wow']

请注意,这将拆分出现在字符串中的每个数字实例。例如,'1a23bc'将被分割为'1','a','23','bc'。


查看完整回答
反对 回复 2021-04-27
?
HUWWW

TA贡献1874条经验 获得超12个赞

或者,尝试:


import re

input = ['2fee', 'lmao', '222wow']

output = []

for s in input:

    output.extend(t for t in re.split('(\d+)', s) if t)

print output


查看完整回答
反对 回复 2021-04-27
  • 2 回答
  • 0 关注
  • 205 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号