3

Using Python, I'm trying to convert a sentence of words into a flat list of all distinct letters in that sentence.

Here's my current code:

words = 'She sells seashells by the seashore'

ltr = []

# Convert the string that is "words" to a list of its component words
word_list = [x.strip().lower() for x in words.split(' ')]

# Now convert the list of component words to a distinct list of
# all letters encountered.
for word in word_list:
    for c in word:
        if c not in ltr:
            ltr.append(c)

print ltr

This code returns ['s', 'h', 'e', 'l', 'a', 'b', 'y', 't', 'o', 'r'], which is correct, but is there a more Pythonic way to this answer, probably using list comprehensions/set?

When I try to combine list-comprehension nesting and filtering, I get lists of lists instead of a flat list.

The order of the distinct letters in the final list (ltr) is not important; what's crucial is that they be unique.

Art Metzer
  • 83
  • 1
  • 1
  • 3

7 Answers7

13

Sets provide a simple, efficient solution.

words = 'She sells seashells by the seashore'

unique_letters = set(words.lower())
unique_letters.discard(' ') # If there was a space, remove it.
Mike Graham
  • 73,987
  • 14
  • 101
  • 130
3
set([letter.lower() for letter in words if letter != ' '])

Edit: I just tried it and found this will also work (maybe this is what SilentGhost was referring to):

set(letter.lower() for letter in words if letter != ' ')

And if you need to have a list rather than a set, you can

list(set(letter.lower() for letter in words if letter != ' '))
danben
  • 80,905
  • 18
  • 123
  • 145
3

Make ltr a set and change your loop body a little:

ltr = set()

for word in word_list:
    for c in word:
       ltr.add(c)

Or using a list comprehension:

ltr = set([c for word in word_list for c in word])
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
2
>>> set('She sells seashells by the seashore'.replace(' ', '').lower())
set(['a', 'b', 'e', 'h', 'l', 'o', 's', 'r', 't', 'y'])
>>> set(c.lower() for c in 'She sells seashells by the seashore' if not c.isspace())
set(['a', 'b', 'e', 'h', 'l', 'o', 's', 'r', 't', 'y'])
>>> from itertools import chain
>>> set(chain(*'She sells seashells by the seashore'.lower().split()))
set(['a', 'b', 'e', 'h', 'l', 'o', 's', 'r', 't', 'y'])
ephemient
  • 198,619
  • 38
  • 280
  • 391
2

here are some timings made with py3k:

>>> import timeit
>>> def t():                    # mine (see history)
    a = {i.lower() for i in words}
    a.discard(' ')
    return a

>>> timeit.timeit(t)
7.993071812372081
>>> def b():                    # danben
    return set(letter.lower() for letter in words if letter != ' ')

>>> timeit.timeit(b)
9.982847967921138
>>> def c():                    # ephemient in comment
    return {i.lower() for i in words if i != ' '}

>>> timeit.timeit(c)
8.241267610375516
>>> def d():                    #Mike Graham
    a = set(words.lower())
    a.discard(' ')
    return a

>>> timeit.timeit(d)
2.7693045186082372
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • @ephemient: it works, but it's a bit slower. and btw, `set(i.lower for i in words if i != ' ')` version is 20% slower in py3k – SilentGhost Feb 11 '10 at 16:51
0
set(l for w in word_list for l in w)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0
words = 'She sells seashells by the seashore'

ltr = list(set(list(words.lower())))
ltr.remove(' ')
print ltr
ZenGyro
  • 176
  • 1
  • 6
  • oh, dear, oh dear, oh dear... here is the useful read: http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy – SilentGhost Feb 11 '10 at 17:03