How to combine similar characters in a list?

  • A+
Category:Languages

I'm trying to combine similar characters that are next to each other that are in a list. I was wondering if there was a Python way to do it? Here's an example:

test = 'hello###_world###test#test123##' splitter = re.split("(#)", test) splitter = filter(None, splitter) 

Which returns this in the splitter variable:

['hello', '#', '#', '#', '_world', '#', '#', '#', 'test', '#', 'test123', '#', '#'] 

I'm trying to combine the hashes so the list turns into this:

['hello', '###', '_world', '###', 'test', '#', 'test123', '##'] 

Thanks for any help!

 


Try:

splitter = re.split("(#+)", test) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: