简体   繁体   English

如何在 Python 的二维数组中打印除第一个列表的第一个元素之外的所有值

[英]How to print all values except for the first element of the first list in a 2d Array in Python

So, for example, if we have the 2D Array:因此,例如,如果我们有二维数组:

list_2D = [[1, 2, 3], [4, 5, 6]]

I want it to be able to print out:我希望它能够打印出来:

[[2, 3], [4, 5, 6]]

So far, I have tried splicing the list, but I can't figure out a way to isolate just the first element of the first list within the 2D Array.到目前为止,我已经尝试拼接列表,但我想不出一种方法来隔离二维数组中第一个列表的第一个元素。 I feel like the answer lies in list comprehension, but, again, I'm not sure how I would isolate the first element of the first list.我觉得答案在于列表理解,但是,我再次不确定如何隔离第一个列表的第一个元素。 It would be preferrable, if it's possible, if this could fit in just 1 print statement.如果可能的话,如果这只适合 1 个打印语句,那将是更可取的。 Thank you!谢谢!

Just for fun a hack that in one line removes the number, prints, and puts the number back in:只是为了好玩,在一行中删除数字,打印并将数字放回原处:

list_2D[0][print(list_2D):0] = list_2D[0].pop(0),

Try it online! 在线试用!

You can pop the value and then print:您可以pop值然后打印:

list_2D = [[1, 2, 3], [4, 5, 6]]
list_2D[0].pop(0)
print(list_2D)

If you don't want to change the original list, consider copying it and do this on the copy.如果您不想更改原始列表,请考虑复制它并在副本上执行此操作。

Try:尝试:

print([list_2D[0][1:], *list_2D[1:]])

Prints:印刷:

[[2, 3], [4, 5, 6]]

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

相关问题 如何在python中提取2d数组的第一个元素? - How to extract the first element of a 2d array in python? Python:如何在使用 2D 布尔掩码时从 2D numpy 数组逐行获取所有第一个值 - Python: how to get all the first values row-wise from a 2D numpy array when using a 2D boolean mask Python按2D数组中包含列表和值的第一个列表对值进行排序 - Python Sort values by first list in 2D array containing list and value 如何更改二维数组的一维方面的第一个元素 - How to change the first element of 1d aspect of 2d array Python:从二维列表的第一列更改随机元素 - Python: change random element from first column of 2D list Python按第一个元素排序2D数组(不是唯一的) - Python sorting 2D array by the first element (not unique) 在Python中,如何切片列表以获取第一个元素以及除最后一个元素外的所有元素? - In Python, how to slice a list to get the first element, and all elements except the last? 按第一个值对带有 2d 列表的字典进行排序 - Sort a dictionary of with a 2d list by the first values 蟒蛇| 获取二维列表的第一个元素 - python | Get first elements of 2d list 将一维数组的行号与 Python 中大型数据集的二维数组的第一个元素匹配 - match line number of a 1d array with first element of a 2d array for large dataset in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM