【问题标题】:How to remove dates from a list in Python如何从 Python 列表中删除日期
【发布时间】:2016-09-25 04:30:25
【问题描述】:

我有一个标记化文本列表 (list_of_words),看起来像这样:

list_of_words = 
['08/20/2014',
 '10:04:27',
 'pm',
 'complet',
 'vendor',
 'per',
 'mfg/recommend',
 '08/20/2014',
 '10:04:27',
 'pm',
 'complet',
 ...]

我正在尝试从该列表中删除所有日期和时间实例。我试过使用 .remove() 函数,但无济于事。我尝试将通配符(例如“../../....”)传递给我正在排序的停用词列表,但这不起作用。我终于尝试编写以下代码:

for line in list_of_words:
    if re.search('[0-9]{2}/[09]{2}/[0-9]{4}',line):
        list_of_words.remove(line)

但这也不起作用。如何从我的列表中删除格式为日期或时间的所有内容?

【问题讨论】:

  • 是否有特定格式的数据和/或您要删除的时间?

标签: python regex nltk


【解决方案1】:

说明

^(?:(?:[0-9]{2}[:\/,]){2}[0-9]{2,4}|am|pm)$

此正则表达式将执行以下操作:

  • 查找类似于日期12/23/2016 和时间12:34:56 的字符串
  • 在源列表中查找同样是 ampm 的字符串,这些字符串可能是先前时间的一部分

示例

现场演示

示例列表

08/20/2014
10:04:27
pm
complete
vendor
per
mfg/recommend
08/20/2014
10:04:27
pm
complete

处理后的列表

complete
vendor
per
mfg/recommend
complete

示例 Python 脚本

import re

SourceList = ['08/20/2014',
                 '10:04:27',
                 'pm',
                 'complete',
                 'vendor',
                 'per',
                 'mfg/recommend',
                 '08/20/2014',
                 '10:04:27',
                 'pm', 
                 'complete']

OutputList = filter(
    lambda ThisWord: not re.match('^(?:(?:[0-9]{2}[:\/,]){2}[0-9]{2,4}|am|pm)$', ThisWord),
    SourceList)


for ThisValue in OutputList:
  print ThisValue

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    (?:                      group, but do not capture (2 times):
----------------------------------------------------------------------
      [0-9]{2}                 any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
      [:\/,]                   any character of: ':', '\/', ','
----------------------------------------------------------------------
    ){2}                     end of grouping
----------------------------------------------------------------------
    [0-9]{2,4}               any character of: '0' to '9' (between 2
                             and 4 times (matching the most amount
                             possible))
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    am                       'am'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    pm                       'pm'
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------

【讨论】:

  • 这是我一段时间以来遇到的最好解释的答案之一!
【解决方案2】:

如果你想计算列表中的时间和日期字符串,也许你可以试试下面的正则表达式:

[0-9]{2}[\/,:][0-9]{2}[\/,:][0-9]{2,4}

添加python代码:

import re

list_of_words = [
 '08/20/2014',
 '10:04:27',
 'pm',
 'complet',
 'vendor',
 'per',
 'mfg/recommend',
 '08/20/2014',
 '10:04:27',
 'pm',
 'complet'
]
new_list = [item for item in list_of_words if not re.search(r'[0-9]{2}[\/,:][0-9]{2}[\/,:][0-9]{2,4}', item)]

【讨论】:

  • 你的正则表达式很棒。我在回复中使用了它。
  • @user6188402 \d 检查所有 Unicode 数字,而 [0-9] 仅限于这 10 个字符。所以 [0-9] 更有效
  • 使用re.compile然后访问编译后的表达式会更干净、更高效。
【解决方案3】:

试试这个:

import re

list_of_words = ['08/20/2014',
                 '10:04:27',
                 'pm',
                 'complet',
                 'vendor',
                 'per',
                 'mfg/recommend',
                 '08/20/2014',
                 '10:04:27',
                 'pm', 'complet']

list_of_words = filter(
    lambda x: not re.match('[0-9]{2}[\/,:][0-9]{2}[\/,:][0-9]{2,4}', x),
    list_of_words)

【讨论】:

    猜你喜欢
    • 2017-12-05
    • 2019-07-14
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多