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打印星图


http://www.taodudu.cc/news/show-6912614.html

相关文章:

  • 在Arcgis及ArcgisPro中加载全国亚米级影像——星图影像
  • soul的星图实现
  • 【python爬虫】Python爬取+PR绘制你出生那日的星图
  • java面向对象的概念,Java类、引用变量与堆对象
  • 五对俄罗斯夫妇愿意让科学家对孩子进行基因编辑?|技术前沿洞察
  • HTML5期末大作业:棋牌游戏网站设计——棋牌游戏(8页) HTML+CSS+JavaScript web大作业 静态网页
  • 专精特新新企业技术创新发展趋势研究分析讲座详情
  • 软件开发实训需要用到的算法和结构_软件开发实习心得体会
  • 红宝书1-8单元生词速记
  • 在字节跳动,造赛博古籍
  • 毕业设计-基于深度学习的古文字识别系统
  • [oeasy]python0128_unicode_字符集_character_set_八卦_星座
  • “帐”与“账”
  • 软件测试必备技能
  • Android屏幕适配-必备知识
  • 便利店零售分析
  • 【智能零售】德勤中国和阿里研究院:便利店的下一站
  • 超市管理系统(对象+集合)
  • 便利店小程序
  • 145. 超市
  • .NET超市管理系统
  • 便利蜂的“便利经”,降本增效,2023年门店要达到1万家
  • 代码规范格式
  • 规定时间格式
  • 证书格式
  • 关于格式规范
  • 规范格式!
  • 邮件格式的规定
  • 格式与规范
  • 实现规定格式的字符串显示

python打印星图_在python中探索地图与星图相关推荐

  1. python打印时间_在Python中定义Main函数(第二部分)

    Main函数的最佳实践 既然您已经了解两种执行方式上的差异,那么掌握一些最佳实践方案还是很有用的.它们将适用于编写作为脚本运行的代码或者在另一个模块导入的代码. 如下是四种实践方式: 将大部分代码放入 ...

  2. python 打印文件名_在Python中打印文件名,关闭状态和文件模式

    python 打印文件名 Prerequisite: Opening, closing a file/open(), close() functions in Python 先决条件: 在Python ...

  3. python 打印类型_让Python输出更漂亮:PrettyPrinter

    PrettyPrinter是Python 3.6 及以上版本中的一个功能强大.支持语法高亮.描述性的美化打印包.它使用了改进的Wadler-Leijen布局算法,和Haskell打印美化库中的pret ...

  4. python打印长方形_利用python打印出菱形、三角形以及矩形的方法实例

    前言 本文主要给大家介绍了关于利用python打印出菱形.三角形以及矩形的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍: 实例代码 #coding:utf-8 rows = int ...

  5. python 打印文件_在Python中打印word文档

    我这里有一个简单的批处理文件,它将从命令行打印word文档. "C:\Program Files\Microsoft Office\Office12\winword.exe" &q ...

  6. python打印表格_使用 Python 打印漂亮的表格,这两项基本功你可会?

    今天给大家介绍如何在打印字符串时,规则对齐的两种方法,帮助大家在 shell 界面下输出漂亮的表格. 第一种:使用 format 先来看几个小 demo 左对齐 >>>"{ ...

  7. 用python打印倒三角形_用Python打印三角形

    打印左下直角三角形 num = input("please input your number: ") for i in range(num): for j in range(i+ ...

  8. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  9. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

最新文章

  1. 手机Excel怎么转换成PDF方法
  2. GDALWarp设置GDALWarpOptions::dfWarpMemoryLimit过大时处理失败
  3. CentOS下Docker安装
  4. HDU2199,HDU2899,HDU1969,HDU2141--(简单二分)
  5. 电脑组装与维护教程_小白不会装机?教你如何自己组装一台电脑。装机图文教程...
  6. 自定义Flume拦截器,并将收集的日志存储到Kafka中(案例)
  7. c语言的查询功能,求C语言实现查询功能(如果选择3,如何实现查询)
  8. 什么能在main()函数之前或之后执行
  9. Atitit.异常处理 嵌套  冗长的解决方案
  10. AtCoder ZONe Energy Programming Contest 题解
  11. 服务器部署docker
  12. STM32连续采样_STM32 - 利用双缓冲实现实时曲线显示(续)
  13. bond解除 centos7_centos7上实现bonding
  14. SAP License:2021年了!还不知道 SAP顾问的职业前景?
  15. 2021年Java开发者应该学习的技术
  16. 大数据数学基础 python描述下载_正版 大数据数学基础(Python语言描述)Python 大数据 数学 高职-计算机-大数据技术...
  17. 成也萧何,败也萧何---PIG JOIN 的replicated
  18. js人脸识别,tracker.js前端人脸识别框架
  19. 通过调用ffmpeg来将mp3和jpg合并为mp4视频-批处理。
  20. DS,Enovia,MatrixOne, eMatrix

热门文章

  1. Android图像处理之画笔特效处理
  2. 网桥和NAT原理和区别祥解
  3. html返回不刷新页面,js有什么办法返回上一个页面并不刷新代码
  4. 设计模式-观察者模式(Observer Pattern)
  5. kali之中文输入法
  6. 测一测你的性格基因与生命路线,很准噢!
  7. Python爬取网易云音乐歌单内所有歌曲
  8. Android OpenGL三——旋转和触控事件
  9. html 滤镜效果 黑白
  10. 在“打破次元壁”这件事上,以太坊做得超乎你想象