Speed of calculating powers (in python)

I'm curious as to why it's so much faster to multiply than to take powers in python (though from what I've read this may well be true in many other languages too). For example it's much faster to do x*x than x**2 I suppose the ** operator is more general and can also deal with fractional powers. But if that's why it's so much slower, why doesn't it perform a chec

计算能力的速度(在python中)

我很好奇,为什么它乘法的速度要快于在python中的权力(尽管从我读过的内容来看,这在许多其他语言中也是如此)。 例如,它要快得多 x*x 比 x**2 我认为**运算符更一般,也可以处理分数的权力。 但是,如果这就是为什么它慢得多,为什么它不执行一个int指数检查,然后只是做乘法? 编辑:这是我尝试的一些示例代码... def pow1(r, n): for i in range(r): p = i**n def pow2(r, n): for i in range(r): p =

How to validate / verify an X509 Certificate chain of trust in Python?

I am working on implementing a web application that utilizes an API. During a response, the API server sends over a link to an X509 certificate (in PEM format, composed of a signing certificate and one or more intermediate certificates to a root CA certificate ) that I must download and use to do further verification. Before using the certificate, I need to ensure that all certificates in the

如何验证/验证Python中的X509证书信任链?

我正在致力于实现一个使用API​​的Web应用程序。 在响应期间,API服务器通过链接发送X509证书(PEM格式,由签名证书和一个或多个中间证书组成的根证书),我必须下载该证书并使用该证书进行进一步验证。 在使用证书之前,我需要确保链中的所有证书组合在一起,为受信任的根CA证书创建一个信任链(以检测并避免任何恶意请求)。 我在python中很难做到这一点,我对这个主题的研究没有产生任何有用的东西。 证书很容易通过请求

How to check if type of a variable is string?

Is there a way to check if the type of a variable in python is string.. like isinstance(x,int); for integer values? In Python 2.x, you would do isinstance(s, basestring) basestring is the abstract superclass of str and unicode . It can be used to test whether an object is an instance of str or unicode . In Python 3.x, the correct test is isinstance(s, str) The bytes class isn't con

如何检查变量的类型是否是字符串?

有没有办法检查python中的变量的类型是否是字符串 isinstance(x,int); 整数值? 在Python 2.x中,你会这样做 isinstance(s, basestring) basestring是str和unicode的抽象超类。 它可以用来测试一个对象是一个str还是unicode的实例。 在Python 3.x中,正确的测试是 isinstance(s, str) 在Python 3中, bytes类不被视为字符串类型。 我知道这是一个古老的话题,但作为第一个显示在谷歌上,并且我没有找到任何令人满意

How to get string objects instead of Unicode from JSON?

I'm using Python 2 to parse JSON from ASCII encoded text files. When loading these files with either json or simplejson , all my string values are cast to Unicode objects instead of string objects. The problem is, I have to use the data with some libraries that only accept string objects. I can't change the libraries nor update them. Is it possible to get string objects instead of U

如何从JSON获取字符串对象而不是Unicode?

我使用Python 2从ASCII编码的文本文件中解析JSON。 当用json或simplejson加载这些文件时,我所有的字符串值都被转换为Unicode对象而不是字符串对象。 问题是,我必须将数据与一些只接受字符串对象的库一起使用。 我无法更改库或更新它们。 是否可以获取字符串对象而不是Unicode字符串? 例 >>> import json >>> original_list = ['a', 'b'] >>> json_list = json.dumps(original_list) >>> json_list '["a", "b"]' >>> n

Python, Unicode, and the Windows console

When I try to print a Unicode string in a Windows console, I get a UnicodeEncodeError: 'charmap' codec can't encode character .... error. I assume this is because the Windows console does not accept Unicode-only characters. What's the best way around this? Is there any way I can make Python automatically print a ? instead of failing in this situation? Edit: I'm using Pyt

Python,Unicode和Windows控制台

当我尝试在Windows控制台中打印Unicode字符串时,出现UnicodeEncodeError: 'charmap' codec can't encode character ....错误。 我认为这是因为Windows控制台不接受仅Unicode字符。 最好的解决办法是什么? 有什么办法可以让Python自动打印? 而不是在这种情况下失败? 编辑:我正在使用Python 2.5。 注意: @ LasseV.Karlsen的答案与复选标记有些过时(从2008年开始)。 请谨慎使用下面的解决方案/答案/建

Shuffling a list of objects

I have a list of objects in Python and I want to shuffle them. I thought I could use the random.shuffle method, but this seems to fail when the list is of objects. Is there a method for shuffling object or another way around this? import random class a: foo = "bar" a1 = a() a2 = a() b = [a1,a2] print random.shuffle(b) This will fail. random.shuffle should work. Here's an example

洗牌对象列表

我有一个在Python中的对象列表,我想打乱他们。 我想我可以使用random.shuffle方法,但是当列表是对象时,这似乎失败了。 有没有一种方法来洗牌或其他方式? import random class a: foo = "bar" a1 = a() a2 = a() b = [a1,a2] print random.shuffle(b) 这将失败。 random.shuffle应该可以工作。 以下是一个示例,其中的对象是列表: from random import shuffle x = [[i] for i in range(10)] shuffle(x) # pri

moving a point along a circular path via mouse motion (python/pygame)

The problem: move a point along a circular path with mouse input as a guide. There are plenty of solutions online for moving a point along a circular path, namely using the center + math.sin||cos(angle) * radius equation and incrementing the angle. This doesn't work with mouse input, however; even at a fraction of the original x/y value and converted to radians your "circle" has

通过鼠标移动沿圆形路径移动一个点(python / pygame)

问题:使用鼠标输入沿圆形路径移动点作为指导。 在线上有很多解决方案可以沿着圆形路径移动一个点,即使用中心+数学公式|| cos(角度)*半径方程并递增角度。 但是,这不适用于鼠标输入。 即使只是原始x / y值的一小部分,并转换为弧度,“圆”的半径也不会变化,路径点与鼠标位置之间的关系变得异常。 我的想法是,路径点(圆的圆周上的点)需要被限制在距原点一定距离处(强制半径)。 我也感觉到,要么y需要是x或x的函数

Polygon area, perimeter and side length around the circle with python

I have functions to calculate area, perimeter and side of the polygon inscribed on circle, but I'd like to find out similar general way to calculate same properties of the polygons drawn around the circle. # Area of an equal sided polygon with given radius and number of sides def polygon_area(r, n): return ((n*pow(r, 2))/2)*sin(2*pi/n) # Perimeter of an equal sided polygon with given ra

蟒蛇圈周围的多边形区域,周长和边长

我有函数来计算圆上刻的多边形的面积,周长和边,但我想找出类似的一般方法来计算围绕圆周绘制的多边形的相同属性。 # Area of an equal sided polygon with given radius and number of sides def polygon_area(r, n): return ((n*pow(r, 2))/2)*sin(2*pi/n) # Perimeter of an equal sided polygon with given radius and number of sides def polygon_perimeter(r, n): return 2*n*r*sin(pi/n) # Side length of an

how can I send raw packets over wifi without a network?

I am trying to send some data between 2 raspberry pi's over wifi without them being connected to a network. I want it to work similar to the way beacons and probe requests work, where a broadcast can be sent out the wireless interface. the data I want to send will be the hostname of the device and the time the packet was sent. I've been trying to figure this out for the last few day

如何在没有网络的情况下通过WiFi发送原始数据包?

我正在尝试通过无线网络传送2个覆盆子pi之间的数据,而无需连接到网络。 我希望它的工作方式类似于信标和探测请求的工作方式,可以在无线接口上发送广播。 我想发送的数据将是设备的主机名和数据包发送的时间。 过去几天我一直在试图弄清楚这一点,但是如果没有他们在同一个网络上,我就无法获得任何工作。 有人能指出我正确的方向吗? 我对我使用的语言不太感兴趣。 我一直在尝试python和C,并没有取得成功。 注意

Finding an unknown point using weighted multilateration

I have a series of points (latitude/longitude coordinates) on the earth and a series of distance estimates from each point to an unknown location. I would like to use multilateration to estimate the location of this unknown location. Starting with a simple example imagine 4 points and associated distance estimates to an unknown point with unknown location: latitude, longitude, distance estima

使用加权多边测量找到未知点

我在地球上有一系列的点(纬度/经度坐标),以及从每个点到未知位置的一系列距离估计值。 我想用多边测量来估计这个未知位置的位置。 从一个简单的例子开始,设想4点和相关距离估计到未知位置的未知点: 纬度,经度,距离估计3元组以下: p1 = (31.2297, 121.4734, 3335.65) p2 = (34.539, 69.171, 2477.17) p3 = (47.907, 106.91, 1719.65) p4