[英]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),
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.