如何在Python中从列表中删除英文字母

7

我有一个列表,其中一些是英文,而另一些是印地语。我想从列表中删除所有用英文书写的元素。如何实现?

示例:如何从下面的列表L中删除hello

L = ['मैसेज','खेलना','दारा','hello','मुद्रण']  

for i in range(len(L)):    
    print L[i]

预期输出:

मैसेज    
खेलना    
दारा    
मुद्रण
4个回答

9
您可以使用 isalpha() 函数。
l = ['मैसेज', 'खेलना', 'दारा', 'hello', 'मुद्रण']
for word in l:
    if not word.isalpha():
        print word

将会给你返回结果:

मैसेज
खेलना
दारा
मुद्रण

2

那么来看一个简单的列表推导:

>>> import re
>>> i = ['मैसेज','खेलना','दारा','hello','मुद्रण']
>>> [w for w in i if not re.match(r'[A-Z]+', w, re.I)]
['मैसेज', 'खेलना', 'दारा', 'मुद्रण']

1
你可以使用正则表达式 matchfilter
import re
list(filter(lambda w: not re.match(r'[a-zA-Z]+', w), ['मैसेज','खेलना','दारा','hello','मुद्रण']))

0
你可以使用Python的正则表达式模块。
import re
l=['मैसेज','खेलना','दारा','hello','मुद्रण']
for string in l:
    if not re.search(r'[a-zA-Z]', string):
        print(string)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接