tcl lsearch 在列表列表中
Tcl中有一个list列表。
set somelist {{aaa 1} {bbb 2} {ccc 1}}
如何搜索列表中第一项是“bbb”的元素?
我尝试了这种方法,但它不起作用。
lsearch $somelist "{bbb *}"
谢谢。
There is a list of list in Tcl.
set somelist {{aaa 1} {bbb 2} {ccc 1}}
How to search the list's element which first item is "bbb"?
I tried this way but it doesn't work.
lsearch $somelist "{bbb *}"
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用-index,它正是针对这种情况而设计的。正如拉曼曼指出的那样,当你有一个列表时,请使用列表过程。你有没有想过如果你有多个匹配会发生什么?
在你的情况下,我会这样做:
你使用 0 的 -index 来指定你对外部列表的第一个索引感兴趣。 -all 返回所有结果。如果你只想要匹配的列表元素的值,你可以使用 -inline ,如果你只想要匹配元素的索引,则省略它。
Use -index, it's designed for exactly this case. As ramanman points out, when you have a list, use list procedures. Have you thought about what happens if you have multiple matches?
In your case, I would just do this:
You use -index of 0 to specify that you are interested in the first index of the outer list. -all returns all results. And you can use -inline if you just want the value of list element that matches, omit it if you just want the index of the matching element.
如果您要搜索每个列表成员的第一个子列表,则
-index
选项适合您:-index 0
选项告诉lsearch
比较引擎在执行任何其他比较操作之前获取每个项目的第一个元素(即,非常类似于lindex $item 0
)(在我的示例中为-exact
比较) 。您甚至可以提供一个索引列表来深入到特定的子子列表等。(注意:
lsearch
不做的事情不要做的是通过列表的所有子列表执行递归搜索,问题是正式无法知道何时到达叶子。)编辑:< br>
如果您使用旧版本的 Tcl,则可能没有
-index
选项。在这种情况下,您可以使用手动迭代:(我不喜欢对此类事情使用全局匹配。当您查找不是列表中第一个或最后一个的元素时,它也不能很好地工作.)
If you're searching through the first sublist of each list member, the
-index
option is for you:The
-index 0
option tells thelsearch
comparison engine to take the first element of each item (i.e., very much likelindex $item 0
) before doing any other comparison operations (-exact
comparison in my example). You can even supply a list of indices to drill down into a specific sub-sub-list, etc.(NB: What
lsearch
does not do is perform a recursive search through all sublists of a list. The issue is that there's formally no way to know when you've got to the leaves.)EDIT:
If you have an old version of Tcl, you might not have the
-index
option. In that case, you use manual iteration:(I dislike using glob matching for this sort of thing. It also doesn't work nearly so well when you're looking for an element that isn't the first or the last in a list.)
在 Tcl 8.5 中,但不在 Tcl 8.4 中:
-index 标志指定要搜索的子列表的索引: 0 搜索 aaa、bbb... 而 1 搜索 1、2、1...
在 Tcl 8.4 中,使用 Tclx 包中的 keylget 命令:
此语法中的 keylget 命令如果找到则返回 1,如果没有则返回 0。然后将 bbb (2) 旁边的值放入变量 myvar 中。请注意,keylget 命令中 somelist 前面不应有美元符号 ($)。
更新
事实上,-index 标志允许搜索任意深度,如以下代码所示:
In Tcl 8.5, but not in Tcl 8.4:
The -index flags specifies which index of the sub-list to search: 0 searches for aaa, bbb, ... While 1 searches for 1, 2, 1...
In Tcl 8.4, use the keylget command from the Tclx package:
The keylget command in this syntax returns 1 if found, 0 if not. The value next to bbb (2) is then placed in the variable myvar. Note that there should be no dollar sign ($) in front of somelist in the keylget command.
Update
In fact, the -index flag allows searching for arbitrary depth as illustrated by this code:
使用 [lsearch] 的“-index”选项。
Use the "-index" option of [lsearch].
如果你想按照你想要的方式搜索它,你可以使用 glob 选项:
你这些确实是列表列表,并且你正在寻找与 bbb 的第一个元素匹配的第一个子列表,你也可以这样做:
如果 当然,其详细信息取决于列表列表的详细信息(如果您的嵌套列表实际上只有两个元素,或者它是否可以嵌套得更深)。
If you want to search it the way you are trying to, you can use the glob option:
If you those really are lists of lists, and you are looking for matching the first sublist with a first element of bbb you could also do:
The details of that would of course depend on the details of your list of lists (if you actually have only two elements of your nested list, or if it could be more deeply nested.