Questions tagged «python»

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

23
Django升级到1.9错误“ AppRegistryNotReady:应用尚未加载。”
从1.8升级到django 1.9时,出现此错误。我检查了类似问题的答案,但我认为这不是任何第三方包装或应用程序的问题。 Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/home/kishore/.virtualenvs/andone/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line utility.execute() File "/home/kishore/.virtualenvs/andone/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 342, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/kishore/.virtualenvs/andone/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 176, in fetch_command commands = get_commands() File "/home/kishore/.virtualenvs/andone/local/lib/python2.7/site-packages/django/utils/lru_cache.py", line 100, in wrapper result = user_function(*args, **kwds) File "/home/kishore/.virtualenvs/andone/local/lib/python2.7/site-packages/django/core/management/__init__.py", line …
93 python  django 

6
熊猫Groupby累积总和
我想向我的Pandas数据框添加一个累加和列,以便: name | day | no -----|-----------|---- Jack | Monday | 10 Jack | Tuesday | 20 Jack | Tuesday | 10 Jack | Wednesday | 50 Jill | Monday | 40 Jill | Wednesday | 110 变成: Jack | Monday | 10 | 10 Jack | Tuesday | 30 …
93 python  pandas 

2
Python泡菜错误:UnicodeDecodeError
我正在尝试使用Textblob进行一些文本分类。我首先训练模型并使用pickle对其进行序列化,如下所示。 import pickle from textblob.classifiers import NaiveBayesClassifier with open('sample.csv', 'r') as fp: cl = NaiveBayesClassifier(fp, format="csv") f = open('sample_classifier.pickle', 'wb') pickle.dump(cl, f) f.close() 当我尝试运行此文件时: import pickle f = open('sample_classifier.pickle', encoding="utf8") cl = pickle.load(f) f.close() 我收到此错误: UnicodeDecodeError:'utf-8'编解码器无法解码位置0的字节0x80:无效的起始字节 以下是我的sample.csv的内容: 我的SQL根本无法正常工作。这是一个错误的选择,SQL 我有问题。请立即回复,支持 我在哪里错了?请帮忙。

21
python中的AWS Lambda导入模块错误
我正在创建一个AWS Lambda python部署程序包。我正在使用一个外部依赖项请求。我使用AWS文档http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html安装了外部依赖项。以下是我的python代码。 import requests print('Loading function') s3 = boto3.client('s3') def lambda_handler(event, context): #print("Received event: " + json.dumps(event, indent=2)) # Get the object from the event and show its content type bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8') try: response = s3.get_object(Bucket=bucket, Key=key) s3.download_file(bucket,key, '/tmp/data.txt') lines = [line.rstrip('\n') for line in …

11
如何使用Redis存储和检索字典
# I have the dictionary my_dict my_dict = { 'var1' : 5 'var2' : 9 } r = redis.StrictRedis() 我将如何存储my_dict并使用Redis进行检索。例如,以下代码不起作用。 #Code that doesn't work r.set('this_dict', my_dict) # to store my_dict in this_dict r.get('this_dict') # to retrieve my_dict
93 python  redis 



9
ImportError:无法导入名称NUMPY_MKL
我正在尝试运行以下简单代码 import scipy scipy.test() 但是我收到以下错误 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 586, in runfile execfile(filename, namespace) File "C:/Users/Mustafa/Documents/My Python Code/SpectralGraphAnalysis/main.py", line 8, in <module> import scipy File "C:\Python27\lib\site-packages\scipy\__init__.py", line 61, in <module> from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl ImportError: cannot import …

3
为什么max比排序慢?
我发现它max比sortPython 2和3中的函数慢。 Python 2 $ python -m timeit -s 'import random;a=range(10000);random.shuffle(a)' 'a.sort();a[-1]' 1000 loops, best of 3: 239 usec per loop $ python -m timeit -s 'import random;a=range(10000);random.shuffle(a)' 'max(a)' 1000 loops, best of 3: 342 usec per loop Python 3 $ python3 -m timeit -s 'import random;a=list(range(10000));random.shuffle(a)' 'a.sort();a[-1]' 1000 loops, …


10
寻找一个好的Python Tree数据结构
关闭。此问题不符合堆栈溢出准则。它当前不接受答案。 想改善这个问题吗?更新问题,使其成为Stack Overflow 的主题。 5年前关闭。 改善这个问题 我正在寻找一个好的Tree数据结构类。我遇到过这个软件包,但是由于我对Python相对较新(不是编程),所以我不知道是否还有更好的软件包。 我想在这里听到来自Pythonista的消息-您是否有喜欢的树脚本经常使用并会推荐使用? [编辑] 为了澄清,用“树”来表示,是指一个简单的无序树(嗯,这是一个递归定义,但希望可以使情况有所澄清)。关于我需要的树(即用例)。我正在从一个平面文件中读取树数据,并且需要从该数据构建一棵树并遍历树中的所有节点。
92 python 

8
matplotlib:将轴偏移值格式化为整数或特定数字
我有一个matplotlib图,该图正在绘制始终被称为纳秒(1e-9)的数据。在y轴上,如果我有数十纳秒的数据,即 在44e-9中,轴上的值显示为4.4,其中+ 1e-8为偏移量。无论如何,有强制轴以+ 1e-9偏移显示44吗? 我的x轴也是如此,该轴显示+ 5.54478e4,我希望它显示+55447的偏移量(整数,无小数-该值以天为单位)。 我已经尝试了一些类似的事情: p = axes.plot(x,y) p.ticklabel_format(style='plain') 对于x轴,但这不起作用,尽管我可能使用不正确或对文档中的内容有误解,有人可以指出我正确的方向吗? 谢谢乔纳森 我尝试使用格式化程序进行操作,但尚未找到任何解决方案...: myyfmt = ScalarFormatter(useOffset=True) myyfmt._set_offset(1e9) axes.get_yaxis().set_major_formatter(myyfmt) 和 myxfmt = ScalarFormatter(useOffset=True) myxfmt.set_portlimits((-9,5)) axes.get_xaxis().set_major_formatter(myxfmt) 附带一提,我实际上对“偏移数”对象实际位于何处感到困惑……它是主要/次要刻度线的一部分吗?


5
测试numpy数组是否仅包含零
我们初始化一个具有零的numpy数组,如下所示: np.zeros((N,N+1)) 但是,如何检查给定n * n numpy数组矩阵中的所有元素是否为零。 如果所有值确实为零,则该方法仅需要返回True。
92 python  numpy 


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.