0

Hello everyone I have a python question.

I'm trying to print each letter in the given string only once. How do I do this using a for loop and sort the letters from a to z?

Heres what I have;

import string

sentence_str = ("No punctuation should be attached to a word in your list, 
                e.g., end.  Not a correct word, but end is.")

letter_str = sentence_str 
letter_str = letter_str.lower()

badchar_str = string.punctuation + string.whitespace

Alist = []


for i in badchar_str:
    letter_str = letter_str.replace(i,'')


letter_str = list(letter_str)
letter_str.sort() 

for i in letter_str:
    Alist.append(i)
    print(Alist))

Answer I get:

['a']
['a', 'a']
['a', 'a', 'a']
['a', 'a', 'a', 'a']
['a', 'a', 'a', 'a', 'a']
['a', 'a', 'a', 'a', 'a', 'b']
['a', 'a', 'a', 'a', 'a', 'b', 'b']
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c']....

I need:

['a', 'b', 'c', 'd', 'e', 'g', 'h', 'i', 'l', 'n', 'o', 'p', 'r', 's', 't', 'u', 'w', 'y']

no errors...

Wooble
  • 87,717
  • 12
  • 108
  • 131
Thomas Jones
  • 355
  • 2
  • 11
  • 17
  • You may benefit from the set data structure http://docs.python.org/2/tutorial/datastructures.html#sets - also it looks like you are printing a lot more than you should be (see the indentation) – NG. Mar 03 '13 at 22:33
  • `print(sorted(set(sentence_str.lower())))` – jfs Mar 03 '13 at 22:38
  • @J.F.Sebastian - nice, but it still contains the punctuation... – Fredrik Pihl Mar 03 '13 at 22:45
  • @Fredrik: I've skipped that part. Related: [Best way to strip punctuation from a string in Python](http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python). Through a whitelist that contains a set of what OP considers "letters" could be used here instead. – jfs Mar 03 '13 at 22:54
  • @J.F.Sebastian - I saw a chance to comment on someone with a 66k rep and I took it :-) I've seen that link before as well. – Fredrik Pihl Mar 03 '13 at 22:56

6 Answers6

2

Just check if the letter is not already in your array before appending it:

for i in letter_str:
    if  not(i in Alist):
        Alist.append(i)
    print(Alist))

or alternatively use the Set data structure that Python provides instead of an array. Sets do not allow duplicates.

aSet = set(letter_str)
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • You second example could be just `aSet = set(letter_str)` (Except for the print, but that doesn't seem to be essential except for debugging) – Michael Mauderer Mar 03 '13 at 22:41
  • @MichaelMauderer thanks, for some reason I thought if I called set on the entire string, I would only get a set of one element, which would be the string itself. Didn't know set applied like this. – Hunter McMillen Mar 03 '13 at 22:43
  • @HunterMcMillen It works because when you iterate over a string you get each individual letter. Since you can transform any iterable thing to a set, it can be assumed that `set()` means `set()`. In the case of a string that's the letters – entropy Mar 03 '13 at 22:51
2

Using itertools ifilter which you can say has an implicit for-loop:

In [20]: a=[i for i in itertools.ifilter(lambda x: x.isalpha(), sentence_str.lower())]

In [21]: set(a)
Out[21]: 
set(['a',
     'c',
     'b',
     'e',
     'd',
     'g',
     'i',
     'h',
     'l',
     'o',
     'n',
     'p',
     's',
     'r',
     'u',
     't',
     'w',
     'y'])
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • Can be simplified a bit further, using an implicit set comprehension. Assuming Python 2.7. `a = {i for i in itertools.ifilter(lambda x: x.isalpha(), sentence_str.lower())}` – Anorov Mar 04 '13 at 00:53
2

Malvolio correctly states that the answer should be as simple as possible. For that we use python's set type which takes care of the issue of uniqueness in the most efficient and simple way possible.

However, his answer does not deal with removing punctuation and spacing. Furthermore, all answers as well as the code in the question do that pretty inefficiently(loop through badchar_str and replace in the original string).

The best(ie, simplest and most efficient as well as idiomatic python) way to find all unique letters in the sentence is this:

import string

sentence_str = ("No punctuation should be attached to a word in your list, 
                e.g., end.  Not a correct word, but end is.")

bad_chars = set(string.punctuation + string.whitespace)
unique_letters = set(sentence_str.lower()) - bad_chars

If you want them to be sorted, simply replace the last line with:

unique_letters = sorted(set(sentence_str.lower()) - bad_chars)
entropy
  • 3,134
  • 20
  • 20
0

If the order in which you want to print doesn't matter you can use:

sentence_str = ("No punctuation should be attached to a word in your list, 
                e.g., end.  Not a correct word, but end is.")
badchar_str = string.punctuation + string.whitespace
for i in badchar_str:
    letter_str = letter_str.replace(i,'')
print(set(sentence_str))

Or if you want to print in sorted order you could convert it back to list and use sort() and then print.

Ankur Ankan
  • 2,953
  • 2
  • 23
  • 38
0

First principles, Clarice. Simplicity.

list(set(sentence_str))
Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
0

You can use set() for remove duplicate characters and sorted():

import string

sentence_str = "No punctuation should be attached to a word in your list, e.g., end.  Not a correct word, but end is."

letter_str = sentence_str 
letter_str = letter_str.lower()

badchar_str = string.punctuation + string.whitespace

for i in badchar_str:
    letter_str = letter_str.replace(i,'')

characters = list(letter_str);

print sorted(set(characters))
DarthTux
  • 156
  • 1
  • 5