程序设计

专业和发烧友程序员的问答

5
如何在Python中解析JSON?
我的项目目前正在python中接收JSON消息,我需要从中获取一些信息。为此,我们将其设置为字符串中的一些简单JSON: jsonStr = '{"one" : "1", "two" : "2", "three" : "3"}' 到目前为止,我一直在使用列表生成JSON请求json.dumps,但是与此相反,我认为我需要使用json.loads。但是我没有那么幸运。谁能为我提供一个片段,该片段将在上述示例"2"的输入中返回"two"?
281 python  json  parsing 

10
在R中测量功能执行时间
R中测量功能执行时间的方法是否标准化? 显然,我可以system.time在执行之前和执行之后进行处理,然后加以区别,但是我想知道是否存在某种标准化的方式或功能(想不发明轮子)。 我似乎记得我曾经使用过以下方法: somesysfunction("myfunction(with,arguments)") > Start time : 2001-01-01 00:00:00 # output of somesysfunction > "Result" "of" "myfunction" # output of myfunction > End time : 2001-01-01 00:00:10 # output of somesysfunction > Total Execution time : 10 seconds # output of somesysfunction
281 r  time  profiling 

6
捕获异步void方法引发的异常
使用Microsoft .NET的异步CTP,是否可以在调用方法中捕获由异步方法引发的异常? public async void Foo() { var x = await DoSomethingAsync(); /* Handle the result, but sometimes an exception might be thrown. For example, DoSomethingAsync gets data from the network and the data is invalid... a ProtocolException might be thrown. */ } public void DoFoo() { try { Foo(); …



7
如何使用Jest模拟ES6模块导入?
我开始认为这是不可能的,但是无论如何我都想问。 我想测试我的一个ES6模块以特定方式调用另一个ES6模块。有了茉莉花,这非常容易- 应用程式码: // myModule.js import dependency from './dependency'; export default (x) => { dependency.doSomething(x * 2); } 和测试代码: //myModule-test.js import myModule from '../myModule'; import dependency from '../dependency'; describe('myModule', () => { it('calls the dependency with double the input', () => { spyOn(dependency, 'doSomething'); myModule(2); expect(dependency.doSomething).toHaveBeenCalledWith(4); }); }); 笑话相当于什么?我觉得这是一件很想做的简单的事情,但是我一直在努力尝试弄清头发。 我最接近的方法是将imports …

18
是否有用于字符串自然排序的内置函数?
使用Python 3.x,我有一个要对其执行自然字母排序的字符串列表。 自然排序: Windows中文件的排序顺序。 例如,以下列表是自然排序的(我想要的): ['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13'] 这是上面列表的“排序”版本(我所拥有的): ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] 我正在寻找一种类似于第一个的排序功能。

18
如何打印出向量的内容?
我想在C ++中打印出向量的内容,这是我所拥有的: #include <iostream> #include <fstream> #include <string> #include <cmath> #include <vector> #include <sstream> #include <cstdio> using namespace std; int main() { ifstream file("maze.txt"); if (file) { vector<char> vec(istreambuf_iterator<char>(file), (istreambuf_iterator<char>())); vector<char> path; int x = 17; char entrance = vec.at(16); char firstsquare = vec.at(x); if (entrance == 'S') { path.push_back(entrance); …
281 c++  vector  output  stdvector  cout 


14
获取目录中文件的过滤列表
我正在尝试使用Python获取目录中的文件列表,但是我不想要所有文件的列表。 我本质上想要的是能够执行以下操作但使用Python而不执行ls的功能。 ls 145592*.jpg 如果没有内置方法,我目前正在考虑编写一个for循环以遍历an的结果。 os.listdir()并将所有匹配的文件附加到新列表中。 但是,该目录中有很多文件,因此我希望有一种更有效的方法(或内置方法)。



6
从Numpy数组创建Pandas DataFrame:如何指定索引列和列标题?
我有一个由列表列表组成的Numpy数组,代表带有行标签和列名的二维数组,如下所示: data = array([['','Col1','Col2'],['Row1',1,2],['Row2',3,4]]) 我希望所得的DataFrame将Row1和Row2作为索引值,并将Col1,Col2作为标头值 我可以指定索引如下: df = pd.DataFrame(data,index=data[:,0]), 但是我不确定如何最好地分配列标题。
281 python  pandas  numpy 



By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.