[英]ValueError: too many values to unpack
I am trying to sort dictionaries in MongoDB. 我正在尝试对MongoDB中的字典进行排序。 However, I get the value error "too many values to unpack" because I think it's implying that there are too many values in each dictionary (there are 16 values in each one). 但是,出现值错误“无法解包的值太多”,因为我认为这意味着每个字典中的值太多(每个字典中有16个值)。 This is my code: 这是我的代码:
FortyMinute.find().sort(['Rank', 1])
Anyone know how to get around this? 有人知道如何解决这个问题吗?
EDIT: Full traceback 编辑:完全追溯
Traceback (most recent call last):
File "main.py", line 33, in <module>
main(sys.argv[1:])
File "main.py", line 21, in main
fm.readFortyMinute(args[0])
File "/Users/Yih-Jen/Documents/Rowing Project/FortyMinute.py", line 71, in readFortyMinute
writeFortyMinute(FortyMinData)
File "/Users/Yih-Jen/Documents/Rowing Project/FortyMinute.py", line 104, in writeFortyMinute
FortyMinute.find().sort(['Rank', 1])
File "/Users/Yih-Jen/anaconda/lib/python2.7/site-packages/pymongo/cursor.py", line 692, in sort
self.__ordering = helpers._index_document(keys)
File "/Users/Yih-Jen/anaconda/lib/python2.7/site-packages/pymongo/helpers.py", line 65, in _index_document
for (key, value) in index_list:
ValueError: too many values to unpack
You pass the arguments and values in unpacked as so: 您可以这样解压传递参数和值:
FortyMinute.find().sort('Rank', 1)
FortyMinute.find().sort([(Rank', 1), ('Date', 1)])
Cursor.sort
documentation linked below recommends using pymongo.DESCENDING
and pymongo.ASCENDING
instead of 1 and -1;
提示:即使下面链接的Cursor.sort
文档也建议使用pymongo.DESCENDING
和pymongo.ASCENDING
而不是1和-1;
in general, you should use descriptive variable names instead of magic constants in your code as so:
通常,应该在代码中使用描述性变量名称而不是魔术常数,如下所示:
FortyMinute.find().sort('Rank',pymongo.DESCENDING)
sorted
accepts iterators and not just sequences it might be more inefficient and nonstandard:
但即使思想sorted
可以接受迭代器,而不仅是序列,它可能效率更低,更不规范:
sorted(FortyMinute.find(), key=key_function)
where you might define key_function
to return the Rank
column of a record. 您可以在其中定义key_function
以返回记录的Rank
列。
If you want mong/pymongo to sort: 如果您想对mong / pymongo进行排序:
FortyMinute.find().sort('Rank', 1)
If you want to sort using multiple fields: 如果要使用多个字段进行排序:
FortyMinute.find().sort([('Rank': 1,), ('other', -1,)])
You also have constants to make it more clear what you're doing: 您还可以使用常量来使自己更清楚自己在做什么:
FortyMinute.find().sort('Rank',pymongo.DESCENDING)
If you want to sort in python first you have to return the result and use a sorting method in python: 如果要首先在python中排序,则必须返回结果并在python中使用排序方法:
sorted(FortyMinute.find(), key=<some key...>)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.