【问题标题】:filter object at python 3.X [duplicate]python 3.X中的过滤器对象[重复]
【发布时间】:2017-06-11 23:48:24
【问题描述】:

python 3.X 中,我一直在编写这些代码:

一个函数用于“text_tokenizing”,另一个函数用于“删除多余字符”。 在“remove_characters_after_tokenization”函数中,我使用了“filter”。

我的问题:当我运行我的项目时,我在控制台中看到这一行:

<filter object at 0x00000277AA20DE48> <filter object at 0x00000277AA44D160> <filter object at 0x00000277AA44D470>

我该如何解决这个问题?

这是我项目的代码:

import nltk
import re
import string
from pprint import pprint

corpus = ["The brown fox wasn't that quick and he couldn't win the race",
          "Hey that's a great deal! I just bought a phone for $199",
          "@@You'll (learn) a **lot** in the book. Python is an amazing language !@@"]



# Declare a function for "Tokenizing Text"
def tokenize_text(text):
    sentences = nltk.sent_tokenize(text)
    word_tokens = [ nltk.word_tokenize(sentence) for sentence in sentences]
    return word_tokens

# Declare a function for "Removing Special Characters"
def remove_characters_after_tokenization(tokens):
    pattern = re.compile('[{}]'.format(re.escape(string.punctuation)))
    filtered_tokens = list(filter(None, [pattern.sub('', token) for token in tokens]))
    return filtered_tokens


token_list = [tokenize_text(text) for text in corpus]
pprint(token_list)

filtered_list_1 = list(filter(None,[remove_characters_after_tokenization(tokens)
                                for tokens in sentence_tokens])
                   for sentence_tokens in token_list)

print(type(filtered_list_1))
print(len(filtered_list_1))
print(filtered_list_1)

【问题讨论】:

  • @JETM,感谢您的回复,但是如果存在,我如何检查此重复项?
  • 罪魁祸首是您创建filtered_list_1的行。此行迭代 token_list,而不是 filter 返回的迭代器,因此 filter 迭代器是左联合的。
  • @brelian 抱歉,我不确定你在问什么?这是一条评论,当有人标记为重复时会自动插入。这基本上意味着我认为那个人和你有同样的问题。
  • @JETM 好的,没问题。实际上我必须为 token_list 中的每个 sentence_tokens 创建一个过滤器。

标签: python python-3.x nltk stop-words


【解决方案1】:

以下行为每个 sentence_tokenstoken_list:

filtered_list_1 = list(filter(None, [remove_characters_after_tokenization(tokens)
                                for tokens in sentence_tokens])
                   for sentence_tokens in token_list)

也许您想创建一个列表列表:

filtered_list_1 = list(filter(None, ([remove_characters_after_tokenization(tokens)
                                      for tokens in sentence_tokens]
                                     for sentence_tokens in token_list)))

【讨论】:

  • 它有效。谢谢你的帮助。正是我必须过滤域。 ;)
猜你喜欢
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 2023-01-02
相关资源
最近更新 更多