【问题标题】:How to remove english alphabets from list in python如何从python列表中删除英文字母
【发布时间】:2016-07-19 05:01:48
【问题描述】:

我有一个包含一些英文文本的列表,而其他在Hindi。我想从用英文写的列表中删除所有元素。如何做到这一点?

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

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

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

预期输出:

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

【问题讨论】:

    标签: python non-english


    【解决方案1】:

    你可以使用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)]
      ['मैसेज', 'खेलना', 'दारा', 'मुद्रण']
      

      【讨论】:

        【解决方案3】:

        您可以将filter 与正则表达式match 一起使用:

        import re
        list(filter(lambda w: not re.match(r'[a-zA-Z]+', w), ['मैसेज','खेलना','दारा','hello','मुद्रण']))
        

        【讨论】:

          【解决方案4】:

          您可以使用 Python 的正则表达式模块。

          import re
          l=['मैसेज','खेलना','दारा','hello','मुद्रण']
          for string in l:
              if not re.search(r'[a-zA-Z]', string):
                  print(string)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-06-14
            • 1970-01-01
            • 1970-01-01
            • 2020-04-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多