[英]How can i remove the even number indexes in a list of tuples?
我有一个这样的元组列表:
lis__ = [('string', 'id1',...,'string', 'idn')]
具有不同的ids
和strings
。 我如何从该元组列表中删除ids
?这些ID看起来像DSDF2342
,但它们是不同的。 例如:
lis__ = [('string', '234SDFSD',...,'string', 'DFSFSD234')]
所需的输出将是这样的:
[(string string .... string)]
在此先感谢大家。 这是我尝试的:
my_list = [tuple([j.split()[0] for j in i]) for i in lis__]
print my_list
一个好的解决方案:
my_list = [t[::2] for t in lis__]
切片 t[::2]
仅采用索引为偶数(0,2,4,&c)的项目。
这将为您提供不包含以“ id”开头的项目的项目列表:
t = ('string', 'id1',...,'string', 'idn')
no_ids = [ item for item in t if not item.startswith("id")]
如果要删除奇数索引项,还可以使用range()
来提供所需的索引。 ( 其中位置参数是range(start, stop, step)
):
t = ('string', 'id1',...,'string', 'idn')
no_ids = [ t[idx] for idx in range(0, len(t), 2)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.