Tag: python

mongodb光标ID无效错误

我正在尝试遍历这个循环: for doc in coll.find() 我在第100,000条logging上得到以下错误。 File "build\bdist.win32\egg\pymongo\cursor.py", line 703, in next File "build\bdist.win32\egg\pymongo\cursor.py", line 679, in _refresh File "build\bdist.win32\egg\pymongo\cursor.py", line 628, in __send_message File "build\bdist.win32\egg\pymongo\helpers.py", line 95, in _unpack_response pymongo.errors.OperationFailure: cursor id '1236484850793' not valid at server 这个错误是什么意思?

为什么从不同初始化集构造的元组是相等的?

我期待以下两个元组 >>> x = tuple(set([1, "a", "b", "c", "z", "f"])) >>> y = tuple(set(["a", "b", "c", "z", "f", 1])) 比较不平等,但他们不: >>> x == y >>> True 这是为什么?

您正尝试在不缺省的情况下向userprofile添加一个不可为空的字段'new_field'

我知道从Django 1.7我不需要使用南或任何其他迁移系统,所以我只是使用简单的命令python manage.py makemigrations 不过,我得到的只是这个错误: You are trying to add a non-nullable field 'new_field' to userprofile without a default; we can't do that (the database needs something to populate existing rows). 这里是models.py: class UserProfile(models.Model): user = models.OneToOneField(User) website = models.URLField(blank=True) new_field = models.CharField(max_length=140) 什么是选项?

为什么浮点字典键可以用相同的值覆盖整数键?

我正在通过http://www.mypythonquiz.com工作, 问题#45要求输出以下代码: confusion = {} confusion[1] = 1 confusion['1'] = 2 confusion[1.0] = 4 sum = 0 for k in confusion: sum += confusion[k] print sum 输出是6 ,因为键1.0取代了1 。 这对我来说有点危险,这是否是一个有用的语言function?

Python中的dir(…)和vars(…).keys()之间的区别?

Python中的dir(…)和vars(…).keys()之间有区别吗? (我希望有一个区别,否则这将打破“做到这一点”的原则… 🙂

我如何从Django的checkbox中获取多个值

我想要使​​用request.POST['xzy']作为列表来获取多选checkbox的值。 这是我的模型和模板代码。 我的模型 class Recommend(models.Model): user=models.ForeignKey(User) book=models.ForeignKey(BookModel) friends=models.ManyToManyField(User, related_name="recommended") 我的模板 {% for friend in friends %} <input type="checkbox" name="recommendations" id="option{{friend.id}}" value={{friend.username}} /> <label for="option{{friend.id}}"><b>{{friend.username}}</b></label><br /> {% endfor %} 我的查看代码 if request.method == 'POST': recommendations=request.POST['recommendations'] 在这里,我希望'build议'是一个包含所有朋友id的列表,但是这里只是被覆盖,并且只包含最后一个循环迭代中分配的值。 我怎么解决这个问题。 需要帮助绝望。 谢谢。

Clojure与Numpy的matrix乘法

我正在使用Clojure中的一个应用程序,需要将大型matrix乘法,并且遇到一些性能问题,与相同的Numpy版本相比。 Numpy似乎能够在一秒之内通过转置乘以一个1,000,000×23的matrix,而相同的clojure代码则需要六分钟。 (我可以打印出Numpy生成的matrix,所以它肯定是评估一切)。 我在这个Clojure代码中做了一些非常错误的事情吗? 我可以尝试模仿Numpy的一些技巧吗? 这是python: import numpy as np def test_my_mult(n): A = np.random.rand(n*23).reshape(n,23) At = AT t0 = time.time() res = np.dot(AT, A) print time.time() – t0 print np.shape(res) return res # Example (returns a 23×23 matrix): # >>> results = test_my_mult(1000000) # # 0.906938076019 # (23, 23) 和clojure: (defn feature-vec [n] (map […]

subprocess改变目录

我想在一个子目录/超目录中执行一个脚本(我需要先在这个子目录/超级目录中)。 我不能让subprocess进入我的子目录: tducin@localhost:~/Projekty/tests/ve$ python Python 2.7.4 (default, Sep 26 2013, 03:20:26) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import subprocess >>> import os >>> os.getcwd() '/home/tducin/Projekty/tests/ve' >>> subprocess.call(['cd ..']) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/subprocess.py", line 524, in call return Popen(*popenargs, […]

我如何获得一个Python类的名称作为一个string?

我叫什么方法来获得一个class的名字?

Python opencv2(cv2)包装获取图像大小?

如何在Python OpenCV(numpy)的cv2包装中获取图像的大小。 有没有比numpy.shape()正确的方法。 如何获得格式维度:(宽度,高度)列表? 谢谢