Mathematica 列表 - 搜索二级并返回一级?

发布于 2024-11-19 05:06:54 字数 613 浏览 2 评论 0原文

我需要在列表的第二级中进行字符串匹配,但在第一级返回真实案例(第一级中有我需要对返回进行分类的信息)。

 First /@ GatherBy[#, #[[3]] &] &@
  Cases[#, x_List /;
   MemberQ[x, s_String /;
    StringMatchQ[s, ("*PHYSICAL EXAMINATION*"), 
     IgnoreCase -> True]], {2}] &@
  Cases[MemoizeTable["Diagnostic_table.txt"], {_, 
   11111, __}]

顶部的 GatherBy 命令只是按日期组织所有条目,因此我不会得到任何重复项。然后,我询问诊断表中具有与字符串“体检”匹配的术语的病例。

我只需搜索列表的第二级。如果我只搜索第一个,我不会返回所有真实的案例。如果我搜索第一层和第二层,我会得到重复项(某些情况下在第一层和第二层都包含所需的字符串,因此列表的第一层和第二层都会分别返回)。

我需要在第二级搜索字符串,然后仅返回包含匹配的第二级的列表的第一级。我不需要排除第二级,我只是不希望它像搜索第一级和第二级那样单独返回。

任何帮助将不胜感激!

I need to string match in the second level of a list but have true cases returned at the first level (there's information in the first level that I need to categorize the returns).

 First /@ GatherBy[#, #[[3]] &] &@
  Cases[#, x_List /;
   MemberQ[x, s_String /;
    StringMatchQ[s, ("*PHYSICAL EXAMINATION*"), 
     IgnoreCase -> True]], {2}] &@
  Cases[MemoizeTable["Diagnostic_table.txt"], {_, 
   11111, __}]

The GatherBy command at the top is just organizing all the entries by date so I don't get any duplicates. Then I ask for cases within the diagnostic table that have terms matching the string "PHYSICAL EXAMINATION".

I have to search only the second level of the lists. If I search just the first I don't return all the true cases. If I search the first and second, I get duplicates (some cases include the desired string at both the first and second levels, so the first and second levels of the list are both returned, separately).

I need to search for the string at the second level and then return ONLY the first level of the lists that contain that matched second level. I don't need to exclude the second level, I just don't want it returned separately like it is if I search levels one and two.

Any help would be much appreciated!

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

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

发布评论

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

评论(2

天冷不及心凉 2024-11-26 05:06:54

也许是这样的?

list = {{a, b, c, {x, y}, d, x}, {a, b, c, d, x}, {{a, b, c, d}, x}}

Select[list, MemberQ[#, x, {2}] &]

输出:

{{a, b, c, {x, y}, d, x}}

更新

这也可以

Cases[list, _?(MemberQ[#, x, {2}] &)]

更新@rose问题(参见评论)

以下数据如何选择在字符串中包含单词“PHYSICAL EXAMINATION”的条目,例如在字符串“P-023 PHYSICAL EXAMINATION,TECHNICIAN,NOS”中,在第二级(即仅在子列表)? (我稍微修改了@rose的示例)

rdata2={{1111113,21119,SQLDateTime[{2011,1,11,11,11,0.`}],31111,"EB/JW",1,47000,"T-510 CHEEK",{"T-510 CHEEK","No Examination, NOS"},"Text bla bla bla physical examination bla bla"},{1111114,21119,SQLDateTime[{2011,2,11,11,11,0.`}],31112,"EB/JW",1,47000,"T-510 CHEEK",{"T-510 CHEEK","P-023 PHYSICAL EXAMINATION, TECHNICIAN, NOS"},"Text bla bla bla"},
{1111115,21000,SQLDateTime[{2011,1,11,11,11,0.`}],31111,"EB/JW",1,47000,"T-510 CHEEK",{"T-510 CHEEK","P-023 physical examination, TECHNICIAN, NOS"},"Text bla bla bla physical examination bla bla"}};

(1)一种方式(仅返回条目1111114)

Select[rdata2, 
 MemberQ[Characters@#, 
   Flatten@{___, Characters["PHYSICAL EXAMINATION"], ___}, {2}] &]

(2)忽略大小写(但仍在子列表中选择)

Select[rdata2,MemberQ[ToLowerCase@Characters@#,
Flatten@{___,ToLowerCase@
Characters["PHYSICAL EXAMINATION"],___},{2}]&]

(选择条目1111114和1111115)

(3)最后一个示例(选择子列表中包含“No Examination”的条目,但“No”和“Examination”之间可能有零个或多个个字符,并且其中大小写再次被忽略)

Select[rdata2, 
 MemberQ[ToLowerCase@Characters@#, 
   Flatten@Riffle[
     ToLowerCase@Characters@{"No", "Examination"}, ___, {1, -1, 
      2}], {2}] &]

(选择条目 1111113)

毫无疑问,有更有效的方法可以继续。我希望我正确地解释了这个问题。

Maybe something like this?

list = {{a, b, c, {x, y}, d, x}, {a, b, c, d, x}, {{a, b, c, d}, x}}

Select[list, MemberQ[#, x, {2}] &]

Output:

{{a, b, c, {x, y}, d, x}}

Update

This will also work

Cases[list, _?(MemberQ[#, x, {2}] &)]

Update to @rose question (see comments)

Given the following data how can I select for entries which contain the words "PHYSICAL EXAMINATION" within a string, for example within the string "P-023 PHYSICAL EXAMINATION, TECHNICIAN, NOS", at the second level (ie only within the sublist)? (I have modified @rose's example somewhat)

rdata2={{1111113,21119,SQLDateTime[{2011,1,11,11,11,0.`}],31111,"EB/JW",1,47000,"T-510 CHEEK",{"T-510 CHEEK","No Examination, NOS"},"Text bla bla bla physical examination bla bla"},{1111114,21119,SQLDateTime[{2011,2,11,11,11,0.`}],31112,"EB/JW",1,47000,"T-510 CHEEK",{"T-510 CHEEK","P-023 PHYSICAL EXAMINATION, TECHNICIAN, NOS"},"Text bla bla bla"},
{1111115,21000,SQLDateTime[{2011,1,11,11,11,0.`}],31111,"EB/JW",1,47000,"T-510 CHEEK",{"T-510 CHEEK","P-023 physical examination, TECHNICIAN, NOS"},"Text bla bla bla physical examination bla bla"}};

(1) One way (returning only entry 1111114)

Select[rdata2, 
 MemberQ[Characters@#, 
   Flatten@{___, Characters["PHYSICAL EXAMINATION"], ___}, {2}] &]

(2) Ignore case (but still selecting within sublist)

Select[rdata2,MemberQ[ToLowerCase@Characters@#,
Flatten@{___,ToLowerCase@
Characters["PHYSICAL EXAMINATION"],___},{2}]&]

(selects for entries 1111114 & 1111115)

(3) A final example (select for entries with "No Examination" within sublist but where there may be zero or more characters between "No" and "Examination", and where case is again ignored)

Select[rdata2, 
 MemberQ[ToLowerCase@Characters@#, 
   Flatten@Riffle[
     ToLowerCase@Characters@{"No", "Examination"}, ___, {1, -1, 
      2}], {2}] &]

(selects for entry 1111113)

There are no doubt more efficient ways to proceed. I hope I have interpreted the question correctly.

桜花祭 2024-11-26 05:06:54

如果我了解您的要求,您可以考虑:

dat = {{1111113, 21119, SQLDateTime[{2011, 1, 11, 11, 11, 0.`}], 
    31111, "EB/JW", 1, 47000, 
    "T-510 CHEEK", {"T-510 CHEEK", "No Examination, NOS"}, 
    "Text bla bla bla physical examination bla bla"}, {1111114, 21119,
     SQLDateTime[{2011, 2, 11, 11, 11, 0.`}], 31112, "EB/JW", 1, 
    47000, "T-510 CHEEK", {"T-510 CHEEK", 
     "P-023 PHYSICAL EXAMINATION, TECHNICIAN, NOS"}, 
    "Text bla bla bla"}, {1111115, 21000, 
    SQLDateTime[{2011, 1, 11, 11, 11, 0.`}], 31111, "EB/JW", 1, 47000,
     "T-510 CHEEK", {"T-510 CHEEK", 
     "P-023 physical examination, TECHNICIAN, NOS"}, 
    "Text bla bla bla physical examination bla bla"}};

p = 
 Position[ dat,
   _String?(StringMatchQ[#, "*PHYSICAL EXAMINATION*", IgnoreCase -> True] &),
   {2, 3}
 ];

dat[[ Union[First /@ p] ]]

If I understand your requirements, you may consider:

dat = {{1111113, 21119, SQLDateTime[{2011, 1, 11, 11, 11, 0.`}], 
    31111, "EB/JW", 1, 47000, 
    "T-510 CHEEK", {"T-510 CHEEK", "No Examination, NOS"}, 
    "Text bla bla bla physical examination bla bla"}, {1111114, 21119,
     SQLDateTime[{2011, 2, 11, 11, 11, 0.`}], 31112, "EB/JW", 1, 
    47000, "T-510 CHEEK", {"T-510 CHEEK", 
     "P-023 PHYSICAL EXAMINATION, TECHNICIAN, NOS"}, 
    "Text bla bla bla"}, {1111115, 21000, 
    SQLDateTime[{2011, 1, 11, 11, 11, 0.`}], 31111, "EB/JW", 1, 47000,
     "T-510 CHEEK", {"T-510 CHEEK", 
     "P-023 physical examination, TECHNICIAN, NOS"}, 
    "Text bla bla bla physical examination bla bla"}};

p = 
 Position[ dat,
   _String?(StringMatchQ[#, "*PHYSICAL EXAMINATION*", IgnoreCase -> True] &),
   {2, 3}
 ];

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