Find "one letter that appears twice" in a string

PythonRegexPython 2.7

Python Problem Overview

I'm trying to catch if one letter that appears twice in a string using RegEx (or maybe there's some better ways?), for example my string is:


The output would be:


However, I've tried something like:

re.findall('[a-z]{2}', 'ugknbfddgicrmopn')

but in this case, it returns:

['ug', 'kn', 'bf', 'dd', 'gi', 'cr', 'mo', 'pn']   # the except output is `['dd']`

I also have a way to get the expect output:

>>> l = []
>>> tmp = None
>>> for i in 'ugknbfddgicrmopn':
...     if tmp != i:
...         tmp = i
...         continue
...     l.append(i*2)
>>> l

But that's too complex...

If it's 'abbbcppq', then only catch:

 ^^  ^^

So the output is:

['bb', 'pp']

Then, if it's 'abbbbcppq', catch bb twice:

 ^^^^ ^^

So the output is:

['bb', 'bb', 'pp']

Python Solutions

Solution 1 - Python

You need use capturing group based regex and define your regex as raw string.

>>>'([a-z])\1', 'ugknbfddgicrmopn').group()
>>> [i+i for i in re.findall(r'([a-z])\1', 'abbbbcppq')]
['bb', 'bb', 'pp']


>>> [i[0] for i in re.findall(r'(([a-z])\2)', 'abbbbcppq')]
['bb', 'bb', 'pp']

Note that , re.findall here should return the list of tuples with the characters which are matched by the first group as first element and the second group as second element. For our case chars within first group would be enough so I mentioned i[0].

Solution 2 - Python

As a Pythonic way You can use zip function within a list comprehension:

>>> s = 'abbbcppq'
>>> [i+j for i,j in zip(s,s[1:]) if i==j]
['bb', 'bb', 'pp']

If you are dealing with large string you can use iter() function to convert the string to an iterator and use itertols.tee() to create two independent iterator, then by calling the next function on second iterator consume the first item and use call the zip class (in Python 2.X use itertools.izip() which returns an iterator) with this iterators.

>>> from itertools import tee
>>> first = iter(s)
>>> second, first = tee(first)
>>> next(second)
>>> [i+j for i,j in zip(first,second) if i==j]
['bb', 'bb', 'pp']

#Benchmark with RegEx recipe:

~ $ python -m timeit --setup "s='abbbcppq'" "[i+j for i,j in zip(s,s[1:]) if i==j]"
1000000 loops, best of 3: 1.56 usec per loop
~ $ python -m timeit --setup "s='abbbcppq';import re" "[i[0] for i in re.findall(r'(([a-z])\2)', 'abbbbcppq')]"
100000 loops, best of 3: 3.21 usec per loop

After your last edit as mentioned in comment if you want to only match one pair of b in strings like "abbbcppq" you can use finditer() which returns an iterator of matched objects, and extract the result with group() method:

>>> import re
>>> s = "abbbcppq"
>>> [ for item in re.finditer(r'([a-z])\1',s,re.I)]
['bb', 'pp']

Note that re.I is the IGNORECASE flag which makes the RegEx match the uppercase letters too.

Solution 3 - Python

Using back reference, it is very easy:

import re
p = re.compile(ur'([a-z])\1{1,}')
re.findall(p, u"ugknbfddgicrmopn")
#output: [u'd']
#output: ['b', 'p']

For more details, you can refer to a similar question in perl:

Solution 4 - Python

It is pretty easy without regular expressions:

In [4]: [k for k, v in collections.Counter("abracadabra").items() if v==2]
Out[4]: ['b', 'r']

Solution 5 - Python

Maybe you can use the generator to achieve this

def adj(s):
    last_c = None
    for c in s:
        if c == last_c:
            yield c * 2
        last_c = c

s = 'ugknbfddgicrmopn'
v = [x for x in adj(s)]
# output: ['dd']

Solution 6 - Python

"or maybe there's some better ways"

Since regex is often misunderstood by the next developer to encounter your code (may even be you), And since simpler != shorter,

How about the following pseudo-code:

function findMultipleLetters(inputString) {        
    foreach (letter in inputString) {
        if (dictionaryOfLettersOccurrance[letter] == 2) {
    return multipleLetters;
multipleLetters = findMultipleLetters("ugknbfddgicrmopn");

Solution 7 - Python

A1 = "abcdededdssffffccfxx"

print A1[1]
for i in range(len(A1)-1):
    if A1[i+1] == A1[i]:
        if not A1[i+1] == A1[i-1]:
            print A1[i] *2

Solution 8 - Python

>>> l = ['ug', 'kn', 'bf', 'dd', 'gi', 'cr', 'mo', 'pn']
>>> import re
>>> newList = [item for item in l if"([a-z]{1})\1", item)]
>>> newList


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRemi GuanView Question on Stackoverflow
Solution 1 - PythonAvinash RajView Answer on Stackoverflow
Solution 2 - PythonMazdakView Answer on Stackoverflow
Solution 3 - PythonGurupad HegdeView Answer on Stackoverflow
Solution 4 - PythonDima TisnekView Answer on Stackoverflow
Solution 5 - PythonxhgView Answer on Stackoverflow
Solution 6 - PythonLavi AvigdorView Answer on Stackoverflow
Solution 7 - PythonMark WhiteView Answer on Stackoverflow
Solution 8 - PythonMayur KoshtiView Answer on Stackoverflow