tcl/tk参考——列表操作lindex

.

.

名称

lindex - 从列表中获得一个元素

语法

lindex list ?index...?

描述

lindex命令接受一个参数列表 list,可以接受0个或者多个 index参数,在多个参数的情况下,参数可以是单独的一次排列,也可以是在一个列表当中。

如果不指定index参数:

lindex list
或者
lindex list {}
这种情况下返回 lindex列表本身。

当只有一个单独的元素时,lindex命令返回list列表中的第index个元素。替代时元素从0开始(也就是说索引0就是指列表的第一个元素),如果index是负数或者大于列表长度就返回一个空字符串。解释器在解释每一个index值时和string index命令相同,都支持单个和多个index参数。

如果指定了多个index,将会选择列表的子列表中的元素。例如

lindex $a 1 2 3
或者
lindex $a {1 2 3}
与下面的命令相同
lindex [lindex [lindex $a 1] 2] 3

示例

lindex {a b c}
       a b c
lindex {a b c} {}
       a b c
lindex {a b c} 0
       a
lindex {a b c} 2
       c
lindex {a b c} end
       c
lindex {a b c} end-1
       b
lindex {{a b c} {d e f} {g h i}} 2 1
       h
lindex {{a b c} {d e f} {g h i}} {2 1}
       h
lindex {{{a b} {c d}} {{e f} {g h}}} 1 1 0
       g
lindex {{{a b} {c d}} {{e f} {g h}}} {1 1 0}
       g
 
  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值