Questions tagged «python»

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

4
根据列表索引选择熊猫行
我有一个数据框df: 20060930 10.103 NaN 10.103 7.981 20061231 15.915 NaN 15.915 12.686 20070331 3.196 NaN 3.196 2.710 20070630 7.907 NaN 7.907 6.459 然后,我想选择列表中指示的具有某些序列号的行,假设这里是[1,3],然后向左移: 20061231 15.915 NaN 15.915 12.686 20070630 7.907 NaN 7.907 6.459 如何或什么功能可以做到这一点?
105 python  pandas 

9
如何使用pyplot.barh()在每个条形上显示条形的值?
我生成了条形图,如何在每个条形上显示条形的值? 当前情节: 我想要得到的是: 我的代码: import os import numpy as np import matplotlib.pyplot as plt x = [u'INFO', u'CUISINE', u'TYPE_OF_PLACE', u'DRINK', u'PLACE', u'MEAL_TIME', u'DISH', u'NEIGHBOURHOOD'] y = [160, 167, 137, 18, 120, 36, 155, 130] fig, ax = plt.subplots() width = 0.75 # the width of the bars ind = np.arange(len(y)) …

16
获取数的所有除数的最佳方法是什么?
这是非常愚蠢的方式: def divisorGenerator(n): for i in xrange(1,n/2+1): if n%i == 0: yield i yield n 我想要得到的结果与此类似,但是我想要一个更智能的算法(这个算法太慢而且太笨了:-) 我可以很快找到主要因素及其多样性。我有一个生成器以这种方式生成因子: (因数1,多重性1)(因数 2,多重性2) (因数3,多重性3) 等等... 即输出 for i in factorGenerator(100): print i 是: (2, 2) (5, 2) 我不知道这对我想做的事情有多大帮助(我为其他问题编写了代码),无论如何,我都希望有一种更聪明的制作方法 for i in divisorGen(100): print i 输出: 1 2 4 5 10 20 25 50 100 …
105 python  algorithm  math 

6
正则表达式匹配多行文本块
与跨多行的文本进行匹配时,让Python正则表达式无法正常工作有点麻烦。示例文本为(“ \ n”是换行符) some Varying TEXT\n \n DSJFKDAFJKDAFJDSAKFJADSFLKDLAFKDSAF\n [more of the above, ending with a newline]\n [yep, there is a variable number of lines here]\n \n (repeat the above a few hundred times). 我想捕获两件事:“ some_Varying_TEXT”部分,以及一次捕获中位于其下方两行的所有大写文本行(我以后可以去除换行符)。我尝试了几种方法: re.compile(r"^>(\w+)$$([.$]+)^$", re.MULTILINE) # try to capture both parts re.compile(r"(^[^>][\w\s]+)$", re.MULTILINE|re.DOTALL) # just textlines 并有很多变化,没有运气。最后一个似乎与文本行一一对应,这不是我真正想要的。我可以抓住第一部分,没问题,但是我似乎无法抓住4-5行的大写文本。我希望match.group(1)是some_Varying_Text,而group(2)是line1 + …
105 python  regex  multiline 

6
在include()中使用名称空间时有关app_name的ImproperlyConfiguredError
我目前正在尝试Django。我在urls.py namespace中的一个参数中使用了参数。include()当我运行服务器并尝试浏览时,出现此错误。 File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\urls\conf.py", line 39, in include 'Specifying a namespace in include() without providing an app_name ' django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name …


14
Django,创建自定义500/404错误页面
完全按照此处找到的教程进行操作,我无法创建自定义的500或404错误页面。如果我确实输入了错误的网址,则该页面会显示默认的错误页面。我应该检查哪些内容以防止显示自定义页面? 文件目录: mysite/ mysite/ __init__.py __init__.pyc settings.py settings.pyc urls.py urls.pyc wsgi.py wsgi.pyc polls/ templates/ admin/ base_site.html 404.html 500.html polls/ detail.html index.html __init__.py __init__.pyc admin.py admin.pyc models.py models.pyc tests.py urls.py urls.pyc view.py views.pyc templates/ manage.py 在mysite / settings.py中,我启用了以下功能: DEBUG = False TEMPLATE_DEBUG = DEBUG #.... TEMPLATE_DIRS = ( 'C:/Users/Me/Django/mysite/templates', ) 在mysite …

6
Python Pandas将列表插入单元格
我有一个列表“ abc”和一个数据框“ df”: abc = ['foo', 'bar'] df = A B 0 12 NaN 1 23 NaN 我想将列表插入单元格1B中,所以我想要这个结果: A B 0 12 NaN 1 23 ['foo', 'bar'] 我能做到吗? 1)如果我使用这个: df.ix[1,'B'] = abc 我收到以下错误消息: ValueError: Must have equal len keys and value when setting with an iterable 因为它尝试将列表(具有两个元素)插入行/列而不插入单元格。 2)如果我使用这个: df.ix[1,'B'] = …

3
'python setup.py install'和'pip install'之间的区别
我有一个要从tar文件安装到python virtualenv中的外部软件包。安装软件包的最佳方法是什么? 我发现了两种方法可以做到这一点: 提取tar文件,然后python setup.py install在提取的目录中运行。 pip install packagename.tar.gz来自https://pip.pypa.io/en/stable/reference/pip_install/#examples中的示例7 这两种方式在做上是否有区别。





2
定义type.Dict和dict之间的区别?
我正在练习在Python 3.5中使用类型提示。我的一位同事使用typing.Dict: import typing def change_bandwidths(new_bandwidths: typing.Dict, user_id: int, user_name: str) -> bool: print(new_bandwidths, user_id, user_name) return False def my_change_bandwidths(new_bandwidths: dict, user_id: int, user_name: str) ->bool: print(new_bandwidths, user_id, user_name) return True def main(): my_id, my_name = 23, "Tiras" simple_dict = {"Hello": "Moon"} change_bandwidths(simple_dict, my_id, my_name) new_dict = {"new": "energy source"} …

3
如何在python中将项目添加到空集
我有以下步骤: def myProc(invIndex, keyWord): D={} for i in range(len(keyWord)): if keyWord[i] in invIndex.keys(): D.update(invIndex[query[i]]) return D 但是我收到以下错误: Traceback (most recent call last): File "<stdin>", line 3, in <module> TypeError: cannot convert dictionary update sequence element #0 to a sequence 如果D包含元素,则不会出现任何错误。但是我需要D在开始时为空。
104 python  set 

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.