Questions tagged «python»

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

3
如何遍历分组的熊猫数据框?
数据框: c_os_family_ss c_os_major_is l_customer_id_i 0 Windows 7 90418 1 Windows 7 90418 2 Windows 7 90418 码: print df for name, group in df.groupby('l_customer_id_i').agg(lambda x: ','.join(x)): print name print group 我正在尝试仅遍历聚合数据,但出现错误: ValueError:太多值无法解包 @EdChum,这是预期的输出: c_os_family_ss \ l_customer_id_i 131572 Windows 7,Windows 7,Windows 7,Windows 7,Window... 135467 Windows 7,Windows 7,Windows 7,Windows 7,Window... c_os_major_is l_customer_id_i …
146 python  pandas 

5
在Python中我应该如何测试变量是否为None,True或False
我有一个可以返回以下三项之一的函数: 成功(True) 失败(False) 错误读取/解析流(None) 我的问题是,如果不应该针对True或进行测试False,应该如何查看结果。以下是我目前的操作方式: result = simulate(open("myfile")) if result == None: print "error parsing stream" elif result == True: # shouldn't do this print "result pass" else: print "result fail" 它真的像删除== True零件一样简单,还是应该添加一个三布尔数据类型。我不希望该simulate函数引发异常,因为我希望外部程序对错误进行的所有操作均将其记录并继续。
146 python 


9
将鼠标悬停在matplotlib中的某个点上时可以显示标签吗?
我正在使用matplotlib制作散点图。散点图上的每个点都与一个命名对象相关联。当我将光标悬停在与该对象关联的散点图上的点上时,我希望能够看到该对象的名称。尤其是,能够快速查看异常点的名称将是很好的。我在此处搜索时能够找到的最接近的东西是注释命令,但这似乎在绘图上创建了固定标签。不幸的是,根据我拥有的点数,如果我标记每个点,则散点图将无法读取。有谁知道一种创建仅在光标悬停在该点附近时才会显示的标签的方法吗?
146 python  matplotlib 

12
在Django模型中存储列表的最有效方法是什么?
目前,我的代码中有很多类似于以下内容的python对象: class MyClass(): def __init__(self, name, friends): self.myName = name self.myFriends = [str(x) for x in friends] 现在,我想将其转换为Django模型,其中self.myName是字符串字段,而self.myFriends是字符串列表。 from django.db import models class myDjangoModelClass(): myName = models.CharField(max_length=64) myFriends = ??? # what goes here? 由于列表是python中如此常见的数据结构,因此我希望它有一个Django模型字段。我知道我可以使用ManyToMany或OneToMany关系,但是我希望避免代码中的额外间接访问。 编辑: 我添加了这个相关问题,人们可能会发现它很有用。

10
将“熊猫”列中的字典/列表拆分为单独的列
我将数据保存在postgreSQL数据库中。我正在使用Python2.7查询此数据并将其转换为Pandas DataFrame。但是,此数据框的最后一列中包含值的字典(或列表?)。DataFrame看起来像这样: [1] df Station ID Pollutants 8809 {"a": "46", "b": "3", "c": "12"} 8810 {"a": "36", "b": "5", "c": "8"} 8811 {"b": "2", "c": "7"} 8812 {"c": "11"} 8813 {"a": "82", "c": "15"} 我需要将此列拆分为单独的列,以便DataFrame如下所示: [2] df2 Station ID a b c 8809 46 3 12 8810 36 5 8 …

17
无法在ubuntu中将默认python版本设置为python3
我试图将默认python版本设置为python3in Ubuntu 16.04。默认情况下为python2(2.7)。我遵循以下步骤: update-alternatives --remove python /usr/bin/python2 update-alternatives --install /usr/bin/python python /usr/bin/python3 但是第二条语句出现以下错误, rejeesh@rejeesh-Vostro-1015:~$ update-alternatives --install /usr/bin/python python /usr/bin/python3 update-alternatives: --install needs <link> <name> <path> <priority> Use 'update-alternatives --help' for program usage information. 我是Ubuntu的新手,我不知道自己在做什么错。

