【问题标题】:TypeError: expected string or bytes-like object while filtering the nested list of strings with RegExTypeError:使用 RegEx 过滤嵌套的字符串列表时预期的字符串或类似字节的对象
【发布时间】:2018-05-30 13:50:24
【问题描述】:

我有这个嵌套的字符串列表,它处于清理的最后阶段。我想用空格替换嵌套列表中的非字母或创建一个没有非字母的新列表。这是我的清单:

list = [['hello', 'mr.', 'smith', ',', 'how', 'are', 'you', 'doing', 'today', '?'], ['the', 'weather', 'is', 'great', ',', 'and', 'python', 'is', 'awesome', '.'], ['the', 'sky', 'is', 'pinkish-blue', '.'], ['you', 'should', "n't", 'eat', 'cardboard', '.']]

这就是我想用它来清理它的模式

pattern = re.compile(r'\W+')
newlist = list(filter(pattern.search, list))
print(newlist)

代码不起作用,这是我得到的错误:

Traceback (most recent call last):
File "/Users/art/Desktop/TxtProcessing/regexp", line 28, in <module>
newlist = [list(filter(pattern.search, list))]
TypeError: expected string or bytes-like object

我知道list 不是字符串,而是字符串列表,我该如何解决? 非常感谢任何帮助!

【问题讨论】:

  • 您最初是如何获得这份清单的?在标记句子和单词时,您可能已经清除了非字母标记。
  • 永远不要将变量调用到内置函数(listsetdict 等)。
  • 您不能在列表列表上执行 pattern.search。
  • 尝试[list(filter(pattern.search, sl)) for sl in your_list],不要命名变量list,否则您将无法使用该函数。

标签: python regex string python-3.x filter


【解决方案1】:

首先,隐藏像 list 这样的内置名称可能会导致各种麻烦 - 请谨慎选择变量名称。

这里实际上不需要正则表达式 - 有一个内置的 isalpha() string method

如果字符串中的所有字符都是字母并且至少有一个字符,则返回 true,否则返回 false。

In [1]: l = [['hello', 'mr.', 'smith', ',', 'how', 'are', 'you', 'doing', 'today', '?'], ['the', 'wea
   ...: ther', 'is', 'great', ',', 'and', 'python', 'is', 'awesome', '.'], ['the', 'sky', 'is', 'pink
   ...: ish-blue', '.'], ['you', 'should', "n't", 'eat', 'cardboard', '.']]

In [2]: [[item for item in sublist if item.isalpha()] for sublist in l]
Out[2]: 
[['hello', 'smith', 'how', 'are', 'you', 'doing', 'today'],
 ['the', 'weather', 'is', 'great', 'and', 'python', 'is', 'awesome'],
 ['the', 'sky', 'is'],
 ['you', 'should', 'eat', 'cardboard']]

您可以通过以下方式应用相同的过滤逻辑,但使用mapfilter(您还需要functools.partial() 的帮助):

In [4]: from functools import partial

In [5]: for item in map(partial(filter, str.isalpha), l):
    ...:     print(list(item))

['hello', 'smith', 'how', 'are', 'you', 'doing', 'today']
['the', 'weather', 'is', 'great', 'and', 'python', 'is', 'awesome']
['the', 'sky', 'is']
['you', 'should', 'eat', 'cardboard']

【讨论】:

  • 我已经尝试过这段代码 [[item for item in sublist if item.isalpha()] for sublist in l] 并且它工作得很好,但是它让我的文本没有“先生”并且没有“粉蓝色”。我希望先生留下,但没有'。和没有'-'的粉蓝色。
【解决方案2】:

你需要深入你的列表

import re

list_ = [['hello', 'mr.', 'smith', ',', 'how', 'are', 'you', 'doing', 'today', '?'], ['the', 'weather', 'is', 'great', ',', 'and', 'python', 'is', 'awesome', '.'], ['the', 'sky', 'is', 'pinkish-blue', '.'], ['you', 'should', "n't", 'eat', 'cardboard', '.']]

pattern = re.compile(r'\W+')

newlist_ = [item 
            for sublist_ in list_ 
            for item in sublist_ 
            if pattern.search(item)]

print(newlist_)
# ['mr.', ',', '?', ',', '.', 'pinkish-blue', '.', "n't", '.']

此外,您不得将变量命名为list

【讨论】:

  • 我将模式更改为 = re.compile(r'\w+') 并得到以下输出: ['hello', 'mr.', 'smith', 'how', 'are' , 'you', 'doing', 'today', 'the', 'weather', 'is', 'great', 'and', 'python', 'is', 'awesome', 'the', '天空','是','粉蓝色','你','应该','不','吃','纸板']。我的问题是“先生”中的句号,“粉蓝色”中的连字符和“n't”中的撇号。它不应该消失吗?
  • @Art:re.search()re.match()有区别!
【解决方案3】:

您正在尝试将列表传递给re.search,但是,由于应该进行模式匹配,因此只允许使用字符串。尝试循环遍历列表:

import re
l = [['hello', 'mr.', 'smith', ',', 'how', 'are', 'you', 'doing', 'today', '?'], ['the', 'weather', 'is', 'great', ',', 'and', 'python', 'is', 'awesome', '.'], ['the', 'sky', 'is', 'pinkish-blue', '.'], ['you', 'should', "n't", 'eat', 'cardboard', '.']]
new_l = [[b for b in i if re.findall('^\w+$', b)] for i in l]

另外,请注意,您的原始变量名称 list 隐藏了内置的 list 函数,在这种情况下会将列表内容分配给属性 list

【讨论】:

  • re.findall() 会创建一个不必要的匹配列表 - 您可能想要使用 re.search()re.match() 代替?...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 2022-01-21
  • 2018-10-17
  • 2020-07-08
  • 2018-05-04
  • 2017-08-29
相关资源
最近更新 更多