在Python中从字符串中删除辅音 [英] Deleting consonants from a string in Python
问题描述
这是我的代码.我不确定是否需要计数器才能正常工作.答案应该是'iiii'
.
Here is my code. I'm not exactly sure if I need a counter for this to work. The answer should be 'iiii'
.
def eliminate_consonants(x):
vowels= ['a','e','i','o','u']
vowels_found = 0
for char in x:
if char == vowels:
print(char)
eliminate_consonants('mississippi')
推荐答案
更正您的代码
if char == vowels:
行是错误的.它必须是if char in vowels:
.这是因为您需要检查在元音列表中是否存在该特定字符.除此之外,您还需要print(char,end = '')
(在python3中)将输出全部打印为iiii
.
Correcting your code
The line if char == vowels:
is wrong. It has to be if char in vowels:
. This is because you need to check if that particular character is present in the list of vowels. Apart from that you need to print(char,end = '')
(in python3) to print the output as iiii
all in one line.
最终程序将是
def eliminate_consonants(x):
vowels= ['a','e','i','o','u']
for char in x:
if char in vowels:
print(char,end = "")
eliminate_consonants('mississippi')
输出将是
iiii
其他方式包括
-
使用
in
字符串
Other ways include
Using
in
a stringdef eliminate_consonants(x): for char in x: if char in 'aeiou': print(char,end = "")
看起来很简单,语句
if char in 'aeiou'
检查字符串aeiou
中是否存在char
.As simple as it looks, the statement
if char in 'aeiou'
checks ifchar
is present in the stringaeiou
.''.join([c for c in x if c in 'aeiou'])
仅当字符在
aeiou
''.join(c for c in x if c in 'aeiou')
此gen exp将返回一个生成器,而仅当字符在
aeiou
This gen exp will return a generator than will return the characters only if the character is in
aeiou
您可以使用
re.findall
仅发现你弦中的元音.代码You can use
re.findall
to discover only the vowels in your string. The codere.findall(r'[aeiou]',"mississippi")
将返回在字符串
['i', 'i', 'i', 'i']
中找到的元音列表.所以现在我们可以使用str.join
然后使用will return a list of vowels found in the string i.e.
['i', 'i', 'i', 'i']
. So now we can usestr.join
and then use''.join(re.findall(r'[aeiou]',"mississippi"))
str.translate
andmaketrans
对于此技术,您将需要存储一个映射,该映射将每个非元音与
None
类型匹配.为此,您可以使用string.ascii_lowecase
.制作地图的代码是For this technique you will need to store a map which matches each of the non vowels to a
None
type. For this you can usestring.ascii_lowecase
. The code to make the map isstr.maketrans({i:None for i in string.ascii_lowercase if i not in "aeiou"})
这将返回映射.请将其存储在变量中(对于地图,这里为
m
)this will return the mapping. Do store it in a variable (here
m
for map)"mississippi".translate(m)
这将从字符串中删除所有非
aeiou
字符.This will remove all the non
aeiou
characters from the string.Using
dict.fromkeys
您可以将
dict.fromkeys
与sys.maxunicode
一个>.但是请记住先import sys
!dict.fromkeys(i for i in range(sys.maxunicode+1) if chr(i) not in 'aeiou')
,现在使用
str.translate
.'mississippi'.translate(m)
-
使用
bytearray
Using
bytearray
Using this we can translate the word ,
'mississippi'.encode('ascii', 'ignore').translate(None, non_vowels)
,它将返回
b'iiii'
.使用decode
即可轻松将其转换为str
,即b'iiii'.decode("ascii")
.which will return
b'iiii'
. This can easily be converted tostr
by usingdecode
i.e.b'iiii'.decode("ascii")
.使用
bytes
Using
bytes
bytes
返回一个字节对象,并且是bytearray
的不变版本. (特定于Python 3 )bytes
returns an bytes object and is the immutable version ofbytearray
. (It is Python 3 specific)non_vowels = bytes(set(range(0x100)) - set(b'aeiou'))
使用此功能,我们可以翻译单词
Using this we can translate the word ,
'mississippi'.encode('ascii', 'ignore').translate(None, non_vowels)
,它将返回
b'iiii'
.使用decode
即可轻松将其转换为str
,即b'iiii'.decode("ascii")
.which will return
b'iiii'
. This can easily be converted tostr
by usingdecode
i.e.b'iiii'.decode("ascii")
.python3 -m timeit -s "text = 'mississippi'*100; non_vowels = bytes(set(range(0x100)) - set(b'aeiou'))" "text.encode('ascii', 'ignore').translate(None, non_vowels).decode('ascii')" 100000 loops, best of 3: 2.88 usec per loop python3 -m timeit -s "text = 'mississippi'*100; non_vowels = bytearray(set(range(0x100)) - set(b'aeiou'))" "text.encode('ascii', 'ignore').translate(None, non_vowels).decode('ascii')" 100000 loops, best of 3: 3.06 usec per loop python3 -m timeit -s "text = 'mississippi'*100;d=dict.fromkeys(i for i in range(127) if chr(i) not in 'aeiou')" "text.translate(d)" 10000 loops, best of 3: 71.3 usec per loop python3 -m timeit -s "import string; import sys; text='mississippi'*100; m = dict.fromkeys(i for i in range(sys.maxunicode+1) if chr(i) not in 'aeiou')" "text.translate(m)" 10000 loops, best of 3: 71.6 usec per loop python3 -m timeit -s "text = 'mississippi'*100" "''.join(c for c in text if c in 'aeiou')" 10000 loops, best of 3: 60.1 usec per loop python3 -m timeit -s "text = 'mississippi'*100" "''.join([c for c in text if c in 'aeiou'])" 10000 loops, best of 3: 53.2 usec per loop python3 -m timeit -s "import re;text = 'mississippi'*100; p=re.compile(r'[aeiou]')" "''.join(p.findall(text))" 10000 loops, best of 3: 57 usec per loop
按时间排序的时间
translate (bytes) | 2.88 translate (bytearray)| 3.06 List Comprehension | 53.2 Regular expressions | 57.0 Generator exp | 60.1 dict.fromkeys | 71.3 translate (unicode) | 71.6
如您所见,使用
bytes
的最终方法是最快的.As you can see the final method using
bytes
is the fastest.python3.5 -m timeit -s "text = 'mississippi'*100; non_vowels = bytes(set(range(0x100)) - set(b'aeiou'))" "text.encode('ascii', 'ignore').translate(None, non_vowels).decode('ascii')" 100000 loops, best of 3: 4.17 usec per loop python3.5 -m timeit -s "text = 'mississippi'*100; non_vowels = bytearray(set(range(0x100)) - set(b'aeiou'))" "text.encode('ascii', 'ignore').translate(None, non_vowels).decode('ascii')" 100000 loops, best of 3: 4.21 usec per loop python3.5 -m timeit -s "text = 'mississippi'*100;d=dict.fromkeys(i for i in range(127) if chr(i) not in 'aeiou')" "text.translate(d)" 100000 loops, best of 3: 2.39 usec per loop python3.5 -m timeit -s "import string; import sys; text='mississippi'*100; m = dict.fromkeys(i for i in range(sys.maxunicode+1) if chr(i) not in 'aeiou')" "text.translate(m)" 100000 loops, best of 3: 2.33 usec per loop python3.5 -m timeit -s "text = 'mississippi'*100" "''.join(c for c in text if c in 'aeiou')" 10000 loops, best of 3: 97.1 usec per loop python3.5 -m timeit -s "text = 'mississippi'*100" "''.join([c for c in text if c in 'aeiou'])" 10000 loops, best of 3: 86.6 usec per loop python3.5 -m timeit -s "import re;text = 'mississippi'*100; p=re.compile(r'[aeiou]')" "''.join(p.findall(text))" 10000 loops, best of 3: 74.3 usec per loop
按时间排序的时间
translate (unicode) | 2.33 dict.fromkeys | 2.39 translate (bytes) | 4.17 translate (bytearray)| 4.21 List Comprehension | 86.6 Regular expressions | 74.3 Generator exp | 97.1
这篇关于在Python中从字符串中删除辅音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!