Can you use LINQ types and extension methods in IronPython?

后端 未结 4 1950
迷失自我
迷失自我 2021-01-30 13:28

Is it possible to use the LINQ types and extension methods in IronPython?

If so how? And also is there often more pythonic to do the same thing?

4条回答
  •  面向向阳花
    2021-01-30 14:18

    Some of the things you'd do with LINQ can be done with list comprehensions:

    [myFunc(i) for i in numbers if i > 3]
    

    Or you can use map, reduce, and filter:

    map(myFunc, filter(lambda x: x > 3, numbers))
    

    But list comprehensions are much more "Pythonic" than using the functional programming constructs. For reducing things, consider using "".join or sum. And you can check the truth value of entire iterables by using any and all

    Just remember these translations:

    Select -> map
    Where -> filter
    Aggregate -> reduce
    

    And you'll be well on your way!

提交回复
热议问题