16
ImportError:在Windows 7 32位中运行pip --version命令时无法导入名称main
我已经为32位Windows安装了与pip和setuptools捆绑在一起的最新python(2.7.9)。我尝试重新安装pip,但问题仍然存在。 这是pip --version在Administrator cmd中运行后的错误: Traceback (most recent call last): File "D:\Python\lib\runpy.py", line 162, in _run_module_as_main "__main__", fname, loader, pkg_name) File "D:\Python\lib\runpy.py", line 72, in _run_code exec code in run_globals File "D:\Python\Scripts\pip.exe\__main__.py", line 5, in <module> ImportError: cannot import name main
145 python  pip  importerror 

9
运行`pip install`的Ubuntu给出错误'无法构建以下必需的软件包:* freetype'
执行时pip install -r requirements.txt,在安装阶段出现以下错误matplotlib: REQUIRED DEPENDENCIES AND EXTENSIONS numpy: yes [not found. pip may install it below.] dateutil: yes [dateutil was not found. It is required for date axis support. pip/easy_install may attempt to install it after matplotlib.] tornado: yes [tornado was not found. It is required for the WebAgg …

13
Python是否具有程序包/模块管理系统?
Python是否具有包/模块管理系统,类似于Ruby在哪里可以使用rubygems gem install packagename? 在“ 安装Python模块”上,我仅看到对的引用python setup.py install,但这需要您首先找到该软件包。
145 python  module 

6
有与Ruby的“ rvm”等效的python吗?
问:在Python中,我们有功能上与Ruby版本管理器“ rvm”等效的功能吗? (RVM使您可以轻松地在不同版本的ruby解释器和不同组的gem(模块)之间进行完全切换。有关解释器和 gems的download-build-install-switch的一切,都可以通过调用rvm来解决。全部以您的普通用户帐户运行。)
145 python  egg  equivalent  rvm 

8
python中有内置的标识函数吗?
我想指出一个什么都不做的函数: def identity(*args) return args 我的用例是这样的 try: gettext.find(...) ... _ = gettext.gettext else: _ = identity 当然,我可以使用identity上面定义的方法,但是内置方法肯定会运行得更快(并避免我自己引入的错误)​​。 显然,map与filter使用None的身份,但这是具体到它们的实现。 >>> _=None >>> _("hello") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not callable


8
可以使用scrapy从使用AJAX的网站中抓取动态内容吗?
我最近一直在学习Python,并全力以赴来构建网络抓取工具。一点都不花哨。其唯一目的是从博彩网站上获取数据,并将此数据放入Excel。 大多数问题都是可以解决的,我周围有些混乱。但是,我在一个问题上遇到了巨大的障碍。如果站点加载一张马表并列出当前的投注价格,则此信息不在任何源文件中。线索是这些数据有时是实时的,而数字显然是从某个远程服务器上更新的。我PC上的HTML只是有一个漏洞,他们的服务器正在推送我需要的所有有趣数据。 现在我对动态Web内容的经验很低,所以这件事使我难以理解。 我认为Java或Javascript是关键,这经常弹出。 刮板只是赔率比较引擎。有些网站有API,但对于那些没有的API则需要。我正在使用python 2.7的scrapy库 如果这个问题过于开放,我深表歉意。简而言之,我的问题是:如何使用scrapy来抓取此动态数据,以便可以使用它?这样我就可以实时抓取该赔率赔率数据?

18
如何检查两个列表在Python中是否循环相同
例如,我有以下列表: a[0] = [1, 1, 1, 0, 0] a[1] = [1, 1, 0, 0, 1] a[2] = [0, 1, 1, 1, 0] # and so on 它们似乎不同,但是如果假定起点和终点相连,那么它们在循环上是相同的。 问题是,每个列表的长度为55,并且仅包含三个1和52个零。如果没有循环条件,则有26,235(55选择3)个列表。但是,如果存在条件“循环”,则存在大量循环相同的列表 目前,我通过以下方式循环检查身份: def is_dup(a, b): for i in range(len(a)): if a == list(numpy.roll(b, i)): # shift b circularly by i return True return …
145 python  algorithm 

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.