小编lc2*_*817的帖子

为什么Python没有本地链接列表实现?

我曾尝试一些简单的实验机Python列表的性能链表实现,如比较.

本机python列表总是比非本机链表更快(根据理论).

from linkedlist import *
import time
a = LinkedList()
b = []
for i in range(1000000):
    b.append(i)
    a.add_first(i)
t0 = time.clock()
a.remove(10)
t1 = time.clock()
b.remove(10)
t2 = time.clock()
print t1-t0
print t2-t1
Run Code Online (Sandbox Code Playgroud)

我在上面的测试中得到的结果是:

  • 原生链表= 2.00000000001e-05

  • python list = 0.005576

  • 非本地链表= 3.90000000001e-05

所以,我想知道为什么Python没有原生的Linked List数据结构.在Python的情况下,它在我看来,从算法来讲,使用Linked List而不是标准列表来加速标准库的某些方面可能是有用的.

我的理解是List数据结构是该语言的一个关键构建块,它使代码更易于维护,并且易于优化以专注于该数据结构.

还有其他原因吗?

python arrays algorithm linked-list

11
推荐指数
2
解决办法
3905
查看次数

Symfony和机械化

我正在尝试访问使用Symfony框架设计的本地网站.它与Web浏览器和CURL完美配合,但是当我使用Mechanize时,我总是得到401服务器的未授权答案.

import mechanize


# Browser
br = mechanize.Browser()
br.set_debug_http(True)
br.set_debug_redirects(True)
br.set_debug_responses(True)

# Does not change anything even if we change thos
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# Here is my website
r = br.open('http://localhost:8080/frontend_dev.php/home')
html = r.read()

# Show the html source 
print html
Run Code Online (Sandbox Code Playgroud)

你知道它为什么会这样吗?

谢谢

python symfony1 mechanize

7
推荐指数
1
解决办法
382
查看次数

HTTPS Python客户端

我有一个龙卷风服务器提供https连接,我用这种方式生成自签名证书:

openssl genrsa -out privatekey.pem 1024                                         
openssl req -new -key privatekey.pem -out certrequest.csr 
openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
Run Code Online (Sandbox Code Playgroud)

服务器的代码如下:

import tornado.ioloop
import tornado.web
import tornado.httpserver
import os

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        print "new client "+str(self)
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])


http_server = tornado.httpserver.HTTPServer(application,
                                            ssl_options={
        "certfile": os.path.join("./", "certificate.pem"),
        "keyfile": os.path.join("./", "privatekey.pem"),

})

if __name__ == "__main__":
    http_server.listen(443)
    tornado.ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)

我想有一个连接到服务器的python客户端,并检查服务器是否是正确的服务器(我想通过它的证书).目前我做了一个像这样的简单客户:

import httplib
HOSTNAME='localhost'
conn = httplib.HTTPSConnection(HOSTNAME)
conn.putrequest('GET','/')
conn.endheaders()
response = conn.getresponse()
print response.read()
Run Code Online (Sandbox Code Playgroud)

你建议我做什么(客户端稍后将成为一个移动应用程序, …

python https mobile certificate tornado

6
推荐指数
1
解决办法
2052
查看次数

替换字符串中的字符列表

我有一个字符串,它是网页的标题。所以它可以有 < > 和其他特殊字符。

我想编写一个函数来接受一个字符串并替换一个字符列表。试图找到最好的方法来做到这一点。

我应该使用列表或数组或枚举来保存特殊字符的列表,还是在 java 中已经有一些东西可以做到这一点。

filterText(String text, List specialCharecters)

filterText(String text, Array specialCharecters)

filterText(String text, Enum specialCharecters)
Run Code Online (Sandbox Code Playgroud)

java

5
推荐指数
2
解决办法
2万
查看次数

Python:减少元组的元组

我试图在Python中计算从A点到B点通过中间点列表的路径长度.我知道怎么做,但我确实想使用reduce Built-in功能.

为什么我到目前为止尝试过,请注意这是完全错误的,是这样的:

reduce(lambda x,y: math.sqrt((y[1]-y[0])**2+(x[1]-x[0])**2) , ((1,2),(3,4),(1,8)))
Run Code Online (Sandbox Code Playgroud)

任何的想法?

谢谢.

python reduce lambda euclidean-distance

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

将条目添加到HashMap并在for循环中获取对它们的引用

我试图HashMapfor循环中添加多个元素,但似乎无法正确:

use std::collections::HashMap;

fn set_if_needed_and_get(hmap: &mut HashMap<String, String>, st: String) -> &String {
    hmap.entry(st.clone()).or_insert(st.clone())
}

fn main() {
    let meeting_one_email = ["email1", "email2", "email1"];

    let mut hmap: HashMap<String, String> = HashMap::new();
    let mut attendees: std::vec::Vec<&String> = std::vec::Vec::new();

    for m in meeting_one_email.iter() {
        attendees.push(set_if_needed_and_get(&mut hmap, m.to_string()));
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

error[E0499]: cannot borrow `hmap` as mutable more than once at a time
  --> src/main.rs:14:51
   |
14 |         attendees.push(set_if_needed_and_get(&mut hmap, m.to_string()));
   |                                                   ^^^^ mutable borrow starts here in previous …
Run Code Online (Sandbox Code Playgroud)

rust

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

Python:在异常之前使用自定义 sys.excepthook 在上下文中的行号处恢复程序

目标是os在下面的代码中解析依赖项并ls执行命令。

我想知道如何用双星做我在下面发表的评论。

我已经知道我可以通过 traceback

也就是说,我想知道是否可以在给定上下文中的给定行号处恢复程序的执行。

import sys

def new_sys_excepthook(type, value, traceback):
    if type == NameError:
        pass
        # Resolution of the exception finding the module dependency and doing the import
        # ** Returning to the line that created the exception with the right
        # ** context and continue the execution with the module 
        # ** dependency resolved 
    # call original excepthook if we can't solve the issue
    sys.__excepthook__(type, value, traceback) 

sys.excepthook = new_sys_excepthook

system("ls")
Run Code Online (Sandbox Code Playgroud)

python

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