0

I was trying a sample exercise on regexes. To find all the letters of the alphabets. Sort the array, and finally eliminate all repetitions.

>>> letterRegex = re.compile(r'[a-z]')
>>> alphabets = letterRegex.findall("The quick brown fox jumped over the lazy dog")
>>> alphabets.sort()
>>> alphabets
['a', 'b', 'c', 'd', 'd', 'e', 'e', 'e', 'e', 'f', 'g', 'h', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'o', 'o', 'o', 'p', 'q', 'r', 'r', 't', 'u', 'u', 'v', 'w', 'x', 'y', 'z']

After doing the sort I tried to make a loop that'll eliminate all repetitions in the array. e.g [...'e', 'e'...]

So I did this

>>> i, j = -1,0
>>> for items in range(len(alphabets)):
        if alphabets[i+1] == alphabets[j+1]:
            alphabets.remove(alphabets[j])

However it didn't work. How can I remove repetitons?

ITJ
  • 144
  • 1
  • 13
  • 2
    The code you've posted does not yield the result you've shown. Please update your post with the exact code that you're using and the results that it yields – inspectorG4dget Jun 23 '17 at 15:51
  • 1
    to get rid of repetitive letters you could do `list(set(alphabets)).sort()` – depperm Jun 23 '17 at 15:52
  • Possible duplicate of [Remove items from a list while iterating](https://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating) – fredtantini Jun 23 '17 at 15:53
  • I accidentally closed the shell, and now I'm not certain what code gave those results. But I'll edit the question, thanks. – ITJ Jun 23 '17 at 16:04

1 Answers1

0

Here's a much easier way of removing co-occurrences:

import itertools

L = ['a', 'b', 'c', 'd', 'd', 'e', 'e', 'e', 'e', 'f', 'g', 'h', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'o', 'o', 'o', 'p', 'q', 'r', 'r', 't', 'u', 'u', 'v', 'w', 'x', 'y', 'z']

answer = []
for k,_group in itertools.groupby(L):
    answer.append(k)

Or simpler still:

answer = [k for k,_g in itertools.groupby(L)]

Both yield this:

In [42]: print(answer)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 't', 'u', 'v', 'w', 'x', 'y', 'z']
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241