我编写了我的第一个稍微复杂的算法,即A Star Pathfinding算法的实现.我遵循了一些关于实现图形的Python.org建议,因此字典包含每个节点都链接的所有节点.现在,因为这是游戏的全部,每个节点实际上只是节点网格中的一个区块,因此我如何计算启发式和偶尔引用它们.
感谢timeit我知道我可以成功运行这个功能一秒钟一百多次.可以理解这让我有点不安,这是没有任何其他'游戏的东西'继续下去,如图形或计算游戏逻辑.所以我很想知道你们中是否有人可以加速我的算法,我完全不熟悉Cython或者它的亲属,我不能编写一行C.
没有更多的漫步,这是我的A Star功能.
def aStar(self, graph, current, end):
openList = []
closedList = []
path = []
def retracePath(c):
path.insert(0,c)
if c.parent == None:
return
retracePath(c.parent)
openList.append(current)
while len(openList) is not 0:
current = min(openList, key=lambda inst:inst.H)
if current == end:
return retracePath(current)
openList.remove(current)
closedList.append(current)
for tile in graph[current]:
if tile not in closedList:
tile.H = (abs(end.x-tile.x)+abs(end.y-tile.y))*10
if tile not in openList:
openList.append(tile)
tile.parent = current
return path
Run Code Online (Sandbox Code Playgroud) if (message.value[0] == "/" or message.value[0] == "\"):
do stuff.
Run Code Online (Sandbox Code Playgroud)
我确定这是一个简单的语法错误,但这个if语句有问题.
尝试在运行Ubuntu Server的小型VPS上安装Cython.难道
sudo apt-get install gcc
Run Code Online (Sandbox Code Playgroud)
然后
python setup.py install
Run Code Online (Sandbox Code Playgroud)
在Cython目录中,但我得到了这个特殊的错误.
running install
running build
running build_py
running build_ext
building 'Cython.Plex.Scanners' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c Cython/Plex/Scanners.c -o build/temp.linux-x86_64-2.6/Cython/Plex/Scanners.o
Cython/Plex/Scanners.c:4:20: error: Python.h: No such file or directory
Cython/Plex/Scanners.c:6:6: error: #error Python headers needed to compile C extensions, please install development version of Python.
error: command 'gcc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)
我为什么需要'Python的开发版'?运行Python 2.6.5(r265:79063,2010年4月16日,13:57:41).谢谢!
所以我试图根据宽度和高度在Python中生成嵌套列表.这是我到目前为止:
width = 4
height = 5
row = [None]*width
map = [row]*height
Run Code Online (Sandbox Code Playgroud)
现在,这显然不太正确.打印时看起来很好:
[[None, None, None, None],
[None, None, None, None],
[None, None, None, None],
[None, None, None, None],
[None, None, None, None]]
Run Code Online (Sandbox Code Playgroud)
但是尝试将值分配给这样的位置:
map[2][3] = 'foo'
Run Code Online (Sandbox Code Playgroud)
我明白了:
[[None, None, None, 'foo'],
[None, None, None, 'foo'],
[None, None, None, 'foo'],
[None, None, None, 'foo'],
[None, None, None, 'foo']]
Run Code Online (Sandbox Code Playgroud)
很明显,这是因为每个子列表实际上只是引用相同的对象,行,所以更改一个,更改它们.所以这是我最接近的!
如何动态生成嵌套列表?谢谢!
我正在搞乱机器学习,我在Python中编写了一个K Means算法实现.它采用二维数据并将它们组织成簇.每个数据点的类值也为0或1.
让我对算法感到困惑的是我如何使用它来预测另一组二维数据的某些值,这些数据没有0或1,而是未知的.对于每个群集,我应该将其中的点平均为0还是1,如果未知点最接近该群集,那么该未知点将采用平均值?或者有更聪明的方法吗?
干杯!
我正在努力尝试将一个透明背景粘贴在另一个图像上,同时使用透明背景,使用正确的alpha /颜色混合.
以下是一些示例图像,red.png和blue.png:
我想在red.png上粘贴blue.png,并实现这个效果:
该图像是通过在Photoshop中将两个图像组合而成,只需两层.
我可以使用Python Imaging Library获得的最接近的是:
使用此代码:
from PIL import Image
blue = Image.open("blue.png")
red = Image.open("red.png")
red.paste(blue, (0,0), blue)
red.save("result.png")
Run Code Online (Sandbox Code Playgroud)
你看到两个圆圈重叠的地方的alpha和颜色是怎么样的?在预期的结果图像中,红色和蓝色以紫色的方式混合在一起,但实际结果图像中存在不需要的alpha晕.
如何在PIL中实现理想的结果?
我在一个类的实例中计时函数时遇到了一些麻烦.我不确定我是不是以正确的方式(从未使用过时间),我尝试了第二个参数导入内容的一些变体,但没有运气.这是我正在做的一个愚蠢的例子:
import timeit
class TimedClass():
def __init__(self):
self.x = 13
self.y = 15
t = timeit.Timer("self.square(self.x, self.y)")
try:
t.timeit()
except:
t.print_exc()
def square(self, _x, _y):
print _x**_y
myTimedClass = TimedClass()
Run Code Online (Sandbox Code Playgroud)
哪,跑步时,抱怨自我.
Traceback (most recent call last):
File "timeItTest.py", line 9, in __init__
t.timeit()
File "C:\Python26\lib\timeit.py", line 193, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
self.square(self.x, self.y)
NameError: global name 'self' is not defined
Run Code Online (Sandbox Code Playgroud)
这与TimeIt创建一个虚拟环境以运行该函数有关,但是我需要传递给第二个参数以使其全部满意?
我是Python的新手,我非常喜欢这个min
功能.
>>>min([1,3,15])
0
Run Code Online (Sandbox Code Playgroud)
但是,如果我有一个实例列表,并且它们都有一个名为number
?的变量怎么办?
class Instance():
def __init__(self, number):
self.number = number
i1 = Instance(1)
i2 = Instance(3)
i3 = Instance(15)
iList = [i1,i2,i3]
Run Code Online (Sandbox Code Playgroud)
我真的需要这样的东西吗?
lowestI = iList[0].number
for i in iList:
if lowestI > iList[i].number: lowestI = iList[i].number
print lowestI
Run Code Online (Sandbox Code Playgroud)
我不能min
以一种漂亮的pythonic方式使用吗?
尝试弄清楚如何将构造函数与继承的类一起使用。我知道这是非常错误的,我已经编写 C++ 大约三天了,但无论如何这是我的代码:
clientData.h,两个类,ClientData扩展Entity:
#pragma once
class Entity
{
public:
int x, y, width, height, leftX, rightX, topY, bottomY;
Entity(int x, int y, int width, int height);
~Entity();
};
class ClientData : public Entity
{
public:
ClientData();
~ClientData();
};
Run Code Online (Sandbox Code Playgroud)
和clientData.cpp,其中包含函数:
#include <iostream>
#include "clientData.h"
using namespace std;
Entity::Entity(int x, int y, int width, int height)
{
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->leftX = x - (width/2);
this->rightX = x + (width/2);
this->topY = …
Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用Mercurial,下载并安装了适用于Windows的hg命令行,克隆了一个新的BitBucket存储库,添加了一个文件,但是当我提交时,我收到此错误:
>hg commit -m 'first commit'
abort: commit': The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)
添加的文件肯定在存储库文件夹中.当我运行hg状态时,我得到:
A Ant.java
Run Code Online (Sandbox Code Playgroud)
在提交消息中添加--traceback会让我:
Traceback (most recent call last):
File "mercurial\dispatch.pyc", line 87, in _runcatch
File "mercurial\dispatch.pyc", line 684, in _dispatch
File "mercurial\dispatch.pyc", line 466, in runcommand
File "mercurial\dispatch.pyc", line 738, in _runcommand
File "mercurial\dispatch.pyc", line 692, in checkargs
File "mercurial\dispatch.pyc", line 681, in <lambda>
File "mercurial\util.pyc", line 458, in check
File "mercurial\commands.pyc", line 1194, in commit
File "mercurial\cmdutil.pyc", line 1223, in commit
File "mercurial\commands.pyc", …
Run Code Online (Sandbox Code Playgroud) 我正面对Datetime的奇怪小问题.这是我正在做的事情:
>>> from datetime import datetime, date
>>> t = date.timetuple(datetime.now())
>>> t
time.struct_time(tm_year=2011, tm_mon=6, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=165, tm_isdst=-1)
Run Code Online (Sandbox Code Playgroud)
tm_hour,tm_min和tm_sec都为零.为什么是这样?