我试图找出如何将多个值附加到Python中的列表.我知道有一些方法来做到这一点,如手动输入值,或在PUR追加操作for循环,或append和extend功能.
但是,我想知道是否有更简洁的方法呢?也许某个包或功能?
我是Python的第1天,所以如果我问天真的问题,请原谅我.
非常感谢你.:)
我正在尝试使用Cython来编写我的项目.
我的计划是用C++编写.dll,然后通过Cython从Python调用它们.所以我可以拥有C++的高计算性能,同时保持Python开发的简单性.
随着我走得更远,我有点困惑.据我所知,Cython将python代码包装成C.由于C具有更好的计算性能,因此性能得到了提高.我对此是否正确?
如果我在上面,那么是否有必要在C++中编写.dll并从Python调用它以提高性能?
如果我编写python代码并将其包装到C中,然后从Python调用它,它是否比调用用C++编写的.dll更好?
我pyodbc用来连接数据库并从中提取某些数据.
这是我的代码:
con = pyodbc.connect("driver={SQL Server};server= MyServer;database= MyDatabase;trusted_connection=true")
cursor = con.cursor()
SQL_command = """
SELECT RowID = ISNULL
(
(
SELECT TOP 1 RowID
FROM [MyDatabase].[admin].[MyTable]
WHERE [queue] = ? and processed IS NULL
)
,-1
)
"""
cursor.execute(SQL_command, queueNumber)
cursor.commit()
con.commit()
result_set = cursor.fetchall()
Run Code Online (Sandbox Code Playgroud)
运行上面的代码后出现以下错误:
pyodbc.Error:('HY010','[HY010] [Microsoft] [ODBC SQL Server驱动程序]函数序列错误(0)(SQLFetch)')
我可以知道是什么导致了这样的问题,我该如何解决?
谢谢.
我试图将一些python代码翻译成C++.代码的作用是运行蒙特卡罗模拟.我认为Python和C++的结果可能非常接近,但似乎发生了一些有趣的事情.
这是我在Python中所做的:
self.__length = 100
self.__monte_carlo_array=np.random.uniform(0.0, 1.0, self.__length)
Run Code Online (Sandbox Code Playgroud)
这是我在C++中所做的:
int length = 100;
std::random_device rd;
std::mt19937_64 mt(rd());
std::uniform_real_distribution<double> distribution(0, 1);
for(int i = 0; i < length; i++)
{
double d = distribution(mt);
monte_carlo_array[i] = d;
}
Run Code Online (Sandbox Code Playgroud)
我在Python和C++中以100x5的随机数运行,然后使用这些随机数进行蒙特卡罗模拟.
在蒙特卡罗模拟中,我将阈值设置为0.5,因此我可以轻松验证结果是否均匀分布.
这是monte carlo模拟的概念草案:
for(i = 0; i < length; i++)
{
if(monte_carlo_array[i] > threshold) // threshold = 0.5
monte_carlo_output[i] = 1;
else
monte_carlo_output[i] = 0;
}
Run Code Online (Sandbox Code Playgroud)
由于monte carlo数组的长度为120,我希望1在Python和C++中看到60 秒.我计算了1s 的平均数,并发现,尽管C++和Python中的平均数约为60,但趋势是高度相关的.而且,Python中的平均数总是高于 C++.
我是否知道这是因为我做错了什么,还是仅仅因为C++和Python中随机生成机制的区别?
我正在尝试使用xlwt多个选项卡创建输出文件(.xlsx格式).我的Python的版本号是2.7,我使用Aptana Studio 3作为IDE.
我之前使用过xlwt相同的环境来执行相同的任务.它运作良好.但这一次,它起初运行良好,然后突然,输出文件出现故障,MS Excel无法打开.
这是一条可能有用的线索.我的Aptana Studio 3决定.xlsx在自己的编辑器中打开而不是启动MS Excel.虽然这发生在问题之前,但我想知道它是否相关.
该文件在Aptana3中打开时看起来很正常,但是当我关闭它并用MS Excel打开它时会弹出一个错误:"Excel cannot open the file "output.xlsx" because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."
我可以知道如何克服这个问题吗?欢迎任何建议.谢谢.
我在C++中有一个3D字符串向量:
vector<vector<vector<string>>> some_vector
Run Code Online (Sandbox Code Playgroud)
我正在尝试的是找到一种快速的方法来为它分配内存.
我试着用两种不同的方法来定义它,如下所示:
#include<vector>
#include<iostream>
#include<ctime>
using namespace std;
#define DIM1 100
#define DIM2 9
#define DIM3 120
int main()
{
clock_t t1_start = clock();
vector<vector<vector<string>>> vec1(DIM1, vector<vector<string>>(DIM2, vector<string>(DIM3)));
clock_t t1_end = clock();
double diff1 = (t1_end - t1_start) / double(CLOCKS_PER_SEC);
clock_t t2_start = clock();
vector<vector<vector<string>>> vec2;
vec2.resize(DIM1);
for(int i = 0; i < DIM1; i++)
{
vec2[i].resize(DIM2);
for(int j = 0; j < DIM2; j++)
vec2[i][j].resize(DIM3);
}
clock_t t2_end = clock();
double diff2 = (t2_end - t2_start) …Run Code Online (Sandbox Code Playgroud) 我正在尝试根据官方网站上的说明构建Boost.Python.
我的操作系统是Windows 7 64位,编译器是MSVC11,增强是1.54.
===================安装Boost ==================
要安装基本的Boost,我从其网站下载了boost库,将其解压缩到我的本地磁盘.路径是C:\local\boost_1_54_0.
===============安装Boost.Python ===============
然后我发现Boost.Python需要单独构建.所以我遵循了Boost.Python指令.
===============安装Boost.Build ================
在3.1.2阶段,需要Boost.Build.所以我再次按照步骤安装Boost.Build C:\local\boost_1_54_0\BoostBuild,并添加C:\local\boost_1_54_0\BoostBuild\bin到Path环境变量中.
然后我被困在阶段3.1.3和3.1.4,指令说
3.cd进入Boost安装的libs/python/example/quickstart /目录,其中包含一个小示例项目.
4.Invoke bjam.将"入门指南"第5节中的示例调用中的"stage"参数替换为"test",以构建所有测试目标.还要添加参数"--verbose-test"以查看测试运行时生成的输出.
在Windows上,您的bjam调用可能类似于:
C:\ boost_1_34_0\...\quickstart> bjam toolset = msvc --verbose-test test
我输入了指令告诉我的命令行,我确信我在正确的目录中.然而,不存在bjam或b2中quickstart文件夹(它们是在升压和C的根direcroty:\本地\ boost_1_54_0\BoostBuild\bin)中.
我甚至试图将这两个文件复制到该quickstart文件夹,但根本不起作用.
我对Windows管理机制不太熟悉,因此不确定这是否Path是我的问题的关键.我想后,我添加了bin文件夹到PATH,操作系统会自动我的链接bjam toolset=msvc --verbose-test test到bjam的在C:\local\boost_1_54_0\BoostBuild\bin文件夹?
谁能帮我这个?:)
我有一个浮点数列表,其格式为:
float_list = [1.13, 0.25, 3.28, ....]
Run Code Online (Sandbox Code Playgroud)
我使用以下代码将其写入.csv文件:
float_str = ""
for result in result_list:
float_str = float_str + str(result.get_value()) + ","
with open(output_path, "wb") as file:
writer = csv.writer(file)
writer.writerow(float_str)
file.close()
Run Code Online (Sandbox Code Playgroud)
在哪里result.get_value():
def get_value():
return float(value)
Run Code Online (Sandbox Code Playgroud)
我希望在每个单元格中看到有一个值.但是,我得到的是:
1 . 1 3 , 0 . 2 5 , 3 . 2 8 ,
Run Code Online (Sandbox Code Playgroud)
似乎每个数字占据一个单元格.
那么,我可以知道如何解决这个问题并得到我的期望吗?
我正在学习wxPython.在其中一个示例中,代码如下:
import wx
class App(wx.App):
def OnInit(self):
frame = wx.Frame(parent=None, title = 'bare')
frame.Show()
return True
app=App()
app.MainLoop()
Run Code Online (Sandbox Code Playgroud)
我注意到该类App没有构造函数,只有函数OnInit.据我所知,Python类是用__init__函数构造的.
那么,OnInit函数是针对特定的类吗?或者它是另一种类型的构造函数?
请原谅我的无知,因为我是新手.谢谢.
我有一个清单:
s = [0.995537725, 0.994532199, 0.996027983, 0.999891383, 1.004754272, 1.003870012, 0.999888944, 0.994438078, 0.992548715, 0.998344545, 1.004504764, 1.00883411]
Run Code Online (Sandbox Code Playgroud)
我在Excel中计算了它的标准差,我得到了答案:0.005106477我使用的函数是:=STDEV(C5:N5)
然后我使用numpy.stdas 进行相同的计算:
import numpy as np
print np.std(s)
Run Code Online (Sandbox Code Playgroud)
但是,我得到了答案: 0.0048890791894
我甚至写了自己的std函数:
def std(input_list):
count = len(input_list)
mean = float(sum(input_list)) / float(count)
overall = 0.0
for i in input_list:
overall = overall + (i - mean) * (i - mean)
return math.sqrt(overall / count)
Run Code Online (Sandbox Code Playgroud)
而我自己的函数给出与numpy相同的结果.
所以我想知道是否有这样的差异?或者只是我犯了一些错误?
python ×8
c++ ×3
excel ×2
boost ×1
boost-python ×1
c ×1
command-line ×1
constructor ×1
csv ×1
cython ×1
dll ×1
list ×1
montecarlo ×1
numpy ×1
path ×1
performance ×1
pyodbc ×1
random ×1
sql ×1
vector ×1
wxpython ×1
xlwt ×1