python打印星图

    科技2025-03-26  16

    python打印星图

    地图(map)

    “map(function, iterable, ...)

    “ map(function, iterable, ...)

    Return an iterator that applies a function to every item of iterable, yielding the results. If additional iterable arguments are passed, the function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.” — Python’s documentation

    返回一个迭代器,该迭代器将函数应用于所有可迭代项,并产生结果。 如果传递了其他可迭代的参数,则该函数必须接受那么多参数,并且并行地将其应用于所有可迭代的项目。 对于多个可迭代对象,最短可迭代对象耗尽时,迭代器将停止。” — Python的文档

    The map() function is used to apply a function to each item in the iterable.

    map()函数用于将函数应用于可迭代的每个项目。

    We can also pass multiple iterables, but the function mentioned should also have that many arguments (e.g. two iterables means two arguments).

    我们还可以传递多个可迭代对象,但是提到的函数还应该具有那么多参数(例如,两个可迭代对象意味着两个参数)。

    If multiple iterables given are of different lengths, the map iterator stops when the shortest iterable is exhausted.

    如果给定的多个可迭代对象的长度不同,则最短可迭代对象耗尽时, map迭代器将停止。

    The return type is a map object.

    返回类型是一个map对象。

    A map object is an iterator.

    map对象是迭代器。

    Photo by the author. 图片由作者提供。

    We can access the map object, which is an iterator, in the following ways:

    我们可以通过以下方式访问作为迭代器的map对象:

    We can convert the map object to sequence objects like a list using a list() constructor and like a tuple using a tuple() constructor.

    我们可以使用list()构造函数将map对象转换为序列对象,例如列表,而使用tuple()构造函数将其转换为tuple() 。

    We can also iterate through the map object using a for loop.

    我们还可以使用for循环遍历map对象。

    We can access the element in the map object using the next() function as well.

    我们也可以使用next()函数访问map对象中的元素。

    示例1:使用map()将一个函数应用于所有可迭代项 (Example 1: Applying a function to all items in one iterable using map())

    The square() function is defined to return the square the numbers.

    square()函数定义为返回数字的平方。

    In the map function, we are passing the square() function and list object.

    在map函数中,我们传递了square()函数和list对象。

    The square() function is applied to all items in the list object.

    square()函数应用于列表对象中的所有项目。

    The return type is a map object.

    返回类型是一个map对象。

    A map object is an iterator that contains the square of all items in the iterable (list object).

    map对象是一个迭代器,它包含可迭代对象(列表对象)中所有项目的平方。

    Convert the map object to a list using a list() constructor.

    使用list()构造函数将map对象转换为list() 。

    示例2:使用map()函数将lambda函数应用于所有可迭代项 (Example 2: Applying a lambda function to all items in one iterable using the map() function)

    示例3:使用map()函数将一个函数应用于两个相同长度的可迭代对象(Example 3: Applying a function to two iterables of the same length using the map() function)

    Since two iterables are given, the function should contain two arguments.

    由于给出了两个可迭代,该函数应包含两个参数。

    The multiply(x,y) function will take the first argument x from the first iterable num1 and the second argument y from second iterable num2.

    multiply(x,y)函数将从第一个可迭代num1获取第一个参数x ,从第二个可迭代num2获取第二个参数y 。

    示例4:使用map()函数将包含一个参数的函数应用于两个可迭代对象 (Example 4: Applying a function containing one argument to two iterables using the map() function)

    This will raise a TypeError.

    这将引发TypeError 。

    For two iterables, the function should contain two arguments.

    对于两个可迭代对象,该函数应包含两个参数。

    示例5:使用map()函数将函数应用于相同长度的更多可迭代对象 (Example 5: Applying a function to more iterables of the same length using the map() function)

    示例6:使用map()函数将函数应用于更多不同长度的可迭代对象(Example 6: Applying a function to more iterables of different lengths using the map() function)

    If multiple iterables given are of different lengths, the map iterator stops when the shortest iterable is exhausted.

    如果给定的多个可迭代对象的长度不同,则最短可迭代对象耗尽时,映射迭代器将停止。

    So here in this example, the shortest iterable is of length 2. So the function multiply() is applied to only two items in each iterable.

    因此,在此示例中,最短的可迭代项的长度为2。因此,函数multiply()仅应用于每个可迭代项中的两个项目。

    示例7:使用for循环遍历map对象 (Example 7: Iterating through a map object using a for loop)

    示例8:使用next()函数访问地图对象中的元素(Example 8: Accessing elements in the map object using the next() function)

    A map object is an iterator. So we can access the next element in the map object using the next() function.

    映射对象是iterator 。 因此,我们可以使用next()函数访问地图对象中的next()一个元素。

    示例9:使用map()函数将内置函数应用于可迭代(字符串) (Example 9: Applying a built-in function to an iterable (string) using the map() function)

    colors=['red','yellow','blue']s=list(map(str.upper,colors))print (s)#Output:['RED', 'YELLOW', 'BLUE']

    示例10:使用map()函数将函数应用于可迭代(元组) (Example 10: Applying a function to an iterable (tuple) using the map() function)

    def square(x): return x*xt1=(1,2,3)s=tuple(map(square,t1))print (s)#Output:(1, 4, 9)

    itertools.starmap() (itertools.starmap())

    “itertools.starmap(function, iterable)

    “ itertools.starmap(function, iterable)

    Make an iterator that computes the function using arguments obtained from the iterable. Used instead of map() when argument parameters are already grouped in tuples from a single iterable (the data has been “pre-zipped”). The difference between map() and starmap() parallels the distinction between function(a,b) and function(*c).” — Python’s documentation

    创建一个迭代器,该迭代器使用从迭代器获得的参数来计算函数。 当参数参数已从单个可迭代项的元组中分组时(数据已“预压缩”),而不是map()。 map()和starmap()之间的区别类似于function(a,b)和function(* c)之间的区别。” — Python的文档

    First, we have to import the itertools module.

    首先,我们必须导入itertools模块。

    The starmap method is supported by the itertools module only.

    仅itertools模块支持starmap方法。

    starmap(function, iterable) → The function and iterable are passed to the starmap method.

    starmap(function, iterable) →将函数和iterable传递给starmap方法。

    Items inside the iterable should also be iterable. Otherwise, it will raise a TypeError.

    可迭代内部的项目也应该可迭代。 否则,它将引发TypeError 。

    The return type is an itertools.starmap object.

    返回类型是itertools.starmap对象。

    The starmap object is an iterator.

    星starmap对象是一个迭代器。

    We can access the starmap object, which is an iterator, in the following ways:

    我们可以通过以下方式访问作为迭代器的starmap对象:

    We can convert the starmap object to sequence objects like a list using a list() constructor or a tuple using a tuple() constructor.

    我们可以使用list()构造函数将starmap对象转换为序列对象,例如列表,或者使用tuple()构造函数将其转换为元tuple() 。

    We can also iterate through the map object using a for loop.

    我们还可以使用for循环遍历map对象。

    We can access the element in the starmap object using the next() function as well.

    我们也可以使用next()函数访问starmap对象中的元素。

    Image by Author 图片作者

    示例1:使用starmap()将用户定义的函数应用于元组列表(Example 1: Applying a user-defined function to a list of tuples using starmap())

    示例2:使用starmap()将pow()应用于元组列表(Example 2: Applying pow() to a list of tuples using starmap())

    Using map():

    使用map() :

    num=map(pow,[0,1,2],[2,2,2])print (list(num))#Output:[0, 1, 4]

    示例3:如果iterable中的元素不可迭代,则将引发TypeError (Example 3: If elements inside the iterable are not iterable, it will raise a TypeError)

    import itertoolsnum=itertools.starmap(lambda x:x**2,[1,2,3])print (list(num))#Output:TypeError: 'int' object is not iterable

    示例4:使用starmap()将lambda函数应用于元组列表 (Example 4: Applying a lambda function to a list of tuples using starmap())

    import itertoolsnum=itertools.starmap(lambda x,y:x+y,[(0,1),(1,2),(2,3)])print (list(num))#Output:[1, 3, 5]

    示例5:使用for循环访问starmap对象中的项目 (Example 5: Accessing items in the starmap object using a for loop)

    示例6:使用next()方法访问starmap对象中的项目(Example 6: Accessing items in the starmap object using the next() method)

    A map object is an iterator. So we can access the next element in the map object using the next() function.

    映射对象是iterator 。 因此,我们可以使用next()函数访问地图对象中的next()一个元素。

    import itertoolsnum=itertools.starmap(lambda x,y:x+y,[(0,1),(1,2),(2,3)])print (next(num))#Output:1print (next(num))#Output:3print (next(num))#Output:5

    结论 (Conclusion)

    map(): Multiple iterables are allowed.

    map() :允许多个可迭代。

    starmap(): Only one iterable is allowed. Items inside the iterable should also be iterable.

    starmap() :仅允许一个迭代。 可迭代内部的项目也应该可迭代。

    Both the map object and starmap object are iterators.

    map对象和starmap对象都是迭代器。

    map(): Built-in function.

    map() :内置函数。

    itertools.starmap(): You have to import the itertools module.

    itertools.starmap() :您必须导入itertools模块。

    资源资源 (Resources)

    map-python documentation

    map-python文档

    itertools.starmap-python documentation

    itertools.starmap-python文档

    翻译自: https://medium.com/better-programming/exploring-map-vs-starmap-in-python-6bcf32f5fa4a

    python打印星图

    相关资源:python开发真实星空显示tkinter软件
    Processed: 0.010, SQL: 8