python打印星图
“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对象中的元素。
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() 。
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 。
This will raise a TypeError.
这将引发TypeError 。
For two iterables, the function should contain two arguments. 对于两个可迭代对象,该函数应包含两个参数。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()仅应用于每个可迭代项中的两个项目。
A map object is an iterator. So we can access the next element in the map object using the next() function.
映射对象是iterator 。 因此,我们可以使用next()函数访问地图对象中的next()一个元素。
“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 图片作者Using map():
使用map() :
num=map(pow,[0,1,2],[2,2,2])print (list(num))#Output:[0, 1, 4]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:5map(): 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模块。
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软件