Greg
Greg

Reputation: 109

How to combine similar characters in a list?

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!

Upvotes: 10

Views: 773

Answers (4)

Ambuj Verma
Ambuj Verma

Reputation: 1

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']

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71461

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', '##']

Upvotes: 6

Vibhutha Kumarage
Vibhutha Kumarage

Reputation: 1399

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', '##']
>>>

Upvotes: 3

mingganz
mingganz

Reputation: 363

Try:

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

Upvotes: 9

Related Questions