15

this code doesn't run as expected in Python3 when I do my data-analysing practice.

The typeerror is "TypeError: unsupported operand type(s) for /: 'dict_values' and 'int'".

How should I solve it?

import numpy as np
# Summarize the data about minutes spent in the classroom
total_minutes = total_minutes_by_account.values()
total_minutes = np.array(total_minutes)
print('Mean:', np.mean(total_minutes))
print('Standard deviation:', np.std(total_minutes))
print('Minimum:', np.min(total_minutes))
print('Maximum:', np.max(total_minutes))
1
  • 5
    In python3, dict.values returns a dict_values object, which is not a list or tuple. Try coercing that into a list. total_minutes = list(total_minutes_by_account.values()).
    – Abdou
    Apr 27, 2017 at 16:40

4 Answers 4

20

This code was giving me some unexpected trouble when going through this lesson as well, but I got it to work by making the following changes:

import numpy as np

total_minutes = list(total_minutes_by_account.values())
print ('Mean:', np.mean(total_minutes))
print ('Standard Deviation:', np.std(total_minutes))
print ('Minimum:', np.min(total_minutes))
print ('Maximum:', np.max(total_minutes))
15

Hopefully this helps:

The class is written in Python 2, where Dict.values() returns a list, but this was updated in Python 3 to return a dictionary view, described here: https://docs.python.org/3/library/stdtypes.html#dict-views

It's a potentially helpful change because the view will update when the content of the dictionary updates, but they don't behave like a List, and numpy's mean, std, min, and max all take a list as their argument.

11
total_minutes = total_minutes_by_account.values()

The variable total_minutes will be of type dict_values. To turn it into a list you need to wrap it in a list function like this:

total_minutes = list(total_minutes_by_account.values())
1

A very good example is given by @hpaulj at ( not being able to do numpy operations on values on a dictionary )

Find below a summary of his answer. It helped me a lot.

In [1618]: dd = {'a':[1,2,3], 'b':[4,5,6]}
In [1619]: dd
Out[1619]: {'a': [1, 2, 3], 'b': [4, 5, 6]}
In [1620]: dd.values()
Out[1620]: dict_values([[1, 2, 3], [4, 5, 6]])
In [1621]: np.mean(dd.values())
... 
TypeError: unsupported operand type(s) for /: 'dict_values' and 'int'

Solution: convert the dict_values to list:

In [1623]: list(dd.values())
Out[1623]: [[1, 2, 3], [4, 5, 6]]
In [1624]: np.mean(list(dd.values()))
Out[1624]: 3.5

In Py3, range and dict.keys() require the same extra touch.

========

np.mean first tries to convert the input to an array, but with values() that isn't what we want. It makes a single item object array containing this whole object.

In [1626]: np.array(dd.values())
Out[1626]: array(dict_values([[1, 2, 3], [4, 5, 6]]), dtype=object)
In [1627]: _.shape
Out[1627]: ()
In [1628]: np.array(list(dd.values()))
Out[1628]: 
array([[1, 2, 3],
       [4, 5, 6]])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.