我想按照铃声的顺序生成正态分布。我使用此代码来生成数字:
import numpy as np
mu,sigma,n = 0.,1.,1000
def normal(x,mu,sigma):
return ( 2.*np.pi*sigma**2. )**-.5 * np.exp( -.5 * (x-mu)**2. / sigma**2. )
x = np.random.normal(mu,sigma,n) #generate random list of points from normal distribution
y = normal(x,mu,sigma) #evaluate the probability density at each point
x,y = x[np.argsort(y)],np.sort(y) #sort according to the probability density
Run Code Online (Sandbox Code Playgroud)
这是中提出的代码:按顺序生成正态分布 python,numpy
但数字并不遵循钟形。有任何想法吗?非常感谢
我有一个数组double:
QVector<double> Y(count);
Run Code Online (Sandbox Code Playgroud)
我需要将其打包QByteArray以通过以太网发送.
所以我做到了.这不是太难:
QByteArray line;
line.clear();
line.append(QByteArray::fromRawData(reinterpret_cast<const char*>(Y.data()),
count*sizeof(double)));
Run Code Online (Sandbox Code Playgroud)
我尝试使用此代码从QByteArray recv以下位置解压缩数据:
QVector<double> data((line.size())/sizeof(double));
QByteArray dou(sizeof(double),0x0);
for(int i = 0; i<data.count(); i++){
dou = recv.mid(i*sizeof(double),sizeof(double));
data[i] = *reinterpret_cast<const double*>(dou.data());
dou.clear();
}
Run Code Online (Sandbox Code Playgroud)
但我不喜欢它.我想找出优雅的方式从解压QByteArray到QVector<double>
你能帮助我吗?
我的问题:
*.h.我尝试过的:
我已经能够使用os库收集所有标题
for root, dirs, files in os.walk(r'D:\folder\build'):
for f in files:
if f.endswith('.h'):
print os.path.join(root, f)
Run Code Online (Sandbox Code Playgroud)
这正确打印:
D:\folder\build\a.h
D:\folder\build\b.h
D:\folder\build\subfolder\c.h
D:\folder\build\subfolder\d.h
Run Code Online (Sandbox Code Playgroud)
我被困在哪里:
使用完整文件路径列表,如何在维护子目录的同时将这些文件复制到另一个位置?在上面的例子中,我想要维护下面的目录结构\build\
例如,我希望副本创建以下内容:
D:\other\subfolder\build\a.h
D:\other\subfolder\build\b.h
D:\other\subfolder\build\subfolder\c.h
D:\other\subfolder\build\subfolder\d.h
Run Code Online (Sandbox Code Playgroud) 我试图通过比较键来比较两个字典,如果两个单独的字典中的两个键相同,程序应该检查值是否也相同,如果它们不同,程序应该识别这一点。
这是我写的代码:
def compare(firstdict,seconddict):
shared_items = set(firstdict()) & set(seconddict())
length = len(shared_items)
if length > 0:
return shared_items
if length < 1:
return None
print(compare(firstdict,seconddict))
Run Code Online (Sandbox Code Playgroud)
(“firstdict”和“seconddict”是在之前的函数中创建的两个字典)。
当代码运行时,它会打印出所有相同的键,但没有它们的值,即使它们的值不同。
例如如果:
firstdict = {'cat' : 'animal', 'blue' : 'colour', 'sun' : 'star'}
seconddict = {'cat' : 'pet', 'blue' : 'colour', 'earth' : 'star'}
Run Code Online (Sandbox Code Playgroud)
它会打印出:
'cat', 'blue'
Run Code Online (Sandbox Code Playgroud)
而我试图将其打印出来:
'cat pet (animal)'
Run Code Online (Sandbox Code Playgroud)
以那种确切的格式。
任何有关如何编辑我的代码来执行此操作的建议都将受到赞赏:)
以下是config.py:
from collections import OrderedDict
def test_config(fileName):
tp_dict = collections.OrderedDict()
with open("../../config/" + fileName, 'r') as myfile:
file_str = myfile.read().replace(' ', '').split('\n')
tp_list = []
for i, x in enumerate(file_str):
x = x.strip()
try:
key = x[:x.index(':')].strip()
value = x[x.index(':')+1:]
if key == 'testpoint':
pass
else:
tp_dict[key] = value.strip().split(',')
except ValueError,e:
pass
if i % 4 == 0 and i != 0:
tp_list.append(tp_dict.copy())
return tp_list
Run Code Online (Sandbox Code Playgroud)
我在另一个文件test.py中使用该函数:
import config
a = config.test_config('test.txt')
NameError: global name 'collections' is not defined
Run Code Online (Sandbox Code Playgroud)
但是,如果我将整个代码从config.py复制粘贴到test.py的顶部,然后使用该函数,那么我没有错误(参见下面的代码).请问有人向我解释一下吗?我太困惑了.非常感谢你!
""" …Run Code Online (Sandbox Code Playgroud) 我的代码看起来像这样:
ISessionUpdater* updater = nullptr;
if (eventName == "test")
updater = new TestJSONSessionUpdater(doc);
if (eventName == "plus")
updater = new PlusJSONSessionUpdater(doc);
if (updater)
{
bool result = updater->update(data);
delete updater;
return result;
}
return false;
Run Code Online (Sandbox Code Playgroud)
有没有办法做这样的事情,但有unique_ptr?
也就是说,只有1次呼叫update(data)而不是做:
if(cond)
make unique
call update
end
if(cond)
make unique
call update
end
...
Run Code Online (Sandbox Code Playgroud) 问题陈述 - tl; dr
将数字添加到矢量,然后输出它们
细节
我的目的是创建一个类Foo,其中包含一个std::vector<int>我可以以线程安全的方式填充的类.我创建了一个AddValue方法,允许向该向量添加值,同时牢记线程安全性.
std::mutex mt;
class Foo
{
public:
void AddValue(int i)
{
std::lock_guard<std::mutex> lg{ mt };
values.push_back(i);
}
void PrintValues() const
{
for (int i : values)
{
std::cout << i << " ";
}
}
private:
std::vector<int> values;
};
Run Code Online (Sandbox Code Playgroud)
然后,我创建了一个自由函数,我可以用来创建一个线程,而不需要任何内部知识Foo.
void Func(Foo& foo, int t)
{
for (int i = 0; i < t; i++)
{
foo.AddValue(i);
}
}
Run Code Online (Sandbox Code Playgroud)
我的目的是将以下值添加到向量中(由于线程时序,它们以任何顺序结束)
{3, 2, 2, 1, 1, 1, …Run Code Online (Sandbox Code Playgroud) git init
git add .
git commit -m "first"
git push origin ritu
Run Code Online (Sandbox Code Playgroud)
(第二个是分支的名称)但它说
fatal: origin does not appear to be a git repository.
fatal: could not read from remote repository.
Please sure you have the correct access rights and the repository exists.
Run Code Online (Sandbox Code Playgroud) 在下面的示例中,我只期待一个复制构造,因为我认为中间副本将通过copy elided。唯一需要的(我认为?)副本将在B初始化成员变量的构造函数中a。
#include <iostream>
struct A
{
A() = default;
A(A const&) { std::cout << "copying \n"; }
};
struct B
{
B(A _a) : a(_a) {}
A a;
};
struct C : B
{
C(A _a) : B(_a) {}
};
int main()
{
A a{};
C c(a);
}
Run Code Online (Sandbox Code Playgroud)
当我执行此代码(使用-O3)时,我看到以下输出
copying
copying
copying
Run Code Online (Sandbox Code Playgroud)
为什么不删除这些中间副本?
我遇到过这样一种情况:应用程序具有一个或多个 dll 的多个(不兼容)版本。
app.exe
a.dll // v3.0
...
plugins/foo/foo.dll
plugins/foo/a.dll // v4.0
plugins/foo/...
Run Code Online (Sandbox Code Playgroud)
本例中app.exe依赖a.dllv3.0,foo.dll依赖a.dllv4.0。由于我的exe旁边的v3.0的搜索顺序LoadLibrarya.dll是先加载,然后因为已经在内存中,所以跳过后面的v4.0。
假设我不能:
a.dll在这种情况下)app.exe或foo.dll将它们升级/降级到通用a.dll版本有没有办法加载同名 dll 的多个版本来解决这个问题?我看到了有关 AppDomain 的这个答案,但那是针对托管 C# 应用程序的,我的用例是本机 C++ dll。
c++ ×5
python ×4
c++11 ×3
arrays ×1
collections ×1
copy-elision ×1
dictionary ×1
dll ×1
function ×1
git ×1
import ×1
key ×1
key-value ×1
list ×1
nameerror ×1
python-3.x ×1
qbytearray ×1
qt ×1
unique-ptr ×1
windows ×1