Tag: python

哪些PEP必须读取?

我是一个相当强大的Python编码器,但是我的风格太多了,有点杂乱,我相信对于许多问题,Pythonic解决scheme比我提出的要多。 哪些PEP对于任何精通Pythonista的阅读都是必不可less的?

正则expression式比python慢

嗨,我想了解为什么下面的代码使用正则expression式拆分string拆分 #include<regex> #include<vector> #include<string> std::vector<std::string> split(const std::string &s){ static const std::regex rsplit(" +"); auto rit = std::sregex_token_iterator(s.begin(), s.end(), rsplit, -1); auto rend = std::sregex_token_iterator(); auto res = std::vector<std::string>(rit, rend); return res; } int main(){ for(auto i=0; i< 10000; ++i) split("abc", " "); return 0; } 比以下的python代码慢 import re for i in range(10000): re.split(' +', 'ab […]

Pythonpandas:逐行填充dataframe

将一行添加到pandas.DataFrame对象的简单任务似乎很难完成。 有3个与此相关的stackoverflow问题,没有一个给出了一个工作的答案。 这是我想要做的。 我有一个DataFrame我已经知道的形状以及行和列的名称。 >>> df = pandas.DataFrame(columns=['a','b','c','d'], index=['x','y','z']) >>> df abcd x NaN NaN NaN NaN y NaN NaN NaN NaN z NaN NaN NaN NaN 现在,我有一个函数来迭代计算行的值。 我怎样才能用字典或pandas.Series来填充其中pandas.Series呢? 以下是各种失败的尝试: >>> y = {'a':1, 'b':5, 'c':2, 'd':3} >>> df['y'] = y AssertionError: Length of values does not match length of index 显然它试图添加一列而不是一行。 >>> y = […]

将目录添加到sys.path / PYTHONPATH

我正试图从一个特定的目录中导入一个模块。 问题是,如果我使用sys.path.append(mod_directory)追加path,然后打开python解释器,则将目录mod_directory添加到sys.path列表的末尾。 如果在打开python解释器之前导出PYTHONPATHvariables,则该目录将被添加到列表的开头。 在后一种情况下,我可以导入模块,但在前者,我不能。 有人可以解释为什么发生这种情况,并给我一个解决scheme,以添加mod_directory的开始, 在 python脚本内?

Numpy argsort – 它在做什么?

为什么numpy给出这个结果: x = numpy.array([1.48,1.41,0.0,0.1]) print x.argsort() >[2 3 1 0] 当我期望它做到这一点时: [3 2 0 1] 显然我对这个function的理解是缺乏的。

我怎样才能从Python的文件/stream中懒散地读取多个JSON对象?

我想从Python文件/stream中读取多个JSON对象,一次一个。 不幸的是, json.load()只是.read() s直到文件结束; 似乎没有什么方法可以用它来读取单个对象或懒惰地迭代对象。 有没有办法做到这一点? 使用标准库将是理想的,但如果有第三方库,我会使用它。 目前我把每个对象放在一个单独的行,并使用json.loads(f.readline()) ,但我真的不希望这样做。 示例使用 example.py import my_json as json import sys for o in json.iterload(sys.stdin): print("Working on a", type(o)) in.txt {"foo": ["bar", "baz"]} 1 2 [] 4 5 6 示例会话 $ python3.2 example.py < in.txt Working on a dict Working on a int Working on a int Working on […]

如何在Python中设置matplotlib中的“后端”?

我是matplotlib的新用户,我的平台是Ubuntu 10.04 Python 2.6.5 这是我的代码 import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt plt.plot([1,2,3]) 错误是: /usr/local/lib/python2.6/dist-packages/matplotlib/backends/__init__.py:41: UserWarning: Your currently selected backend, 'agg' does not support show(). Please select a GUI backend in your matplotlibrc file ('/usr/local/lib/python2.6/dist-packages/matplotlib/mpl-data/matplotlibrc') or with matplotlib.use() (backend, matplotlib.matplotlib_fname())) 我安装了Anti-Grain Geometry库apt-get install libagg但是它不起作用。 我试图使用其他像“GTK”和“TkAgg”的后端参数。 我安装了python-gtk2-dev软件包,但是仍然在下面的错误。 任何人都可以告诉我一个可执行的后端参数及其依赖库吗? 这是错误: >>> matplotlib.use('GTK') >>> import matplotlib.pyplot as plt […]

什么是最好的方式来声明numpy.array平等?

我想为我的应用程序做一些unit testing,我需要比较两个数组。 由于array.__eq__返回一个新的数组(所以TestCase.assertEqual失败),什么是最好的方式来断言相等? 目前我正在使用 self.assertTrue((arr1 == arr2).all()) 但是我不太喜欢它:\

将生成器对象转换为列表进行debugging

当我使用IPython在Python中进行debugging时,有时候会碰到一个断点,我想检查一个当前是一个生成器的variables。 我能想到的最简单的方法就是把它转换成一个列表,但是我不清楚在ipdb一行中做什么的简单方法,因为我对Python很ipdb 。

像C#中的StringBuilder的Pythonstring类?

在Python中有像StringBuilder一样的C#中的一些string类吗?