python3.x - python二维数组
PHP中文网
PHP中文网 2017-04-18 10:21:13
0
2
562
 texts = [[word for word in document.lower().split()] for document in documents]

我在网址我爱自然语言处理-如何计算两个文档的相似度(二)中看到下面一份代码。
对于>>> texts = [[word for word in document.lower().split()] for document in documents]的含义不是很理解。

>>>documents = ["Shipment of gold damaged in a fire",
... "Delivery of silver arrived in a silver truck",
... "Shipment of gold arrived in a truck"]
>>> texts = [[word for word in document.lower().split()] for document in documents]
>>> print texts
[['shipment', 'of', 'gold', 'damaged', 'in', 'a', 'fire'], ['delivery', 'of', 'silver', 'arrived', 'in', 'a', 'silver', 'truck'], ['shipment', 'of', 'gold', 'arrived', 'in', 'a', 'truck']]

对于一般的for var in list:这种形式,我是知道的。但是上面的那种二维数组,我就不是很理解为什么了。求助,帮忙分析一下

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
巴扎黑

This syntax is called "List Comprehensions"
First go through the examples in the https://docs.python.org/2/tut...
document and you will understand what is going on.

左手右手慢动作

How to create a two-dimensional array in python
For example, create a 3*3 array
Method 1 Direct definition

[py]matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]][/py]

Method 2 Indirect definition

matrix = [[0 for i in range(3)] for i in range(3)]

It’s just a method. .lower().split() is to process the words in the file, uppercase and lowercase, and split them.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template