【问题标题】:Equivalent list comprhension methods between python and c# [duplicate]python和c#之间的等效列表理解方法[重复]
【发布时间】:2016-07-29 17:55:50
【问题描述】:

大家早上好。

我来自 python 环境并转向 c# 脚本。

在 C# 中有没有一种方法可以简单地从列表中收集多个元素,如图所示?

List = []
List.append(a,b,c,d,e,f,g,h,i)
items = List[2:6]

得到

c
d
e
f
g

有人有简单的解决方案吗?

【问题讨论】:

  • 您可以查看使用 System.Collections.Generic,并使用 List。 List 是通用的,将采用任何数据类型,例如 List my_list = new List();你可以像 my_list.Add(2);你可以使用 foreach 来完成它。或类似 my_list.ForEach(x=> WriteLine($"x = {x} "));
  • 谢谢你们。根据您的建议,我这样解决了: var Selected = list.Skip(2).Take(5);

标签: c# python list


【解决方案1】:

我想你想要类似的东西

var items = new [] {"a", "b", "c", "d", "e", "f", "g", "h", "i" };
foreach(var x in items.Skip(2).Take(5))
    Console.WriteLine(x);

【讨论】:

    【解决方案2】:

    在 .NET 中可能等同于列表解析的是 LINQ。

    List newList = list.Skip(2).Take(6);
    

    试试这个来获取你的数据。

    【讨论】:

    • 这实际上需要一个太多,因为 python 代码正在做开始和结束索引。
    猜你喜欢
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2023-01-02
    • 2011-06-15
    • 2012-01-31
    • 1970-01-01
    相关资源
    最近更新 更多