Maison  >  Questions et réponses  >  le corps du texte

python3.x - python二维数组

 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中文网2740 Il y a quelques jours535

répondre à tous(2)je répondrai

  • 巴扎黑

    巴扎黑2017-04-18 10:23:13

    Cette syntaxe s'appelle "List Comprehensions"
    Parcourez d'abord les exemples du https://docs.python.org/2/tut...
    document, et vous comprendrez ce qui se passe .

    répondre
    0
  • ringa_lee

    ringa_lee2017-04-18 10:23:13

    Comment créer un tableau bidimensionnel en python
    Par exemple, créer un tableau 3*3
    Méthode 1 Définition directe

    [py]matrice = [[0, 0, 0], [0, 0, 0], [0, 0, 0]][/py]

    Méthode 2 Définition indirecte

    matrice = [[0 pour i dans la plage(3)] pour i dans la plage(3)]

    C'est juste une méthode. .lower().split() consiste à traiter les mots du fichier, en majuscules et en minuscules, et à les séparer.

    répondre
    0
  • Annulerrépondre