在以下有关python嵌入的文档中,很好地描述了如何在“ C”代码中嵌入python方法。 https://docs.python.org/2/extending/embedding.html
我测试了上面的代码,它也与g ++编译器一起很好地工作。
但是上面显示了调用全局方法而不是类方法的示例。
任何人都可以显示有关如何创建Python对象并从C ++调用其方法的示例吗?
我想使用bash命令删除所有带有'*.tar.gz'扩展名的文件.我尝试了以下,但它没有用.
find . -iname '*.tar.gz' | rm
Run Code Online (Sandbox Code Playgroud)
你能否建议我在这种情况下使用哪个命令?另外,请你告诉我为什么上面的命令不起作用?
我一直在从Matlab转到NumPy/Scipy,我认为NumPy在很多方面都很棒.
但是我觉得不舒服的一件事是我找不到类似于structC/C++ 的数据结构.
例如,我可能想要做以下事情:
struct Parameters{
double frame_size_sec;
double frame_step_sec;
}
Run Code Online (Sandbox Code Playgroud)
一种最简单的方法是使用字典如下.
parameters = {"frame_size_sec" : 0.0, "frame_step_sec", 0.0}
Run Code Online (Sandbox Code Playgroud)
但是在字典的情况下,与struct不同,可以添加任何键.我想限制钥匙.
另一个选项可能是使用如下的类.但它也有同样类型的问题.
class Parameters:
frame_size_sec = 0.0
frame_step_sec = 0.0
Run Code Online (Sandbox Code Playgroud)
从一个线程,我看到有一个名为名为tuple的数据结构,看起来很棒,但最大的问题是字段是不可变的.所以它仍然与我想要的不同.
总而言之,在python中使用类似结构的对象的最佳方法是什么?
在.bashrc中,我通常设置许多环境变量来存储路径名。
因此,我可以按如下所示转到该特定目录:
cd $PARTICULAR_DIR
Run Code Online (Sandbox Code Playgroud)
在IPython中有做过这件事吗?上面的命令不起作用,因此我尝试了以下操作:
cd os.environ['PARTICULAR_DIR']
Run Code Online (Sandbox Code Playgroud)
但是上述方法也不起作用。
你能启发我吗?
我尝试了以下脚本.
我的初衷是如果成功执行以下代码段中的命令"command_a",则程序结束.否则,它会执行其他处理.
#!/usr/bin/python
import subprocess
try:
command = "command_a"
subprocess.check_call(command.split())
print "Woks fine!, and stopping"
sys.exit()
except:
pass
print "Continue additional processing"
call_some_additional_processing()
Run Code Online (Sandbox Code Playgroud)
但是,我注意到这sys.exit()没有按预期工作.即使subprocess.check_call成功,它也会调用call_some_additional_processing().
你能告诉我原因是什么,解决这个问题的最佳方法是什么?
考虑以下定义PlotFigure () 的Python 模块“ plot_figure.py ” 。请注意,这是一个伪代码。
import matplotlib.pyplot as plt
def PlotFigure(x)
# Some other routines..
plt.plot(x)
# Some other routines..
Run Code Online (Sandbox Code Playgroud)
我想调用 plot_figure.PlotFigure,但在绘制图形后,我想更改此图形的线宽。尽管 PlotFigure() 可能包含其他例程,图中的线条是使用 plt.plot() 绘制的,如上面的伪代码所示。
下面是调用plot_figure.PlotFigure()的代码
#!/usr/bin/python
import matplotlib.pyplot as plt
import plot_figure
x_data = [ # some data ]
plot_figure.PlotFigure(x_data)
#***I would like to change the line width of the figure here***
plt.show()
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用 获得图形句柄fig = plt.gcf(),但plt.setp(fig, linewidth=2)不起作用。
有人可以就此提出一些建议吗?
我有点不确定为什么printPython 中的函数以不同的方式打印出值。考虑以下代码示例:
#!/usr/bin/python3
import numpy as np
array = np.arange(3.0, dtype=np.float32)
print ("array: ", array)
print ("array: ", {0: 2.0 * array})
print ("array {0}".format(2.0 * array))
Run Code Online (Sandbox Code Playgroud)
输出如下:
array: [0. 1. 2.]
array: {0: array([0., 2., 4.], dtype=float32)}
array [0. 2. 4.]
Run Code Online (Sandbox Code Playgroud)
第二个的格式与其他格式不同。这一个显式地显示了数据格式“数组”和数据类型“float32”。这是预期的行为吗?
有人可以向我解释这背后的理由吗?
我是python的新手.在C/C++中,在while或if语句中,我经常进行变量赋值.以下是C++中的一个简单示例代码:
#include <iostream>
int Increment(const int x) {
return (x + 1);
}
int main(void) {
int x = 2, y;
while ((y = Increment(x)) > 2) {
std::cout << "y is larger than 2" << std::endl;
}
return (0);
}
Run Code Online (Sandbox Code Playgroud)
但是以下python代码不起作用:
#!/usr/bin/python
def Increment(x):
return x + 1
def main():
x= 2
while (y = Increment(x)) > 2:
print "y is larger than 2"
if __name__ == "__main__"
main()
Run Code Online (Sandbox Code Playgroud)
错误消息如下:
while (y = Increment(x)) > 2:
^
SyntaxError: invalid …Run Code Online (Sandbox Code Playgroud) 我有一个字符串列表。每个字符串都有data0*(\d*)if 我们使用正则表达式的形式。以下是字符串的示例:
data000000, data000003, data0172, data2312, data008212312
Run Code Online (Sandbox Code Playgroud)
我只想取有意义的数字部分。所有数字都是整数。例如,在上述情况下,我想获得另一个包含以下内容的列表:
0, 3, 172, 2312, 8212312
Run Code Online (Sandbox Code Playgroud)
在上述情况下,最好的方法是什么?
以下是我想到的解决方案:
import re
string_list = ["data0000172", ..... ]
number_list = []
for string in string_list:
match = re.search("data0*(\d+)", string)
if match:
number_list.append(match.group(1))
else:
raise Exception("Wrong format.")
Run Code Online (Sandbox Code Playgroud)
但是,上述方法可能效率低下。你能提出一个更好的方法来做到这一点吗?
我正在使用matplotlib绘制数据点.
基本上,我想绘制离散点.他们中的许多人被置于边界之上.但是,如附图所示,图形边界上的数据点仅显示为半圆而不是整圆.
任何人都可以建议如何在边界上绘制这些点作为完整的圆圈?
def PlotGrid(grid_point, file_name):
plt.figure()
dims = np.shape(grid_point)
for i in range(0, dims[1]):
for j in range(0, dims[2]):
plt.plot(grid_point[0, i, j], grid_point[1, i, j], 'ro', markersize=15)
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在从 Matlab/octve 切换到 Numpy/Scipy。
选择 Matlab 数组的一段非常简单。例如
>> x = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12]
x =
1 2 3 4
5 6 7 8
9 10 11 12
>> y = x(2:3, 1:2)
y =
5 6
9 10
Run Code Online (Sandbox Code Playgroud)
当 NumPy 完成同样的事情时
x = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
Run Code Online (Sandbox Code Playgroud)