Python中C# LINQ的select等价物
我对Python还很陌生,但之前用过一段时间的C#。我发现Python有一个叫做filter的方法,可以用在集合上,这个方法看起来和C#中的LINQ的where子句是相似的。
我在想,Python中有没有类似于LINQ的select语句的东西呢?
举个例子:my_collection.select(my_object => my_object.my_property)
这个语句会返回my_collection中每个对象的my_property属性的集合。
3 个回答
0
试试 pandas 吧!
选择数据
在 C# 中,你可以这样写:my_collection.Select(my_object => my_object.my_property)
而在 pandas 中,你可以用:my_collection['my_property']
或者:
在 C# 中,你可以这样写:my_collection.Select(x => x.my_property + 2)
在 Python 中,你可以用:my_collection['my_property'].apply(lambda x: x + 2)
筛选数据
在 C# 中,你可以这样写:my_collection.Where(x => x.my_property == 1)
而在 pandas 中,你可以用:my_collection[my_collection['my_property']==1]
43
[my_object.my_property for my_object in my_collection]
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。