当前位置: 首页 > news >正文

python如何打印表_Python中的打印表

将列表中的每个项与(另一个或同一个)列表中的每个项进行比较的过程在数学上称为Cartesian product。Python有一个内置函数来完成这个任务:itertools.product它相当于嵌套for循环:

假设A和B是列表:for x in A:

for y in B:

print (x,y)

或者,更简洁地说:from itertools import product

for pair in product(A, B):

print pair

在您的例子中,您将一个列表的所有项与其自身进行比较,因此您可以编写product(texts, texts),但是product在本例中有可选的关键字参数repeat:product(A, repeat=4)的意思与product(A, A, A, A)相同。在

现在可以这样重写代码:from itertools import product

caesar = """BOOK I

I.--All Gaul is divided into three parts, one of which the Belgae

inhabit, the Aquitani another, those who in their own language are

called Celts, in ours Gauls, the third. All these differ from each other

in language, customs and laws."""

hamlet = """Who's there?"

"Nay, answer me. Stand and unfold yourself."

"Long live the King!"

"Barnardo!"

"He." (I.i.1-5)"""

macbeth = """ACT I SCENE I A desert place. Thunder and lightning.

[Thunder and lightning. Enter three Witches]

First Witch When shall we three meet again

In thunder, lightning, or in rain?

Second Witch When the hurlyburly's done,

When the battle's lost and won."""

texts = [caesar, hamlet, macbeth]

def similarity(x, y):

"""similarity based on length of the text,

substitute with similarity function from Natural Language Toolkit"""

return float(len(x))/len(y)

for pair in product(texts, repeat=2):

print "{}".format(similarity(*pair))

相关文章:

  • python 傅里叶滤波_Python_opencv_傅里叶变换
  • hbuilderx如何导入样式_HBuilderX使用Vant组件库
  • 为什么nodejs是单进程的_Nodejs探秘:深入理解单线程实现高并发原理
  • python实现相空间重构_用非平衡零差法实现wigner函数的简单测量
  • list stream 最大和最小值_Java 8 Stream 求集合元素每个属性的最大值
  • sap增加事务代码权限_sap 事务代码跳过权限检查
  • r语言remarkdown展示图_Rmarkdown用法与R语言动态报告
  • jsonpath 判断是否包含_JsonNode findPath方法详解 JsonNode判断是否为空详解
  • php 实现mqtt 订阅监听功能出错_Redis发布订阅原理以及java实现
  • 前端工程师需要学习ps 吗_前端人员一定要掌握的PS技巧
  • 节能证书在哪里查询_证书查询
  • python 检查域名是否可以访问_python检查URL是否能正常访问
  • vue列表渲染中key的作用_React / Vue 项目时为什么要在列表组件中写 key,其作用是什么?...
  • vuepress侧边栏配置_侧边栏(sidebar) - VuePress 中文文档
  • python编写性别比例_python实现爬虫统计学校BBS男女比例(一)
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • 「面试题」如何实现一个圣杯布局?
  • 【Amaple教程】5. 插件
  • java第三方包学习之lombok
  • JS笔记四:作用域、变量(函数)提升
  • Netty源码解析1-Buffer
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • Redis中的lru算法实现
  • vue-loader 源码解析系列之 selector
  • windows下mongoDB的环境配置
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 基于axios的vue插件,让http请求更简单
  • 前端存储 - localStorage
  • 前端面试题总结
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 详解NodeJs流之一
  • linux 淘宝开源监控工具tsar
  • # Java NIO(一)FileChannel
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • #pragma预处理命令
  • (13):Silverlight 2 数据与通信之WebRequest
  • (14)目标检测_SSD训练代码基于pytorch搭建代码
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (附源码)springboot 个人网页的网站 毕业设计031623
  • (附源码)springboot教学评价 毕业设计 641310
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (四) 虚拟摄像头vivi体验
  • (一)WLAN定义和基本架构转
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (转)编辑寄语:因为爱心,所以美丽
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • .NET C#版本和.NET版本以及VS版本的对应关系
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • .net 写了一个支持重试、熔断和超时策略的 HttpClient 实例池
  • .NET/C# 项目如何优雅地设置条件编译符号?
  • .net访问oracle数据库性能问题
  • [ 攻防演练演示篇 ] 利用通达OA 文件上传漏洞上传webshell获取主机权限
  • [20170713] 无法访问SQL Server
  • [20180224]expdp query 写法问题.txt
  • [C/C++]关于C++11中的std::move和std::forward