【问题标题】:Filter words from list python从列表python中过滤单词
【发布时间】:2018-12-21 08:43:58
【问题描述】:

我有这个小脚本来过滤包含liste值的单词

当我这样做时:

它显示:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'filter'

我该如何纠正它? 谢谢

【问题讨论】:

  • 您好,您正在尝试在 python 字符串上使用 Pyspark Dataframe 方法(where() 这是filter() 的别名)。如果你想使用where(),你需要创建一个 pyspark 数据框。如果你想保留这个字符串(我认为没有必要将这个字符串分布在多个节点上),你需要使用可以从字符串中调用的方法。请注意变量的名称,有时您将名称“列表”用于str 对象,这令人不安。 docs.python.org/3.7/library/string.html
  • @CorentinLimier 谢谢
  • 如何将 listeexclure="AIDE_EN;toCCto;tita" 转换为 Dataframe ?我试过 df = sc.toDF(listeexclure) 但它显示 AttributeError: 'SparkContext' object has no attribute 'toDF'

标签: python apache-spark


【解决方案1】:

这是因为您的 listeexclure 只是一个字符串。如果您想在另一个string 中搜索string,您可以执行以下操作:

假设你有一个 list 喜欢:

lst = ['a', 'ab', 'abc', 'bac']
filter(lambda k: 'ab' in k, lst)
# Result will be ['ab', 'abc']

我认为,您可以在代码中应用相同的方法。所以在你的情况下:

filter((lambda k: broadcastMSA in k, listeexclure)

希望对你有帮助

【讨论】:

  • 如何将 listeexclure 转换为 Dataframe spark ?
  • @samtou006 我真的不知道,但是我刚刚搜索并找到了这个Convert list to DataFrame
猜你喜欢
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多