将列表中的每个项与(另一个或同一个)列表中的每个项进行比较的过程在数学上称为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中的打印表相关推荐

  1. python实践输出星星_python中怎么打印星星

    for循环: 1.有大括号与无大括号的区别: 循环体只能有一条语句, 如果需要执行的语句超过一条,就要加大括号将它们变为一条复合语句.//for循环如此,while循环也是如此. for(int i= ...

  2. python显示变量值_Python 中如何打印变量值

    展开全部 python打印变量的值需32313133353236313431303231363533e78988e69d8331333366306435要用到print语句,具体的使用方法如下: 1. ...

  3. python 日志输出变量_python中记录打印的log模块logging的用法实例

    日志基础教程 日志是对软件执行时所发生事件的一种追踪方式.软件开发人员对他们的代码添加日志调用,借此来指示某事件的发生.一个事件通过一些包含变量数据的描述信息来描述(比如:每个事件发生时的数据都是不同 ...

  4. python算法和数据结构_Python中的数据结构和算法

    python算法和数据结构 To 至 Leonardo da Vinci 达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this ar ...

  5. oracle删除表空间中的表,ORACLE删除表空间中的所有表

    ORACLE删除表空间中的所有表,如果是非sysdbA连接用如下sql语句 declare vsql varchar2(200); cursor c1 is select 'drop table '| ...

  6. python中空格怎么打_Python中如何打印空行

    Python中代码比较长时,想在Pycharm-console控制台或Python自带IDLE的Shell窗口查看代码执行结果,打印一些字符之类的,很多行之间没有换行影响查看效果,那么,Python中 ...

  7. python打印星星居中_python中怎么打印星星

    for循环: 1.有大括号与无大括号的区别: 循环体只能有一条语句, 如果需要执行的语句超过一条,就要加大括号将它们变为一条复合语句.//for循环如此,while循环也是如此. for(int i= ...

  8. python for循环九九乘法表_Python中的九九乘法表(for循环)

    用for循环写出的九九乘法表(包括函数的调用) #方向一 for i in range(1,10): for j in range(1,i+1): d = i * j print('%d*%d=%-2 ...

  9. python list遍历删除_Python中list循环遍历删除数据的正确方法

    前言 初学Python,遇到过这样的问题,在遍历list的时候,删除符合条件的数据,可是总是报异常,代码如下: num_list = [1, 2, 3, 4, 5] print(num_list) f ...

最新文章

  1. shell实现批量在多台windows服务器上执行同一命令并获取返回结果
  2. 阑夕-王坚:说服马云之后,还要说服人类
  3. CCF201604-3 路径解析(100分)
  4. vue 心跳监控_vue websocket 加心跳
  5. vc6.0转vs2005中字符串的问题
  6. camel apache_Apache Camel 3 –新增功能前10名
  7. jmespath查找JSON的工具
  8. python的变量对大小写并不敏感_Python编程基础02:Python基本语法
  9. 初探 Go 的编译命令执行过程
  10. 【科普】你所不了解的SWF文件
  11. 循迹小车c语言程序51单片机,51单片机循迹小车Proteus仿真程序
  12. qpsk matlab仿真,qpsk的matlab和simulink仿真.pdf
  13. cdrx4自动排版步骤_教你用CorelDRAW X4轻松排出干净整齐段落
  14. python报错:ValueError: invalid literal for int() with base 16: ‘ ‘
  15. 微信下单小程序怎么做(下单小程序功能介绍)
  16. 7-4 愿天下有情人都是失散多年的兄妹 (25 分)(第十二周编程题)
  17. 【微信机器人】实现保存微信表情包到相册
  18. 数据库---[复习2]---数据查询---设有一个SPJ数据库,包括S、P、J及SPJ4个关系模式··· ···
  19. 2.3Linux中的日志管理
  20. linux中项目常用的start.sh和stop.sh

热门文章

  1. java mysql数据库统计报表系统
  2. CES2018前瞻:在未来 我们需要关注这些领域
  3. mpg格式转换为mp4格式
  4. pdf在线转word文档地址
  5. Live Home 3D Pro - 用于公寓和房屋的室内设计,支持 3D 实时渲染
  6. SEM优化教程第1讲—SEM是什么?SEM与SEO的区别?
  7. 图解 DataX 核心设计原理
  8. MyBatis内容分析
  9. PL/SQL中快捷键、内容提示、字体大小的设置
  10. 无障碍辅助功能(精读React官方文档—13)