Questions tagged «python»

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

10
如何检查一个数字是否可被另一个数字整除(Python)?
我需要测试从1到1000的每个数字是3的倍数还是5的倍数。我认为我要这样做的方式是将数字除以3,如果结果是整数,则它将是3的倍数。与5相同。 如何测试数字是否为整数? 这是我当前的代码: n = 0 s = 0 while (n < 1001): x = n/3 if isinstance(x, (int, long)): print 'Multiple of 3!' s = s + n if False: y = n/5 if isinstance(y, (int, long)): s = s + n print 'Number: ' print n print 'Sum:' …
112 python  integer  modulus 

3
如何从Python函数调用中捕获标准输出?
我正在使用对对象执行某些操作的Python库 do_something(my_object) 并更改它。这样做时,它会向stdout打印一些统计信息,我希望掌握这些信息。正确的解决方案是更改do_something()以返回相关信息, out = do_something(my_object) 但是开发人员需要一段时间才能do_something()解决此问题。作为一种解决方法,我考虑过解析do_something()对stdout的任何写入。 如何捕获代码两点之间的stdout输出,例如 start_capturing() do_something(my_object) out = end_capturing() ?
112 python  stdout  capture 



11
pip安装中“ X的构建车轮失败”是什么意思?
在SO上,这是一个真正流行的问题,但是我看过的所有答案中,没有一个能清楚地说明此错误的真正含义以及发生的原因。 造成混乱的原因之一是,当您(例如)这样做时pip install pycparser,您首先会得到以下错误: Failed building wheel for pycparser 然后出现以下消息,说明该软件包是: Successfully installed pycparser-2.19。 # pip3 install pycparser Collecting pycparser Using cached https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz Building wheels for collected packages: pycparser Running setup.py bdist_wheel for pycparser ... error Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-g_v28hpp/pycparser/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" …

5
使用Pandas Data Frame运行OLS回归
我有一个pandas数据框,我希望能够从B和C列中的值预测A列的值。这是一个玩具示例: import pandas as pd df = pd.DataFrame({"A": [10,20,30,40,50], "B": [20, 30, 10, 40, 50], "C": [32, 234, 23, 23, 42523]}) 理想情况下,我会有类似的东西,ols(A ~ B + C, data = df)但是当我查看算法库中的示例时,看起来好像scikit-learn是用行而不是列的列表将数据提供给模型。这将要求我将数据重新格式化为列表内的列表,这似乎首先使使用熊猫的目的遭到了破坏。在熊猫数据框中的数据上运行OLS回归(或更通用的任何机器学习算法)的最有效方法是什么?


2
python中的复数用法
在这里很难说出要问什么。这个问题是模棱两可,含糊,不完整,过于宽泛或夸张的,不能以目前的形式合理地回答。如需帮助澄清此问题以便可以重新打开, 请访问帮助中心。 8年前关闭。 我是数学新手。现在,我将更深入地了解Python数据类型。我不明白如何使用复数。请给我示例在Python中使用复数的示例。

3
为什么Python 3允许“ 00”作为0的文字,却不允许“ 01”作为1的文字?
为什么Python 3允许“ 00”作为原义的0,却不允许“ 01”作为原义的1?有充分的理由吗?这种矛盾使我感到困惑。(我们正在谈论的是Python 3,它故意打破了向后兼容性以实现诸如一致性之类的目标。) 例如: >>> from datetime import time >>> time(16, 00) datetime.time(16, 0) >>> time(16, 01) File "<stdin>", line 1 time(16, 01) ^ SyntaxError: invalid token >>>

5
Android Python编程[关闭]
已关闭。这个问题需要更加集中。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅通过编辑此帖子来关注一个问题。 5年前关闭。 改善这个问题 我可以使用Python为Android编程吗?我在搜索时似乎偶然发现了许多链接...但是它们都不是具体的。 有什么建议?我想为Android编写应用程序,但实际上并不想为此而涉足Java。 PS:我的问题是我是否可以编写适用于Android的功能完善的应用程序。
111 python  android 

1
SQLAlchemy版本控制关心类的导入顺序
我在这里遵循指南: http://www.sqlalchemy.org/docs/orm/examples.html?highlight=versioning#versioned-objects 并遇到了一个问题。我的关系定义如下: generic_ticker = relation('MyClass', backref=backref("stuffs")) 使用字符串,因此它不在乎模型模块的导入顺序。这一切都正常工作,但是当我使用版本控制元时,出现以下错误: sqlalchemy.exc.InvalidRequestError:初始化映射程序Mapper | MyClass | stuffs时,表达式'Trader'找不到名称(“名称'MyClass'未定义”)。如果这是一个类名,请考虑在定义了两个从属类之后,将这个Relationship()添加到类中。 我跟踪到以下错误: File "/home/nick/workspace/gm3/gm3/lib/history_meta.py", line 90, in __init__ mapper = class_mapper(cls) File "/home/nick/venv/tg2env/lib/python2.6/site-packages/sqlalchemy/orm/util.py", line 622, in class_mapper mapper = mapper.compile() class VersionedMeta(DeclarativeMeta): def __init__(cls, classname, bases, dict_): DeclarativeMeta.__init__(cls, classname, bases, dict_) try: mapper = class_mapper(cls) _history_mapper(mapper) except UnmappedClassError: …
111 python  sqlalchemy 

3
函数调用中的星号
我正在使用itertools.chain以这种方式“拉平”列表列表: uniqueCrossTabs = list(itertools.chain(*uniqueCrossTabs)) 这跟说的有什么不同? uniqueCrossTabs = list(itertools.chain(uniqueCrossTabs))
111 python  operators 

4
Django URLs TypeError:对于include(),视图必须是可调用的或列表/元组
升级到Django 1.10后,出现错误: TypeError: view must be a callable or a list/tuple in the case of include(). 我的urls.py如下: from django.conf.urls import include, url urlpatterns = [ url(r'^$', 'myapp.views.home'), url(r'^contact/$', 'myapp.views.contact'), url(r'^login/$', 'django.contrib.auth.views.login'), ] 完整的回溯是: Traceback (most recent call last): File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper fn(*args, **kwargs) File "/Users/alasdair/.virtualenvs/django110/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 121, …

5
Python中的插入符(^)有什么作用?
我今天在python中遇到了插入符号运算符,并对其进行了尝试,得到了以下输出: >>> 8^3 11 >>> 8^4 12 >>> 8^1 9 >>> 8^0 8 >>> 7^1 6 >>> 7^2 5 >>> 7^7 0 >>> 7^8 15 >>> 9^1 8 >>> 16^1 17 >>> 15^1 14 >>> 它似乎基于8,所以我猜某种字节操作?除了对浮点数的奇怪表现之外,我似乎无法找到更多关于此搜索网站的信息,是否有人链接到该运算符的工作,或者您可以在此处进行解释?
111 python  operators  caret 

11
在熊猫数据框中插入一行
我有一个数据框: s1 = pd.Series([5, 6, 7]) s2 = pd.Series([7, 8, 9]) df = pd.DataFrame([list(s1), list(s2)], columns = ["A", "B", "C"]) A B C 0 5 6 7 1 7 8 9 [2 rows x 3 columns] 并且我需要添加第一行[2、3、4]以获取: A B C 0 2 3 4 1 5 6 7 2 7 …

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.