简体   繁体   English

如何仅将字符串中的每个字母打印一次

[英]How to print each letter in a string only once

Hello everyone I have a python question. 大家好,我有一个python问题。

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? 如何使用for循环并从a到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. 或者使用Python提供的Set数据结构代替数组。 Sets do not allow duplicates. 集不允许重复。

aSet = set(letter_str)

Using itertools ifilter which you can say has an implicit for-loop: 使用itertools ifilter ,您可以说它具有隐式的for循环:

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. 为此,我们使用python的set类型,它以最有效和最简单的方式处理唯一性问题。

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). 此外,所有答案以及问题中的代码的执行效率都非常低(通过badchar_str循环并替换为原始字符串)。

The best(ie, simplest and most efficient as well as idiomatic python) way to find all unique letters in the sentence is this: 在句子中查找所有唯一字母的最佳方法(即最简单,最有效以及惯用的python)是这样的:

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. 或者,如果要按排序顺序打印,可以将其转换回list并使用sort()然后进行打印。

You can use set() for remove duplicate characters and sorted(): 您可以使用set()删除重复的字符和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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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