可读的C#相当于Python的切片操作 [英] Readable C# equivalent of Python slice operation

查看:129
本文介绍了可读的C#相当于Python的切片操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C#相当于Python的切片操作?

What is the C# equivalent of Python slice operations?

my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
result1 = my_list[2:4]
result2 = my_list[1:]
result3 = my_list[:3]
result4 = my_list[:3] + my_list[4:]

<一个HREF =htt​​p://stackoverflow.com/questions/1301316/c-sharp-equivalent-of-rotating-a-list-using-python-slice-operation>有些是这里覆盖,但它是丑陋的并没有解决切片的所有使用到它的点没有明显的回答这个问题。

Some of it is covered here, but it is ugly and doesn't address all the uses of slicing to the point of it not obviously answering the question.

推荐答案

最近真的是LINQ .Skip() 。取()

The closest is really LINQ .Skip() and .Take()

例如:

var result1 = myList.Skip(2).Take(4);
var result2 = myList.Skip(1);
var result3 = myList.Skip(myList.Count() - 3);
var result4 = myList.Skip(myList.Count() - 3).Concat(myList.Skip(4))

这篇关于可读的C#相当于Python的切片操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