我需要在列表的第二级进行字符串匹配,但是在第一级返回真实案例(第一级中的信息需要对返回进行分类)。
First /@ GatherBy[#, #[[3]] &] &@
Cases[#, x_List /;
MemberQ[x, s_String /;
StringMatchQ[s, ("*PHYSICAL EXAMINATION*"),
IgnoreCase -> True]], {2}] &@
Cases[MemoizeTable["Diagnostic_table.txt"], {_,
11111, __}]
顶部的GatherBy命令只是按日期组织所有条目,所以我没有得到任何重复。然后我要求诊断表中的条件具有与字符串“PHYSICAL EXAMINATION”匹配的术语。
我只需搜索列表的第二级。如果我只搜索第一个,我不会返回所有真实案例。如果我搜索第一个和第二个,我会得到重复项(有些情况包括第一级和第二级所需的字符串,因此列表的第一级和第二级都分别返回)。
我需要在第二级搜索字符串,然后仅返回包含匹配的第二级的列表的第一级。我不需要排除第二级,我只是不希望它单独返回,就像我搜索第一级和第二级一样。
非常感谢任何帮助!
答案 0 :(得分:3)
也许是这样的?
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)最后一个例子(在子列表中选择“无考试”的条目,但在“否”和“考试”之间可能有零或多个字符,且情况又是忽略)
Select[rdata2,
MemberQ[ToLowerCase@Characters@#,
Flatten@Riffle[
ToLowerCase@Characters@{"No", "Examination"}, ___, {1, -1,
2}], {2}] &]
(选择条目1111113)
毫无疑问,更有效的方法可以继续进行。我希望我能正确地解释这个问题。
答案 1 :(得分:0)
如果我理解您的要求,您可以考虑:
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] ]]