python初学者函数题库

    科技2023-11-27  94

    python初学者函数题库

    编程 , Python的 (Programming, Python)

    At first, the usage of map, zip, and filter functions seem intimidating for beginners in Python programming. The goal of this article is to provide you details about what are these functions, its syntax, and how to use them with examples so that you can start using them in your application right away. So, let’s get started.

    首先,对于Python编程的初学者来说, map , zip和filter函数的使用似乎令人生畏。 本文的目的是向您提供有关这些函数的含义,语法以及如何将其与示例结合使用的详细信息,以便您可以立即在应用程序中开始使用它们。 因此,让我们开始吧。

    地图功能 (map function)

    The map function returns an iterator after applying the function to the iterable.

    在将函数应用于可迭代对象之后,map函数返回一个迭代器。

    句法: (Syntax:)

    The general syntax of a map function:

    映射函数的一般语法:

    map(<function>, <iterables>)

    where,function: a function which will be executed for each element in the iterable(s)iterable: one or more iterable such as lists, tuples, etc.

    其中, function :将对iterable中的每个元素执行的函数 。iterable :一个或多个可迭代的对象,例如列表,元组等。

    Note: If more than one iterables are passed, the length of the returned iterator depends on the size of the iterable with the least elements.

    注意 :如果传递了多个迭代器,则返回的迭代器的长度取决于元素最少的迭代器的大小。

    When Python encounters map(lambda x: x**3, a), only map object is created. When the the output of map function is passed to iterables such as list, tuple then the function actually gets executed and shows the result.

    当Python遇到map(lambda x: x**3, a) ,只会创建map对象。 当map函数的输出传递给可迭代对象(例如list,tuple)时,该函数实际上会执行并显示结果。

    Image by Author 图片作者

    过滤功能 (filter function)

    The filter function returns an iterator containing only those elements from the iterable for which the function returns a truth value of 1 or True.

    过滤器函数返回一个迭代器,该迭代器仅包含那些来自可迭代函数的元素,该函数为其返回真值1或True。

    句法 (Syntax)

    The general syntax for filter function:

    过滤器功能的一般语法:

    filter(<function>, <iterable>)

    where,function: a function which will be executed for each element in the iterable(s)iterable: iterable such as lists, tuples, etc.

    其中, function :将对iterable中的每个元素执行的函数 iterable :iterable,例如列表,元组等。

    This is similar to map function. When Python encounters filter(lambda x: x < 50, a), only filter object is created. When the the output of filter function is passed to iterables such as list, tuple then the function actually gets executed and shows the result.

    这类似于map功能。 当Python遇到filter(lambda x: x < 50, a) ,仅创建filter对象。 当filter函数的输出传递给可迭代对象(例如list,tuple)时,该函数实际上将被执行并显示结果。

    Image by Author 图片作者

    zip功能 (zip function)

    The zip function joins iterators based on their relative position and returns an iterator in the form of tuples.

    zip函数根据迭代器的相对位置连接它们,并以元组的形式返回迭代器。

    句法 (Syntax)

    The general syntax for zip function:

    zip函数的一般语法:

    zip(iterable1, iterable2,…)

    Note: If more than one iterables are passed, the length of the returned iterator depends on the size of the iterable with the least elements.

    注意 :如果传递了多个迭代器,则返回的迭代器的长度取决于元素最少的迭代器的大小。

    Similar to map and filter functions, when Python encounters zip(list1, list2), only zip object is created and stored. When the the output of zip function is passed to iterables such as list, tuple then the function actually gets executed and shows the result.

    与地图和过滤器功能相似,当Python遇到zip(list1, list2) ,仅创建和存储zip对象。 当zip函数的输出传递给可迭代对象(例如list,tuple)时,该函数实际上会执行并显示结果。

    Image by Author 图片作者

    If you are interested in learning more about Python, here are a few articles which you might like:

    如果您有兴趣了解有关Python的更多信息,请阅读以下几篇文章:

    How to use Python for and while loops

    如何使用Python for和While循环

    Optimization in Python — Interning

    Python优化—实习

    Optimization in Python — Peephole

    Python优化—窥Kong

    Mutability and Immutability in Python

    Python中的可变性和不可变性

    Thank you for reading this article. You can reach me at https://www.linkedin.com/in/chetanambi/

    感谢您阅读本文。 您可以通过https://www.linkedin.com/in/chetanambi/与我联系

    翻译自: https://medium.com/towards-artificial-intelligence/the-beginners-guide-to-map-zip-and-filter-functions-in-python-827d5897dcf

    python初学者函数题库

    相关资源:四史答题软件安装包exe
    Processed: 0.011, SQL: 8