相关疑难解决方法(0)

Python的列表理解与.NET LINQ

以下简单的LINQ代码

string[] words = { "hello", "wonderful", "linq", "beautiful", "world" };

// Get only short words
var shortWords =
  from word in words
  where word.Length <= 5
  select word;

// Print each word out
shortWords.Dump();
Run Code Online (Sandbox Code Playgroud)

可以使用列表推导将其翻译成python,如下所示.

words = ["hello", "wonderful", "linq", "beautiful", "world"]
shortWords = [x for x in words if len(x) <=5]
print shortWords
Run Code Online (Sandbox Code Playgroud)
  • LINQ是另一个实现列表理解的想法吗?
  • LINQ可以做什么样的例子但是列表理解不能做.

c# python linq list-comprehension

54
推荐指数
3
解决办法
2万
查看次数

标签 统计

c# ×1

linq ×1

list-comprehension ×1

python ×1