【问题标题】: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"
               ]
           }
        ]

【问题讨论】:

    标签: arrays ruby


    【解决方案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
    • 是的,虽然我后来发现了=~ 解决方案,但这确实有效。非常感谢您的帮助!谢谢
    猜你喜欢
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2014-12-20
    • 2023-01-18
    • 1970-01-01
    • 2014-12-18
    相关资源
    最近更新 更多