无法根据条件弄清楚如何从数组中删除哈希

Posted

技术标签:

【中文标题】无法根据条件弄清楚如何从数组中删除哈希【英文标题】:Trouble figuring out how to delete hash from array based on conditions 【发布时间】:2021-12-04 00:57:26 【问题描述】:

如果散列中的特定键包含或包含某些特定单词,我正在尝试从数组中删除一些散列。在下面找到数组:

BANNED_WORDS = ['Hacked', 'hack', 'fraud', 'hacked']

    data = [
       
           "news_url": "https://www.benzinga.com/markets/cryptocurrency/21/10/23391043/north-vancouver-to-heat-buildings-with-bitcoin-mining",
           "image_url": "https://crypto.snapi.dev/images/v1/m/v/fw-69939.jpeg",
           "title": "North Vancouver To Heat Buildings With Bitcoin Mining",
           "text": "Canadian hack Bitcoin (CRYPTO: BTC) mining firm MintGreen has partnered with state-owned Lonsdale Energy Corporation (LEC) to heat 100 residential and commercial buildings in North Vancouver with recovered energy from crypto mining.",
           "source_name": "Benzinga",
           "date": "Fri, 15 Oct 2021 12:16:19 -0400",
           "topics": [
               "mining"
           ],
           "sentiment": "Neutral",
           "type": "Article",
           "tickers": [
               "BTC"
           ]
       ,
       
           "news_url": "https://u.today/ethereum-20-next-steps-to-mainnet-shared-by-ethereum-foundation",
           "image_url": "https://crypto.snapi.dev/images/v1/b/t/10169-69937.jpg",
           "title": "Ethereum 2.0 Next Steps to Mainnet Shared by Ethereum Foundation",
           "text": "Ethereum (ETH) developers have entered final phase of testing before hotly anticipated ETH1-ETH2 transition",
           "source_name": "UToday",
           "date": "Fri, 15 Oct 2021 12:11:00 -0400",
           "topics": [],
           "sentiment": "Neutral",
           "type": "Article",
           "tickers": [
               "ETH"
           ]
       
    ]

我正在尝试删除文本或标题包含/包含上述 BANNED_WORDS 数组中的任何单词的任何哈希。

我已经尝试了以下和其他一些变体,但似乎都没有奏效。我是 ruby​​ 新手,谁能指出我做错了什么,谢谢。

data.select|coin| coin[:text].split(" ").select |word| !BANNED_WORDS.include?(word) || coin[:title].split(" ").select |word| !BANNED_WORDS.include?(word)

所以结果应该是:

filtered_result = [
           
               "news_url": "https://u.today/ethereum-20-next-steps-to-mainnet-shared-by-ethereum-foundation",
               "image_url": "https://crypto.snapi.dev/images/v1/b/t/10169-69937.jpg",
               "title": "Ethereum 2.0 Next Steps to Mainnet Shared by Ethereum Foundation",
               "text": "Ethereum (ETH) developers have entered final phase of testing before hotly anticipated ETH1-ETH2 transition",
               "source_name": "UToday",
               "date": "Fri, 15 Oct 2021 12:11:00 -0400",
               "topics": [],
               "sentiment": "Neutral",
               "type": "Article",
               "tickers": [
                   "ETH"
               ]
           
        ]

【问题讨论】:

【参考方案1】:

这是正则表达式的工作。

R = /\b(?:#BANNED_WORDS.join('|'))\b/
  #=> /\b(?:Hacked|hack|fraud|hacked)\b/
data.reject  |h| h[:title].match?(R) || h[:text].match?(R) 
  #=> [:news_url=>"https://u.today/ethereum-20-next-steps...,
  #     ...
  #     :tickers=>["ETH"]]

见Regexp#match?。

\b 在正则表达式中是一个单词边界。它们的存在是为了防止 'haskintosh''defraud' 匹配。

【讨论】:

我在运行代码时收到此错误undefined method match? for "North Vancouver To Heat Buildings With Bitcoin Mining:String (NoMethodError)。关于修复的任何想法? Regexp#match? 在 Ruby v2.4 中首次亮相。如果您使用的是旧版本,请使用 h[:text].match(R)h[:text] =~ R 是的,虽然我后来发现了=~ 解决方案,但这确实有效。非常感谢您的帮助!谢谢

以上是关于无法根据条件弄清楚如何从数组中删除哈希的主要内容,如果未能解决你的问题,请参考以下文章

Java,无法弄清楚如何从回文字符串中删除符号

Swift:无法根据数组播放每个声音序列

如何根据道具有条件地在 Stack Navigator 中渲染不同的屏幕

试图弄清楚如何从 jquery datepicker 中删除假期

根据用户输入 javascript 返回一个过滤后的数组

无法弄清楚如何通过仅使用类的函数删除整个链接列表