简体   繁体   中英

How to print each letter in a string only once

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...

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)

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'])

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)

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.

First principles, Clarice. Simplicity.

list(set(sentence_str))

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM