Mathematica - 从字符串搜索中排除字符串

发布于 2024-11-18 22:50:35 字数 519 浏览 1 评论 0原文

我正在尝试选择表格中最常出现的关键词。 我需要搜索列表中不包含给定第二个、第三个、第四个等单词的单词的出现次数。

例如,我需要搜索单词“lollypop”在不包含单词“candy”的列表中出现的次数。

此代码将返回单词“lollypop”出现的次数:

rt = Parallelize@
 Cases[MemoizeTable["Candy_table.txt"], 
  x_List /; 
   MemberQ[x, 
    s_String /; 
     StringMatchQ[s, ("*lollypop*"), IgnoreCase -> True]], {1}];

我尝试添加 StringFreeQ 来排除“candy”,并尝试添加 Nor ,其中需要添加 > 或者在字符串搜索中,但我不确定如何做到这一点/将其放在哪里/它们..?

基本上,我需要一个“this”而不是“that”代码。

I'm trying to select the most frequently occurring key words on a table.
I need to search for the number of occurrences of a word in a list that DOES NOT include a given second, third, fourth, etc. word.

For example, I need to search for the number of times the word "lollypop" appears in a list that does not include the word "candy".

This code will return the number of times the word "lollypop" appears:

rt = Parallelize@
 Cases[MemoizeTable["Candy_table.txt"], 
  x_List /; 
   MemberQ[x, 
    s_String /; 
     StringMatchQ[s, ("*lollypop*"), IgnoreCase -> True]], {1}];

I tried adding StringFreeQ to exclude "candy", and I tried adding Nor where one would add Or in the string search, but I wasn't sure how to do that/where to put it/them..?

I need a "this" BUT NOT "that" code, basically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

述情 2024-11-25 22:50:35
excludeList = {"candy", "other"};
toCount = "lollypop";

numberOfToCount[list_, tocount_, excludeList_] := 
  If[And @@ ((! MemberQ[list, #]) & /@ excludeList), 
     Count[list, tocount], 
     "Excluded"];

用法:

numberOfToCount[{"lollypop", "lollypop", "the beatles"}, toCount, excludeList]
numberOfToCount[{"lollypop", "lollypop", "candy"}, toCount, excludeList]

(*
-> 2
-> Excluded
*)
excludeList = {"candy", "other"};
toCount = "lollypop";

numberOfToCount[list_, tocount_, excludeList_] := 
  If[And @@ ((! MemberQ[list, #]) & /@ excludeList), 
     Count[list, tocount], 
     "Excluded"];

Usage:

numberOfToCount[{"lollypop", "lollypop", "the beatles"}, toCount, excludeList]
numberOfToCount[{"lollypop", "lollypop", "candy"}, toCount, excludeList]

(*
-> 2
-> Excluded
*)
岁月如刀 2024-11-25 22:50:35

要获取单词,请尝试例如

dl = DictionaryLookup[];

Select[dl, 
 StringFreeQ[#, ___ ~~ "ies" ~~ ___] && 
 StringMatchQ[#, ___ ~~ "loll" ~~ ___] &]
 (*
 -> {"loll", "lolled", "lolling", "lollipop", "lollipops", "lollop", "lolloped", "lolloping", "lollops", "lolls", "lolly"}
 *)

,您可以通过在末尾添加 //Count 来对它们进行计数(例如)。

编辑:看来我误解了你的问题。如果您要问的是:计算“canapes”在列表中出现的次数,该列表不包括“可修改”,则:

dl2 = {"titivation", "curving", "doppelgangers", "objurgations", 
"canapes", "invaluable", "modifiable", "dissect", "ominousness", 
"sentinel"}

If[Not@MemberQ[dl2, "modifiable"], Count[dl2, "canapes"], False]
(*
-> False
*)

while

If[Not@MemberQ[dl2, "plate"], Count[dl2, "canapes"], False]

排除“plate”,因此该列表为 1。

但我对你的评论感到困惑(“此代码正在返回我正在运行其他搜索的列表的初步选择,因此我需要保持列表完整”哪个列表?上面的代码都保持完整)所以我一定还缺少一些东西。

To obtain the words, try eg

dl = DictionaryLookup[];

Select[dl, 
 StringFreeQ[#, ___ ~~ "ies" ~~ ___] && 
 StringMatchQ[#, ___ ~~ "loll" ~~ ___] &]
 (*
 -> {"loll", "lolled", "lolling", "lollipop", "lollipops", "lollop", "lolloped", "lolloping", "lollops", "lolls", "lolly"}
 *)

and you can count them by adding //Count at the end (say).

EDIT: It seems I misunderstood your question. If what you're asking is: count the number of times "canapes" appears in a list, which list does not include "modifiable", then:

dl2 = {"titivation", "curving", "doppelgangers", "objurgations", 
"canapes", "invaluable", "modifiable", "dissect", "ominousness", 
"sentinel"}

If[Not@MemberQ[dl2, "modifiable"], Count[dl2, "canapes"], False]
(*
-> False
*)

while

If[Not@MemberQ[dl2, "plate"], Count[dl2, "canapes"], False]

excludes "plate", thus giving 1 for this list.

But I am confused by your comments ("This code is returning a preliminary selection of lists that I'm running other searches on, so I need to keep the list intact" which list? they're all kept intact by the code above) so I must still be missing something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文