我需要查询维基百科只是为了一个非常特殊的目的,即获取给定网址的文本.更准确一点:
我有大约14.000个英文语料库的维基百科网址,我需要获取文本,或至少引入每个网址.我的进一步处理将在python中,因此这将是首选语言.
我正在寻找具有最佳性能的方法,并制定了4种不同的方法:
sql使用python 查询我应该使用哪种方法,即哪种方法具有最佳性能并且以某种方式标准化?
这是一个非常普遍的问题.我发现了一些关于SO的更具体问题的问题和讨论,但我很确定,你们中的许多人已经解决了这个问题:
输入:
x.题:
我怎样才能得到树的根x?
我发现应该有一种方法来递归地实现它,但我还没有实现它.
有没有办法在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) 我试图正交化 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) 应该输出身份。
如何将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 ×4
numpy ×2
dictionary ×1
elementtree ×1
lxml ×1
orthogonal ×1
postgresql ×1
recursion ×1
sum ×1
wikipedia ×1
xml ×1
xml-parsing ×1