我在下面有一个字典,我想添加到另一个字典,不一定是不同的元素,并合并它的结果.有没有内置功能,或者我需要自己制作?
{
'6d6e7bf221ae24e07ab90bba4452267b05db7824cd3fd1ea94b2c9a8': 6,
'7c4a462a6ed4a3070b6d78d97c90ac230330603d24a58cafa79caf42': 7,
'9c37bdc9f4750dd7ee2b558d6c06400c921f4d74aabd02ed5b4ddb38': 9,
'd3abb28d5776aef6b728920b5d7ff86fa3a71521a06538d2ad59375a': 15,
'2ca9e1f9cbcd76a5ce1772f9b59995fd32cbcffa8a3b01b5c9c8afc2': 11
}
Run Code Online (Sandbox Code Playgroud)
字典中的元素数量也是未知的.
在合并考虑两个相同的键的情况下,这些键的值应该相加而不是被覆盖.
我找到了一个解决方案,但它确实很慢:
def chunks(self,data, SIZE=10000):
for i in xrange(0, len(data), SIZE):
yield dict(data.items()[i:i+SIZE])
Run Code Online (Sandbox Code Playgroud)
没有使用外部模块(numpy等)你有什么想法吗?
我想更换" 与\"使用Javascript.
我有:
text = text.toString().replace("\"", '\\"')
Run Code Online (Sandbox Code Playgroud)
结果:
\\"
Run Code Online (Sandbox Code Playgroud) 我有以下代码,我不知道如何打印下一页的链接,如何进入下一页?
#!/usr/bin/python2.4
# -*- coding: utf-8 -*-
import pprint
from apiclient.discovery import build
def main():
service = build("customsearch", "v1",
developerKey="")
res = service.cse().list(
q='lectures',
cx='013036536707430787589:_pqjad5hr1a',
num=10, #Valid values are integers between 1 and 10, inclusive.
).execute()
for value in res:
#print value
if 'items' in value:
for results in res[value]:
print results['formattedUrl']
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud) 我为DFS非递归编写了一个解决方案,但是我不能修改它来进行拓扑排序:
def dfs(graph,start):
path = []
stack = [start]
while stack != []:
v = stack.pop()
if v not in path: path.append(v)
for w in reversed(graph[v]):
if w not in path and not w in stack:
stack.append(w)
return path
Run Code Online (Sandbox Code Playgroud)
任何想法如何修改它?
使用递归版本,我可以很容易地进行排序:
def dfs_rec(graph,start,path):
path = path + [start]
for edge in graph[start]:
if edge not in path:
path = dfs_rec(graph, edge,path)
print start
return path
Run Code Online (Sandbox Code Playgroud)
输入:
>>> graph = {
1: [2, 3],
2: [4, 5, 6],
3: [4,6],
4: …Run Code Online (Sandbox Code Playgroud) 我在互联网上找到以下代码来计算TFIDF:
https://github.com/timtrueman/tf-idf/blob/master/tf-idf.py
Run Code Online (Sandbox Code Playgroud)
我在函数def idf(word,documentList)中添加了"1+",所以我不会被0除错:
return math.log(len(documentList) / (1 + float(numDocsContaining(word,documentList))))
Run Code Online (Sandbox Code Playgroud)
但我对两件事感到困惑:
码:
documentNumber = 0
for word in documentList[documentNumber].split(None):
words[word] = tfidf(word,documentList[documentNumber],documentList)
Run Code Online (Sandbox Code Playgroud)
是否应仅在第一份文件上计算TFIDF?
python text-processing information-retrieval data-mining tf-idf
我有一个英语语言的工作模式,但由于我的母语不起作用,这让我很头疼.首先,我打开了许多关于编码的问题,我知道我低估了它,这是一个大问题.我花了一些时间阅读它,问题仍然存在.所以现在我正面临一个正则表达问题.所以模式是:
exactMatch = re.compile(r"([^\.]*\b???????\b[^\.]*)\.", re.UNICODE)
print exactMatch.pattern
result= exactMatch.findall("??????? ? ?? ????? ?? ????????????. ??????? ? ?? ????? ?? ????????????.")
Run Code Online (Sandbox Code Playgroud)
它适用于英语.它的功能是给我一个段落中的所有句子.那有什么建议吗?
我也试过编码和解码,但注意到编码错误除外.
任何人都知道如何按密钥长度对这本字典进行排序?
{
'http://ccc.com/viewvc/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.14'}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}],
'http://bbb.com/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.22'}, {'type': 'programming-languages', 'app': 'PHP', 'ver': '5.3.10'}, {'type': 'cms', 'app': 'Drupal', 'ver': None}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}, {'type': 'javascript-frameworks', 'app': 'jQuery', 'ver': None}, {'type': 'captchas', 'app': 'Mollom', 'ver': None}]
}
Run Code Online (Sandbox Code Playgroud)
预期产量:
{
'http://bbb.com/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.22'}, {'type': 'programming-languages', 'app': 'PHP', 'ver': '5.3.10'}, {'type': 'cms', 'app': 'Drupal', 'ver': None}, {'type': 'operating-systems', 'app': …Run Code Online (Sandbox Code Playgroud) 因此,当我用我的母语在mod_python中发布名称或文本时,我得到:
македонија
Run Code Online (Sandbox Code Playgroud)
我也得到:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
当我使用:
hparser = HTMLParser.HTMLParser()
req.write(hparser.unescape(text))
Run Code Online (Sandbox Code Playgroud)
我怎么解码呢?
我有:
TYPO3 4.2 is installed on machine ...
Winamp is installed on machine ...
Winrar 3.20 is installed on machine ...
Run Code Online (Sandbox Code Playgroud)
我如何制作一个正则表达式来分隔句子中的软件包名称.上面有一个软件\版本的示例,但句子并不总是相同,有时也没有显示版本.任何提示怎么样?我找到了这个主题,但它仅适用于版本:版本号的正则表达式
当我读到一些评论时,我忘记了一些像:
软件版本没有标准格式,但它是点分隔的
软件名称在版本之前
在模拟Colatz猜想问题时我已经进行了递归,当我想在递归中打印计数时我得到了我需要的结果但是当函数返回结果时它给了我奇怪的数字,为什么呢?
#include <stdio.h>
#include <stdlib.h>
int divide(int n,int count){
if(n<=1){printf("%d ",count);return count;}
if(n%2==1){divide(n=3*n+1, ++count);}
else{divide(n/=2, ++count);}
}
int main(void) {
printf("%d ",divide(10,1));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想知道为什么for循环没有扩展它的迭代:
for link in frontLinks:
#try:
getCurlink = self.getHref(link) # get current site links
#print getCurlink
#print frontLinks
if getCurlink:
frontLinks = frontLinks + getCurlink
Run Code Online (Sandbox Code Playgroud)
这一行:
frontLinks = frontLinks + getCurlink
Run Code Online (Sandbox Code Playgroud)
不适用于"for"循环的frontLinks.有任何想法吗??
python ×10
dictionary ×3
regex ×2
algorithm ×1
c ×1
data-mining ×1
graph ×1
javascript ×1
mod-python ×1
python-2.7 ×1
sorting ×1
tf-idf ×1