我尝试了"heapq"并得出结论,我的期望与我在屏幕上看到的不同.我需要有人解释它是如何工作的以及它在哪里有用.
从第2.2 节中的本周Python模块一书中可以看出
如果在添加和删除值时需要维护排序列表,请查看heapq.通过使用heapq中的函数来添加或删除列表中的项,您可以以较低的开销维护列表的排序顺序.
这就是我所做的和得到的.
import heapq
heap = []
for i in range(10):
heap.append(i)
heap
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
heapq.heapify(heap)
heapq.heappush(heap, 10)
heap
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
heapq.heappop(heap)
0
heap
[1, 3, 2, 7, 4, 5, 6, 10, 8, 9] <<< Why the list does not remain sorted?
heapq.heappushpop(heap, 11)
1
heap
[2, 3, 5, 7, 4, 11, 6, 10, 8, 9] …
Run Code Online (Sandbox Code Playgroud) 我有以下json文档
// json.json
[
{
"title":"title1",
"value":12234
},
{
"title":"title2",
"value":"some text"
},
{
"title":"title3",
"value":"12qwerty234"
},
{
"title":"title4",
"value":123.5
}
]
Run Code Online (Sandbox Code Playgroud)
我正在使用jQuery来加载它.这是代码:
$(document).ready(function(){
$.getJSON("json.json", {},function(result){
$.each(result, function(i, obj) {
$("form").append($('<label for="'+i+'">'+obj.title+'</label>'));
$("form").append($('<input id="'+i+'" value="'+obj.value+'" type="text"/><br>'));
});
});
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,我在Firefox中遇到语法错误.我加载json.json
为本地文件.这是一个截图(错误显示"第1行的语法错误")
请注意,该表单已成功生成.
编辑:
这是运行python时Chrome的另一个屏幕截图SimpleHTTPServer
:
>>> def hehe():
... return "spam"
...
>>> repr(hehe)
'<function hehe at 0x7fe5624e29b0>'
Run Code Online (Sandbox Code Playgroud)
我希望有:
>>> repr(hehe)
'hehe function created by awesome programmer'
Run Code Online (Sandbox Code Playgroud)
我怎么做?把__repr__
内部hehe
功能不起作用.
编辑:
如果你们想知道我为什么要这样做:
>>> defaultdict(hehe)
defaultdict(<function hehe at 0x7f0e0e252280>, {})
Run Code Online (Sandbox Code Playgroud)
我只是不喜欢它在这里显示的方式.
我目前使用flask、sqlalchemy 和jinja2 构建一个Web 应用程序。
为了获得合适的 Web 界面,我按如下方式构建视图:
@app.route('/mydata/', methods=['GET'])
@login_required
def mydata_list():
# build data here...
return render_template('mydata/index.html', data=data))
Run Code Online (Sandbox Code Playgroud)
现在,如果我需要构建一个 REST API,我应该以
return jsonify(data)
Run Code Online (Sandbox Code Playgroud)
那么,如何处理这个以避免代码重复呢??api=True
在我的 url 中添加一个,在我的视图中测试它,然后返回适当的答案是一个好习惯吗?
我如何通过Django中的多个外键旅行?我已经尝试过django docs中我能想到的一切,但我显然错过了一些东西(极端新手).我有科学家,实验和理论的模型.
如果我想看一个特定的理论(我们称之为'相对论')并获得一份科学家所有电子邮件的清单(保存在普通的django用户模型中),我该怎么做?
class Experiment(models.Model)
experimenter = models.ForeignKey(Scientist)
theory = models.ForeignKey(Theory)
class Theory(models.Model)
name = models.CharField(max_length=100)
class Scientist(models.Model)
user = models.ForeignKey(User, unique=True)
institution = models.CharField(max_length=20, null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)
这些是我重写的模型的简化版本,因此可能存在一些错误,但关系是正确的.
我已经尝试了select_related(),get(),filter()的各种组合,但无法弄明白.在此先感谢您的帮助!
考虑以下Python脚本:
from lxml import etree
html = '''
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<p>This is some text followed with 2 citations.<span class="footnote">1</span>
<span ?lass="footnote">2</span>This is some more text.</p>
</body>
</html>'''
tree = etree.fromstring(html)
for element in tree.findall(".//{*}span"):
if element.get("class") == 'footnote':
print(etree.tostring(element, encoding="unicode", pretty_print=True))
Run Code Online (Sandbox Code Playgroud)
所需的输出将是2个span
元素,而是得到:
<span xmlns="http://www.w3.org/1999/xhtml" class="footnote">1</span>
<span xmlns="http://www.w3.org/1999/xhtml" class="footnote">2</span>This is some more text.
Run Code Online (Sandbox Code Playgroud)
为什么在元素之后直到父元素的末尾都包含文本?
我正在尝试使用lxml链接脚注,当我a.insert()
将span
元素添加到a
为其创建的元素中时,它包含之后的文本,因此链接了许多我不想链接的文本。
我有一个包含文件夹的zip文件,如下所示:
some.zip/
some_folder/
some.xml
...
Run Code Online (Sandbox Code Playgroud)
我正在使用zipfile
图书馆.我要的是只开some.xml文件,但我不知道现在的some_folder名.我的解决方案如下所示:
def get_xml(zip_file):
for filename in zip_file.namelist():
if filename.endswith('some.xml'):
return zip_file.open(filename)
Run Code Online (Sandbox Code Playgroud)
我想知道除了扫描整个列表之外是否还有更好的解决方案.