简体   繁体   English

如何从元组列表中的元组中删除

[英]how do I remove from tuples in a list of tuples

I have a list of tuples like this 我有一个这样的元组列表

[
  ('px', Timestamp('2017-07-01 00:00:00')),
  ('px', Timestamp('2017-08-01 00:00:00')),  
  ('px', Timestamp('2017-09-01 00:00:00')),  
  ('px', Timestamp('2017-10-01 00:00:00')),  
  ('px', Timestamp('2017-11-01 00:00:00'))
]

and want to remove the 'px' keeping only the timestamps as a tuple. 并想删除仅将时间戳记为元组的'px'

You can slice off the first items in a list comprehension : 您可以分割列表理解中的第一项:

new_lst = [x[1:] for x in lst]

The tuple slice gives you a new tuple, with the items at index 0 removed. 元组切片为您提供了一个新的元组,其中索引0处的项目已删除。

您还可以使用list comprehension推导来解压缩数据:

final = [(v,) for _, v in yourlist]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM