Questions tagged «python»

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

5
Celery与RabbitMQ:AttributeError:'DisabledBackend'对象没有属性'_get_task_meta_for'
我正在运行 使用Celery教程第一步”。 我们定义以下任务: from celery import Celery app = Celery('tasks', broker='amqp://guest@localhost//') @app.task def add(x, y): return x + y 然后调用它: >>> from tasks import add >>> add.delay(4, 4) 但是我收到以下错误: AttributeError: 'DisabledBackend' object has no attribute '_get_task_meta_for' 我正在同时运行celery worker和Rabbit-mq服务器。奇怪的是,芹菜工人将任务报告为成功: [2014-04-22 19:12:03,608: INFO/MainProcess] Task test_celery.add[168c7d96-e41a-41c9-80f5-50b24dcaff73] succeeded in 0.000435483998444s: 19 为什么这不起作用?
71 python  celery 

2
Python:Pandas系列-为什么使用loc?
为什么我们对熊猫数据框使用“ loc”?似乎以下代码无论是否使用loc都以模拟速度运行 %timeit df_user1 = df.loc[df.user_id=='5561'] 100 loops, best of 3: 11.9 ms per loop 要么 %timeit df_user1_noloc = df[df.user_id=='5561'] 100 loops, best of 3: 12 ms per loop 那为什么要使用loc? 编辑:这已被标记为重复问题。但是,尽管熊猫iloc vs ix vs loc的解释?确实提到* 您可以只使用数据框的getitem进行列检索 : * df['time'] # equivalent to df.loc[:, 'time'] 尽管它确实解释了loc的许多功能,但它并没有说明我们为什么使用loc,但我的特定问题是“为什么不完全省略loc”?为此,我在下面接受了非常详细的答案。 另外,其他帖子的答案(我认为不是答案)在讨论中非常隐蔽,任何搜索我正在寻找的人的人都会发现很难找到信息,并且提供的答案会更好地为您服务我的问题。
71 python  pandas  series  loc 

6
pip install pygraphviz:找不到软件包“ libcgraph”
我成功安装graphviz并cgraph与 $ sudo pip install graphviz .... Successfully installed graphviz-0.5.1 $ sudo pip install cgraph ... Successfully installed cgraph-0.1 我No package 'libcgraph' found在跑步时遇到问题sudo pip install pygraphviz。以下是完整的堆栈跟踪。 $ sudo pip install pygraphviz The directory '/Users/sparkandshine/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been …

2
Elasticsearch:如何使用python删除索引
如果这是很基本的内容,请原谅我,但是我有Python 2.7和Elasticsearch 2.1.1,我只是想使用删除索引 es.delete(index='researchtest', doc_type='test') 但这给了我 return func(*args, params=params, **kwargs) TypeError: delete() takes at least 4 arguments (4 given) 我也试过 es.delete_by_query(index='researchtest', doc_type='test',body='{"query":{"match_all":{}}}') 但我明白了 AttributeError: 'Elasticsearch' object has no attribute 'delete_by_query' 知道为什么吗?API是否已针对python 2.1.1更改? https://elasticsearch-py.readthedocs.org/en/master/api.html#elasticsearch.client.IndicesClient.delete

6
熊猫:删除连续的重复项
在大熊猫中仅丢弃连续重复项的最有效方法是什么? drop_duplicates提供了以下内容: In [3]: a = pandas.Series([1,2,2,3,2], index=[1,2,3,4,5]) In [4]: a.drop_duplicates() Out[4]: 1 1 2 2 4 3 dtype: int64 但是我想要这个: In [4]: a.something() Out[4]: 1 1 2 2 4 3 5 2 dtype: int64
71 python  pandas 

5
Python defaultdict和lambda
在其他人的代码中,我阅读了以下两行: x = defaultdict(lambda: 0) y = defaultdict(lambda: defaultdict(lambda: 0)) 由于defaultdict的参数是默认工厂,因此我认为第一行表示当我为不存在的键k调用x [k](例如类似v = x [k]的语句)时,键值对(k ,0)会自动添加到字典中,就像首先执行语句x [k] = 0一样。我对么? 那y呢?似乎默认工厂将创建一个默认值为0的defaultdict。但这具体意味着什么?我试图在Python shell中尝试使用它,但无法弄清楚它到底是什么。

