C#相当于旋转使用python切片操作列表切片、操作、列表、python

2023-09-10 23:52:44 作者:小城黑巷痞子少

在Python中,我可以列表my_list和旋转的内容:

 >>> my_list =列表(范围(10))
>>> my_list
[0,1,2,3,4,5,6,7,8,9]
>>> new_list = my_list [1:] + my_list [:1]
>>> new_list
[1,2,3,4,5,6,7,8,9,0]
 

什么是C#中的等价的方式来创建一个由现有的C#列表的两片一个新的列表?我知道我可以用蛮力如果有必要产生。

解决方案

  VAR newlist = oldlist.Skip(1).Concat(oldlist.Take(1));
 
Python中使用索引对列表进行切片操作

In python, I can take a list my_list and rotate the contents:

>>> my_list = list(range(10))
>>> my_list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> new_list = my_list[1:] + my_list[:1]
>>> new_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

What's the equivalent way in C# to create a new list that is a made up of two slices of an existing C# list? I know I can generate by brute force if necessary.

解决方案

var newlist = oldlist.Skip(1).Concat(oldlist.Take(1));

 
精彩推荐
图片推荐