Python - 正确提取拆分列表 [英] Python - extracting split list correctly

查看:168
本文介绍了Python - 正确提取拆分列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为此问题的后续行动,我有一个表达式如下: ['(','44','+(','3','+','ll','))'] 这是通过使用 re.findall('\w + | \W +',item)方法创建的,但是在这个字符串列表中,有两个错误。一个是'+('而另一个是'))'。

As a follow up to this question, I have an expression such as this:['(', '44', '+(', '3', '+', 'll', '))'] which was created by using re.findall('\w+|\W+',item) method, however within this list of strings, there are two errors. One is the '+(' and the other is the '))'.

有没有一个pythonic的方式,我可以拆分只是运算符,使列表将类似 ['(','44','+','(','3','+','ll',')',')']

Is there a pythonic way that I could split just the operators such that the list would be something like ['(', '44', '+','(', '3', '+', 'll', ')',')'].

(保持数字/字母在一起,分隔符号)

(keep the digits/letters together, separate the symbols)

谢谢

推荐答案

尝试这样:

import re
lst = ['z+2-44', '4+(55+z)+88']
[re.findall('\w+|\W', s) for s in lst]

可能对他人有帮助。

这篇关于Python - 正确提取拆分列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