[英]Removing first elements of tuples in a list
I have a list of tuples as follows:我有一个元组列表如下:
values = [('1', 'hi', 'you'), ('2',' bye', 'bye')]
However, the first element of each tuple is not needed.但是,不需要每个元组的第一个元素。 The desired output is:
所需的输出是:
[('hi', 'you'), (' bye', 'bye')]
I've done enough research to know I can't manipulate tuples but I can't seem to find an answer on how to successfully remove the first element of each tuple in the list.我已经做了足够的研究,知道我不能操作元组,但我似乎无法找到有关如何成功删除列表中每个元组的第一个元素的答案。
Firstly tuple is immutable.首先元组是不可变的。
Secondly try this approach using a list comprehension:其次,使用列表理解尝试这种方法:
a_list = [el[1:] for el in values]
Check slice notation
.检查
slice notation
。
You could use the following list comprehension if tuples are not required:如果不需要元组,您可以使用以下列表理解:
a_list = [a_tuple[1:] for a_tuple in values]
print(a_list)
This would print the following:这将打印以下内容:
[('hi', 'you'), (' bye', 'bye')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.