9
如何获得Python中两个字典之间的区别?
我有两个字典。我需要找到两者之间的区别,这应该给我关键和价值。 我已经搜索并找到了一些插件/软件包,例如datadiff,dictdiff-master,但是当我在Python 2.7中尝试时,它说没有定义这样的模块。 我在这里用套装。 first_dict = {} second_dict = {} value = set(second_dict)-set(first_dict) print value 输出>>> set([['SCD-3547','SCD-3456']) 我只有钥匙,我什至需要获取值。

4
如何从Jupyter笔记本上的* .IPYNB文件执行* .PY文件?
我正在使用Python笔记本工作,我希望将较大的输入代码[input] 打包到[* .PY]文件中,然后从Notebook调用此文件。 我知道从笔记本中运行[ .PY]文件的操作,该命令在Linux或Windows之间有所不同。但是当我执行此操作并从笔记本执行[.PY]文件时,它无法识别笔记本中加载的任何现有库或变量(就像[ .PY]文件从零开始...)。 有没有什么办法解决这一问题? 该问题的一个可能的简化示例如下: In[1]: import numpy as np import matplotlib.pyplot as plt In[2]: def f(x): return np.exp(-x ** 2) In[3]: x = np.linspace(-1, 3, 100) In[4]: %run script.py 其中“ script.py ”具有以下内容: plt.plot(x, f(x)) plt.xlabel("Eje $x$",fontsize=16) plt.ylabel("$f(x)$",fontsize=16) plt.title("Funcion $f(x)$") 在实际的问题中,文件[* .PY]没有4行代码,它具有更多的代码。
71 python  jupyter 

9
在无法调用更新错误“模块”对象后,pip不再起作用
点更新后,点已完全停止工作。 Z:\>pip install matplotlib Traceback (most recent call last): File "c:\program files\python37\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "c:\program files\python37\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Program Files\Python37\Scripts\pip.exe\__main__.py", line 9, in <module> TypeError: 'module' object is not callable 有什么帮助吗? 编辑:我正在Windows 10

4
使用virtualenv或buildout安装PIL的问题
当我使用easy_install或buildout安装PIL时,其安装方式必须是“导入映像”,而不是“从PIL导入映像”。 但是,如果我执行“ apt-get install python-imaging”或使用“ pip -E test_pil install PIL”,则一切正常。 以下是我如何尝试使用virtualenv安装PIL的示例: # virtualenv --no-site-packages test_pil # test_pil/bin/easy_install PIL # test_pil/bin/python Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import PIL Traceback (most recent …

3
如何在ubuntu上安装加密技术?
我的Ubuntu版本是14.04 LTS。 当我安装密码术时,错误是: Installing egg-scripts. uses namespace packages but the distribution does not require setuptools. Getting distribution for 'cryptography==0.2.1'. no previously-included directories found matching 'documentation/_build' zip_safe flag not set; analyzing archive contents... six: module references __path__ Installed /tmp/easy_install-oUz7ei/cryptography-0.2.1/.eggs/six-1.10.0-py2.7.egg Searching for cffi>=0.8 Reading https://pypi.python.org/simple/cffi/ Best match: cffi 1.5.0 Downloading https://pypi.python.org/packages/source/c/cffi/cffi-1.5.0.tar.gz#md5=dec8441e67880494ee881305059af656 Processing …

4
在Django 1.9中,我应该使用什么来代替syncdb?
看看这个: $ pypy ./manage.py syncdb /usr/lib64/pypy-2.4.0/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9 warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning) (cut) 我运行了一个快速的Google搜索,但找不到答案-我应该用什么代替syncdb?

16
Python和Intellisense
Python是否有等同于“ intellisense”的功能? 也许我不应该承认,但我发现拥有智能感知确实可以加快学习新语言的“发现阶段”。例如,由于片段和智能感知的帮助,从VB.net切换到C#变得轻而易举。


1
为什么在失败的int转换中此Python字符串的大小会更改
从这里的推文中: import sys x = 'ñ' print(sys.getsizeof(x)) int(x) #throws an error print(sys.getsizeof(x)) 对于两个getsizeof调用,我们得到74个字节,然后是77个字节。 看来我们从失败的int调用中向对象添加了3个字节。 来自Twitter的更多示例(您可能需要重新启动python才能将大小重置为74): x = 'ñ' y = 'ñ' int(x) print(sys.getsizeof(y)) 77! print(sys.getsizeof('ñ')) int('ñ') print(sys.getsizeof('ñ')) 74,然后77。

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.