10

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!

Ajax1234
  • 69,937
  • 8
  • 61
  • 102
Greg
  • 109
  • 3
  • If you want to combine similar characters your result must be `['h’, ‘e', 'll', 'o', '###'...]`. In your case you split the string by repeating `#`. – Mykola Zotko Feb 09 '19 at 14:26
  • `re.split` with the right pattern or [`itertools.groupby`](https://stackoverflow.com/questions/773/how-do-i-use-pythons-itertools-groupby/45873519#45873519) in general should do it. – pylang Feb 11 '19 at 18:42

4 Answers4

9

Try:

splitter = re.split("(#+)", test)
mingganz
  • 363
  • 1
  • 7
6

You can use itertools.groupby:

import itertools
test = 'hello###_world###test#test123##'
new_result = [''.join(b) for _, b in itertools.groupby(test, key=lambda x:x == '#')]

Output:

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

You can also use re.findall:

import re
result = re.findall('#+|[^#]+', test)

Output:

['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
3

Add + at the end of the regular expression and filtering None values will do the trick

>>> import re
>>> test = 'hello###_world###test#test123##'
>>> splitter = re.split("(#+)", test)
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##', '']
>>> splitter = list(filter(None, splitter))
>>> splitter
['hello', '###', '_world', '###', 'test', '#', 'test123', '##']
>>>
Vibhutha Kumarage
  • 1,372
  • 13
  • 26
0

If you want to combine every similar character in a list.['h', 'eee', 'lll', 'oo', '#########', '_', 'w', 'r', 'd', 'tttt', 'ss', '1', '2', '3'] like this so you can use this.

def combine(s):
dict={}
for i in s:
    key = dict.keys()
    if i in key:
        dict[i]+=1
    else:
        dict[i]=1
v,k = list(dict.values()),list(dict.keys())
product =[]
for i,j in zip(v,k):
    product.append(i*j)
print(product)`enter code here 
s = 'hello###_world###test#test123##'    
count(s)

OUTPUT : ['h', 'eee', 'lll', 'oo', '#########', '_', 'w', 'r', 'd', 'tttt', 'ss', '1', '2', '3']