Reputation: 83
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.
Upvotes: 3
Views: 986
Reputation: 176
words = 'She sells seashells by the seashore'
ltr = list(set(list(words.lower())))
ltr.remove(' ')
print ltr
Upvotes: 0
Reputation: 319881
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
Upvotes: 2
Reputation: 204926
>>> 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'])
Upvotes: 2
Reputation: 76753
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.
Upvotes: 13
Reputation: 83290
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 != ' '))
Upvotes: 3
Reputation: 273646
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])
Upvotes: 3