I have this bit of code:
visits = defaultdict(int)
for t in tweetsSQL:
visits[t.user.from_user] += 1
I looked at some examples online that used the sorted method like so:
sorted(visits.iteritems, key=operator.itemgetter(1), reverse=True)
but it is giving me:
"TypeError: 'builtin_function_or_method' object is not iterable"
I am not sure why.
iteritems is a method. You need parenthesis to call it: visits.iteritems()
.
As it stands now, you are passing the iteritems method itself to sorted
which is why it is complaining that it can't iterate over a function or method.
Personally I think one of these forms is a little more succinct as the first argument only needs to be an iterable not an iterator.
sorted_keys = sorted(visits.keys(), reverse=True)
sorted_keys = visits.keys().sort(reverse=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.