小编mik*_*ike的帖子

有没有办法在django中同时循环两个列表?

我有两个相同长度的列表对象,我希望渲染的补充数据有一种方法可以同时渲染两者,即.

{% for i,j in table, total %} 
 {{ i }} 
 {{ j }}
{% endfor %} 
Run Code Online (Sandbox Code Playgroud)

或类似的东西?

python django django-templates django-views

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

更改龙卷风网站中特定静态文件的mime类型

我在/ static /目录服务器端有一堆文件,名称如下:

Slide0.html    Slide121.html  Slide143.html  Slide165.html  Slide187.html  Slide208.html  
Slide28.html   Slide4.html   Slide71.html  Slide93.html
Slide100.html  Slide122.html  Slide144.html  Slide166.html  Slide188.html  Slide209.html  
Run Code Online (Sandbox Code Playgroud)

我在相同的域中获取它们,并定期将它们插入到iframe中,它们所做的只是渲染一些图像,但浏览器给出以下错误:

Resource interpreted as Image but transferred with MIME type text/html: "http://localhost:8888/static/Slide66.html". 
Run Code Online (Sandbox Code Playgroud)

我试图徒然地将staticfilehandler子类化:

class StaticHandler(tornado.web.StaticFileHandler):

def get(self, path):
    abspath = os.path.abspath(os.path.join(self.root, path))
    mime_type, encoding = mimetypes.guess_type(abspath)
    if mime_type:
        self.set_header("Content-Type", mime_type)

    if 'Slide' in abspath:
        self.set_header('Content-Type',"image/jpg" )
Run Code Online (Sandbox Code Playgroud)

但是错误仍然存​​在,我该如何调整?

python tornado

5
推荐指数
1
解决办法
1426
查看次数

我如何从python中的一组单词中寻找最短的唯一子序列?

如果我有一组类似的单词,例如:

\bigoplus
\bigotimes
\bigskip
\bigsqcup
\biguplus
\bigvee
\bigwedge
...
\zebra
\zeta 
Run Code Online (Sandbox Code Playgroud)

我想找到最短的唯一字母集合,这些字母可以唯一地描述每个单词,即

\bigop:
     \bigoplus
\bigot:
     \bigotimes
\bigsk:
     \bigskip
Run Code Online (Sandbox Code Playgroud)

编辑:请注意,唯一序列标识符始终从单词的开头开始。我编写了一个在输入时提供摘要建议的应用程序。因此,通常用户会从单词的开头开始输入

依此类推,序列仅需足够长即可唯一地描述一个单词。编辑:但是需要从单词的开头开始。表征总是从单词的开头开始。我的想法是:我当时正在考虑对单词进行排序,并根据第一个字母字母进行分组,然后可能使用最长的公共子序列算法来找到最长的公共子序列,并取其长度,并对该唯一的子字符串使用length + 1个字符,但由于我知道最长子序列的算法通常一次只能使用两个参数,因此陷入了困境,而且我每个组中可能有两个以上的单词,以一个特定的字母开头。我要解决已经解决的问题吗?谷歌没有帮助。

string algorithm python-3.3

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

如何在matplotlib中使用下标文本进行注释?

我试图用matplotlib绘制一些数据,我需要一些注释来形成像数学/化学公式.这是我的一些代码.

#!/usr/bin/python2 
import numpy as np
import matplotlib.pyplot as pytl
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
recdt = np.dtype([('compound',str,4),('H_v','f4'),('B_o','f4')]);
gat =  np.loadtxt('tra',dtype=object, usecols=(0,1,2),unpack=True);
gct,ght,gbt=[],[],[]
for c,h,b in np.transpose(gat):
    gct=np.append(gct,c)
    ght=np.append(ght,h)
    gbt=np.append(gbt,b)
ght= ght.astype(np.float)
gbt= gbt.astype(np.float)
hard = pytl
four = hard   #####  
four.scatter(gbt,ght)
hard.title( 'physical stuff' )
hard.xlabel('physical prop 1')
hard.ylabel('physical prop2 ')
for l,x1,y2 in zip ( gct,gbt,ght):
    hard.annotate( l,xy=(x1,y2),xytext=(-24,12),textcoords = 'offset points', arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'),rotation=0 )
hard.ylim([0,10])
hard.savefig('hardcomp.png')
hard.show()
Run Code Online (Sandbox Code Playgroud)

这是一些测试数据

ZrC 6.8 …
Run Code Online (Sandbox Code Playgroud)

python annotations matplotlib

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

如何在mongodb集合中搜索嵌套在数组中的字典键

我在mongodb数据库集合中有一些数据(我知道哪个集合只能查看集合),文档看起来像这样:

{ "_id" : ObjectId("52a93577dadf672b96062729"), 
  "owner" : "user1", 
  "files" : [  
              {  "IyzkmGh4YGD61Tc3TJjaEY17hDldH" : "rws" } 
            ] 
}
{ "_id" : ObjectId("52a92f43dadf672b96062725"), 
  "owner" : "user2", 
  "files" : [    
              {       "tUI7RtvpHZklptvHVYssamlgHP6xs" : "rws" },
              {       "RsxRDQdXmHgmSLhMJ8tPxILxarruK" : "rws" },    
              {       "IyzkmGh4YGD61Tc3TJjaEY17hDldH" : "rw" },      
              {       "YA4to7HaB5Gm6JuZrwYrFit7IzvgO" : "rws" } 
           ] 
}
Run Code Online (Sandbox Code Playgroud)

我试图想办法找到在其中的文件数组中具有特定字典键的所有文档

对于样本数据,如果我使用字符串进行搜索IyzkmGh4YGD61Tc3TJjaEY17hDldH,id就像两个样本一样匹配,因为它们都在其文件列表中有一个带有该键的字典,我将如何执行此查询?我正在使用python电机访问mongodb.原因是我想删除这些条目,当它们代表的文件不再存在时,架构可供我使用,我可以更改它以使这种搜索更加清晰,

python mongodb

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