我有一个列表,其中一些是英文,而另一些是印地语。我想从列表中删除所有用英文书写的元素。如何实现?
示例:如何从下面的列表L
中删除hello
?
L = ['मैसेज','खेलना','दारा','hello','मुद्रण']
for i in range(len(L)):
print L[i]
预期输出:
मैसेज
खेलना
दारा
मुद्रण
我有一个列表,其中一些是英文,而另一些是印地语。我想从列表中删除所有用英文书写的元素。如何实现?
示例:如何从下面的列表L
中删除hello
?
L = ['मैसेज','खेलना','दारा','hello','मुद्रण']
for i in range(len(L)):
print L[i]
预期输出:
मैसेज
खेलना
दारा
मुद्रण
isalpha()
函数。l = ['मैसेज', 'खेलना', 'दारा', 'hello', 'मुद्रण']
for word in l:
if not word.isalpha():
print word
将会给你返回结果:
मैसेज
खेलना
दारा
मुद्रण
那么来看一个简单的列表推导:
>>> import re
>>> i = ['मैसेज','खेलना','दारा','hello','मुद्रण']
>>> [w for w in i if not re.match(r'[A-Z]+', w, re.I)]
['मैसेज', 'खेलना', 'दारा', 'मुद्रण']
match
和 filter
:import re
list(filter(lambda w: not re.match(r'[a-zA-Z]+', w), ['मैसेज','खेलना','दारा','hello','मुद्रण']))
import re
l=['मैसेज','खेलना','दारा','hello','मुद्रण']
for string in l:
if not re.search(r'[a-zA-Z]', string):
print(string)