这是我的代码
[temp.append(i.get_hot_post(3)) for i in node_list]
[hot_posts+i.sort(key=sort_by_rate) for i in temp ]get_hot_posts()以这种方式返回包含3个项目的列表
return recent_posts[0:amount-1]可能是列表少于3个元素,它可能会把事情搞乱,但还会继续
[temp.append(i.get_hot_post(3)) for i in node_list]在这个命令之后,在"temp“中,我有一个列表列表,这是很好的。
但是当它执行的时候
[hot_posts+i.sort(key=sort_by_rate) for i in temp ]它会给出这个错误
TypeError: can only concatenate list (not "NoneType") to list发布于 2012-08-23 03:35:59
我想你指的是sorted(i),不是吗?i.sort()就地进行排序,不返回任何内容。
还有,为什么你想做hot_posts + ...?这不会将值存储在hot_posts中,因此该操作没有意义,除非将结果赋给一个新变量。
我怀疑你想做像这样的事情
temp = [i.get_hot_post(3) for i in node_list]
hot_posts = [sorted(i, key=sort_by_rate) for i in temp]虽然我不知道你的最后一行应该做什么。现在,它只对三个小列表中的每一个进行排序,仅此而已。
发布于 2012-08-23 03:42:32
List方法sort返回None (只是更改list)。您可以使用sorted()函数。
PS。
[temp.append(i.get_hot_post(3)) for i in node_list]这不是一个好主意,因为你会有一个None的列表。可能的变体:
temp += [i.get_hot_post(3) for i in node_list]甚至是
from operator import methodcaller
temp += map(methodcaller(get_hot_post, 3), node_list)https://stackoverflow.com/questions/12080169
复制相似问题