指定字符串搜索的列和行

发布于 2024-11-28 10:08:44 字数 819 浏览 2 评论 0原文

因为我正在使用一个非常复杂的表,在可变位置有令人讨厌的重复值,所以我想在特定的行和列之间进行字符串搜索。

例如:

table={{"header1", "header2", "header3", 
 "header4"}, {"falsepositive", "falsepositive", "name1", 
 "falsepositive"}, {"falsepositive", "falsepositive", "name2", 
 "falsepositive"}, {"falsepositive", "falsepositive", 
 "falsepositive", "falsepositive"}}

%//TableForm=
 header1          header1          header1          header1
 falsepositive    falsepositive    name1            falsepositive
 falsepositive    falsepositive    name2            falsepositive
 falsepositive    falsepositive    falsepositive    falsepositive

如何在第三列、第一行到第二行中查找字符串?

我想使用 Which 根据字符串在表中的位置分配值。

例如,

Which[string matched in location one, value, matched in location two, value2]

Because I'm working with a very complex table with nasty repeated values in variable places, I'd like to do a string search between specific rows and columns.

For example:

table={{"header1", "header2", "header3", 
 "header4"}, {"falsepositive", "falsepositive", "name1", 
 "falsepositive"}, {"falsepositive", "falsepositive", "name2", 
 "falsepositive"}, {"falsepositive", "falsepositive", 
 "falsepositive", "falsepositive"}}

%//TableForm=
 header1          header1          header1          header1
 falsepositive    falsepositive    name1            falsepositive
 falsepositive    falsepositive    name2            falsepositive
 falsepositive    falsepositive    falsepositive    falsepositive

How do I look for a string, for example, in column three, rows one through two?

I'd like to use Which to assign values based on a string's location in the table.

E.g.,

Which[string matched in location one, value, matched in location two, value2]

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

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

发布评论

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

评论(3

〆凄凉。 2024-12-05 10:08:44

据我了解,您想要测试给定的字符串是否位于矩阵的某个子部分中。您可以使用 Part ([[...]]) 和 Span (;;) 选择这些小节,您可以使用它们来指示范围或范围的子样本。测试此小节是否包含您的模式可以由 MemberQ 完成,如下所示:

 MemberQ[table[[1 ;; 2, 3]], "name2"]

 (* ==> False *)

 MemberQ[table[[1 ;; 2, 3]], "header3"]

(* ==> True *)

这样,您的 Which 语句可能如下所示:

myVar =
 Which[
  MemberQ[table[[1 ;; 2, 3]], "name2"], 5,
  MemberQ[table[[2 ;; 3, 4]], "falsepositive"], 6,
    ...
   True, 20
  ]

As I understand it you want a test whether or not a given string is in a certain subsection of a matrix. You can pick these subsections using Part ([[...]]) and Span (;;), with which you can indicate ranges or subsamples of ranges. Testing whether or not this subsection contains your pattern can be done by MemberQ, like this:

 MemberQ[table[[1 ;; 2, 3]], "name2"]

 (* ==> False *)

 MemberQ[table[[1 ;; 2, 3]], "header3"]

(* ==> True *)

In this way, your Which statement could look like this:

myVar =
 Which[
  MemberQ[table[[1 ;; 2, 3]], "name2"], 5,
  MemberQ[table[[2 ;; 3, 4]], "falsepositive"], 6,
    ...
   True, 20
  ]
南冥有猫 2024-12-05 10:08:44
Length[Cases[Position[table, "name1"], {1 | 2, 3}]] >= 1

输出->真

Cases[Position[table, "name1"], {1 | 2, 3}]

输出 -> {{2, 3}}

Length[Cases[Position[table, "name1"], {1 | 2, 3}]] >= 1

Output -> True

Or

Cases[Position[table, "name1"], {1 | 2, 3}]

Output -> {{2, 3}}

同尘 2024-12-05 10:08:44

也许,如果我理解你的话:

f[table_, value_, rowmin_, rowmax_, colmin_, colmax_] := 
 Select[Position[table, value], 
  rowmin <= First@# <= rowmax && colmin <= Last@# <= colmax &]
f[table, "name1", 1, 10, 1, 10]
(*
-> {{2, 3}}
*)

Perhaps, if I understand you:

f[table_, value_, rowmin_, rowmax_, colmin_, colmax_] := 
 Select[Position[table, value], 
  rowmin <= First@# <= rowmax && colmin <= Last@# <= colmax &]
f[table, "name1", 1, 10, 1, 10]
(*
-> {{2, 3}}
*)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文