Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。

5
X次运行流程的更多Pythonic方式
哪个更pythonic? While循环: count = 0 while count < 50: print "Some thing" count = count + 1 对于循环: for i in range(50): print "Some thing" 编辑:不重复,因为这有答案可以确定哪个更清晰,而不是如何在不使用“ i”的情况下运行范围-即使最终结果是最优雅的
90 python  loops 

8
将一长串常量导入到Python文件
在Python中,是否存在类似C预处理程序语句的类似物? #define MY_CONSTANT 50 另外,我有大量的常数要导入几个类。是否有类似的方式将常量声明为类似于上述内容的长语句序列,.py然后将其导入另一个.py文件? 编辑。 该文件Constants.py显示为: #!/usr/bin/env python # encoding: utf-8 """ Constants.py """ MY_CONSTANT_ONE = 50 MY_CONSTANT_TWO = 51 并myExample.py写道: #!/usr/bin/env python # encoding: utf-8 """ myExample.py """ import sys import os import Constants class myExample: def __init__(self): self.someValueOne = Constants.MY_CONSTANT_ONE + 1 self.someValueTwo = Constants.MY_CONSTANT_TWO + 1 …

5
在ConfigParser中保留大小写?
我尝试使用Python的ConfigParser模块保存设置。对于我的应用程序,重要的是要在我的部分中保留每个名称的大小写。文档提到将str()传递给ConfigParser.optionxform()可以完成此操作,但对我而言不起作用。名称均为小写。我想念什么吗? <~/.myrc contents> [rules] Monkey = foo Ferret = baz 我得到的Python伪代码: import ConfigParser,os def get_config(): config = ConfigParser.ConfigParser() config.optionxform(str()) try: config.read(os.path.expanduser('~/.myrc')) return config except Exception, e: log.error(e) c = get_config() print c.options('rules') [('monkey', 'foo'), ('ferret', 'baz')]

6
sphinx可以链接到不在根文档下的目录中的文档吗?
我正在使用Sphinx记录非Python项目。我想./doc在每个子模块中分发文件夹,其中包含submodule_name.rst用于记录该模块文件的文件。然后,我想将这些文件吸收到主层次结构中,以创建整个设计的规范。 即: Project docs spec project_spec.rst conf.py modules module1 docs module1.rst src module2 docs module2.rst src 我试图project_spec.rst像这样在主文档toctree中包含文件: .. toctree:: :numbered: :maxdepth: 2 Module 1 <../../modules/module1/docs/module1> 但是,此错误消息导致: 警告:toctree包含对不存在的文档u'modules / module1 / docs / module1'的引用 是否无法以../某种方式在文档路径中使用? 更新:添加了conf.py位置 更新:除了下面的包含技巧之外,仍然不可能(2019)。有一个开放的问题一直在推进:https : //github.com/sphinx-doc/sphinx/issues/701

3
Python:导入子包或子模块
已经使用了平面软件包,我没想到嵌套软件包会遇到这个问题。这是… 目录布局 dir | +-- test.py | +-- package | +-- __init__.py | +-- subpackage | +-- __init__.py | +-- module.py init .py的内容 这两个package/__init__.py和package/subpackage/__init__.py是空的。 内容 module.py # file `package/subpackage/module.py` attribute1 = "value 1" attribute2 = "value 2" attribute3 = "value 3" # and as many more as you want... 内容test.py(3个版本) …

3
将Python编译为WebAssembly
我已经读过可以将Python 2.7代码转换为Web Assembly,但是我找不到关于如何这样做的权威指南。 到目前为止,我已经使用Emscripten及其所有必需的组件将C程序编译为Web程序,因此我知道它是有效的(使用的指南:http : //webassembly.org/getting-started/developers-guide/) 为了在Ubuntu计算机上执行此操作,我必须采取什么步骤?我是否必须将python代码转换为LLVM位代码,然后使用Emscripten进行编译?如果是这样,我将如何实现?

10
向熊猫DataFrame添加元信息/元数据
是否可以向熊猫DataFrame添加一些元信息/元数据? 例如,用于测量数据的仪器名称,负责的仪器等。 一种解决方法是用该信息创建一列,但是在每一行中存储一条信息似乎很浪费!
90 python  pandas 

18
pip安装/usr/local/opt/python/bin/python2.7:错误的解释器:没有这样的文件或目录
我不知道这是怎么回事,但是我被困在一些无法解决的stackoverflow解决方案上。你能帮我吗? Monas-MacBook-Pro:CS764 mona$ sudo python get-pip.py The directory '/Users/mona/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/Users/mona/Library/Caches/pip/http' or …

5
在Dicts列表中,找到一个公共Dict字段的min()值
我有这样的词典列表: [{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}] 我想找到min()和max()价格。现在,我可以使用带有lambda表达式的键(在另一篇SO文章中找到)轻松地对它进行排序,因此,如果没有其他方法,我就不会陷入困境。但是,据我所见,Python中几乎总是有直接方法,因此这是我学习更多知识的机会。
90 python  list  dictionary  max  min 



4
熊猫数据框按日期时间月份分组
考虑一个csv文件: string,date,number a string,2/5/11 9:16am,1.0 a string,3/5/11 10:44pm,2.0 a string,4/22/11 12:07pm,3.0 a string,4/22/11 12:10pm,4.0 a string,4/29/11 11:59am,1.0 a string,5/2/11 1:41pm,2.0 a string,5/2/11 2:02pm,3.0 a string,5/2/11 2:56pm,4.0 a string,5/2/11 3:00pm,5.0 a string,5/2/14 3:02pm,6.0 a string,5/2/14 3:18pm,7.0 我可以阅读一下,然后将date列重新格式化为datetime格式: b=pd.read_csv('b.dat') b['date']=pd.to_datetime(b['date'],format='%m/%d/%y %I:%M%p') 我一直在尝试按月对数据进行分组。似乎应该有一种明显的方式来访问月份并以此进行分组。但是我似乎做不到。有人知道吗? 我目前正在尝试按日期重新建立索引: b.index=b['date'] 我可以这样访问月份: b.index.month 但是我似乎找不到按月汇总的函数。

9
如何安装python distutils
我刚刚在VPS服务器上有一些空间(在ubuntu 8.04上运行),并且我试图在上面安装django。该服务器已安装python 2.5,但我猜它是非标准安装。当我运行Django的安装脚本时,我得到 amitoj@ninja:~/Django-1.2.1$ python setup.py install Traceback (most recent call last): File "setup.py", line 1, in <module> from distutils.core import setup ImportError: No module named distutils.core 我很沮丧 互联网上的所有文章都告诉我如何使用distutils安装模块。但是我如何获得distutils呢?谁能指出我指向distutils的存档?我查看了/usr/lib/local/python2.5、/usr/lib/python2.5等,并且没有发现distutils。
90 python  distutils 


2
Python类定义语法
两者之间有区别吗 class A: ... 和 class A(): ... 我只是意识到我的几个类都被定义为前一个类,并且效果很好。空括号有什么区别吗?
90 python  class 

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.