小编Mil*_*ell的帖子

python查询维基百科的表现

我需要查询维基百科只是为了一个非常特殊的目的,即获取给定网址的文本.更准确一点:

我有大约14.000个英文语料库的维基百科网址,我需要获取文本,或至少引入每个网址.我的进一步处理将在python中,因此这将是首选语言.

我正在寻找具有最佳性能的方法,并制定了4种不同的方法:

  1. 通过python直接获取xml转储和解析
    - >这里的进一步问题是:如何查询xml文件,知道url?
  2. 获取xml,设置数据库并sql使用python 查询
    - >这里的进一步问题是:如何查询sql,知道url?
  3. 使用维基百科api并通过python直接查询
  4. 只是抓取这些维基百科页面(这可能是一种偷偷摸摸的,也很烦人,因为它的HTML和没有纯文本)

我应该使用哪种方法,即哪种方法具有最佳性能并且以某种方式标准化?

python wikipedia

4
推荐指数
1
解决办法
909
查看次数

CTE查询根元素postgres

这是一个非常普遍的问题.我发现了一些关于SO的更具体问题的问题和讨论,但我很确定,你们中的许多人已经解决了这个问题:

输入:

  1. 在一个字段中具有树结构的表.
  2. 数据库记录的任意id x.

题:

我怎样才能得到树的根x

我发现应该有一种方法来递归地实现它,但我还没有实现它.

postgresql recursion common-table-expression

4
推荐指数
1
解决办法
1740
查看次数

除了一个之外,在python dict中对值进行求和

有没有办法在python dict中对所有值求和,除了使用选择器

>>> x = dict(a=1, b=2, c=3)
>>> np.sum(x.values())
6
Run Code Online (Sandbox Code Playgroud)

?我目前的解决方案是基于循环的解决方案

>>> x = dict(a=1, b=2, c=3)
>>> y = 0
>>> for i in x:
...     if 'a' != i:
...             y += x[i]
... 
>>> y
5
Run Code Online (Sandbox Code Playgroud)

编辑:

import numpy as np
from scipy.sparse import *
x = dict(a=csr_matrix(np.array([1,0,0,0,0,0,0,0,0]).reshape(3,3)),      b=csr_matrix(np.array([0,0,0,0,0,0,0,0,1]).reshape(3,3)), c=csr_matrix(np.array([0,0,0,0,0,0,0,0,1]).reshape(3,3)))
y = csr_matrix((3,3))
for i in x: 
    if 'a' != i:
        y = y + x[i]
print y
Run Code Online (Sandbox Code Playgroud)

回报 (2, 2) 2.0

print np.sum(value for …
Run Code Online (Sandbox Code Playgroud)

python dictionary numpy sum

3
推荐指数
1
解决办法
3063
查看次数

正交化矩阵 numpy

我试图正交化 a2d-numpy array并失败了。我使用了这种方法并将其转换为以下代码:

def sym(w):
    return w.dot((w.T.dot(w))**(-.5))
Run Code Online (Sandbox Code Playgroud)

In [1]: a
Out[2]: 
array([[ 1.1,  0.1],
       [ 0.1,  1.1]])
In [3]: a = sym(a)

In [4]: a
Out[5]: 
array([[ 1.20909392,  2.43574363],
       [ 2.43574363,  1.20909392]])

In [6]: a.dot(a.T)
Out[7]: 
array([[ 7.39475513,  5.89008563],
       [ 5.89008563,  7.39475513]])
Run Code Online (Sandbox Code Playgroud)

a.dot(a.T) 应该输出身份。

python numpy orthogonal

3
推荐指数
1
解决办法
5581
查看次数

lxml etree 获取元素之前的所有文本

如何将etree 中某个元素之前的所有文本与该元素之后的文本分开?

from lxml import etree

tree = etree.fromstring('''
    <a>
        find
        <b>
            the
        </b>
        text
        <dd></dd>
        <c>
            before
        </c>
        <dd></dd>
        and after
    </a>
''')
Run Code Online (Sandbox Code Playgroud)

我想要什么?在此示例中,<dd>标签是分隔符,并且对于所有标签

for el in tree.findall('.//dd'):
Run Code Online (Sandbox Code Playgroud)

我想要它们之前和之后的所有文本:

[
    {
        el : <Element dd at 0xsomedistinctadress>,
        before : 'find the text',
        after : 'before and after'
    },
    {
        el : <Element dd at 0xsomeotherdistinctadress>,
        before : 'find the text before',
        after : 'and after'
    }
]
Run Code Online (Sandbox Code Playgroud)

我的想法是在树中使用某种占位符,用它替换标签<dd>,然后在该占位符处剪切字符串,但我需要与实际元素的对应关系。

python xml lxml elementtree xml-parsing

2
推荐指数
1
解决办法
1467
查看次数