Reputation: 25397
I have a list r
and a list l
r = get_r()
l = [['a', 'b'], ['a'], ['c']]
print('-')
[print(type(el)) for el in l];
print('-')
[print(type(el)) for el in r];
will give us
-
<class 'list'>
<class 'list'>
<class 'list'>
-
<class 'list'>
<class 'list'>
<class 'list'>
So let's print r
and l
print(l)
print(r)
will give us
[['a', 'b'], ['a'], ['c']]
[['Schadenersatzrecht'], ['Abgabenrecht, Finanzrecht und Steuerrecht; Verfahrensrecht'], ['Europarecht']]
But now as I np.sum
those lists up:
np.sum(l)
np.sum(r)
I get:
['a', 'b', 'a', 'c']
but also:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-158-0d0bce575128> in <module>()
----> 1 np.sum(r)
~/miniconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py in sum(a, axis, dtype, out, keepdims)
1832 return sum(axis=axis, dtype=dtype, out=out, **kwargs)
1833 return _methods._sum(a, axis=axis, dtype=dtype,
-> 1834 out=out, **kwargs)
1835
1836
~/miniconda3/lib/python3.6/site-packages/numpy/core/_methods.py in _sum(a, axis, dtype, out, keepdims)
30
31 def _sum(a, axis=None, dtype=None, out=None, keepdims=False):
---> 32 return umr_sum(a, axis, dtype, out, keepdims)
33
34 def _prod(a, axis=None, dtype=None, out=None, keepdims=False):
TypeError: cannot perform reduce with flexible type
I totally not understand what the problem is. Even printing the type in the lists of the elements in those lists [print(type(el[0])) for el in r];
will give me <class 'str'>
as a result. The types are identical - at least from what I can tell yet np.sum
won't work on r
.
I am not sure if one can tell what the issue is from what you can see here. The only information I can provide in addition is that r
comes from a parsed JSON - however, I don't see why this would affect the outcome.
Upvotes: 2
Views: 1427
Reputation: 78556
Using np.sum
for flattening your lists is a bad idea. Before np.sum
is performed, both lists are coerced to np array types, which have different dtypes (although that is slightly aside the reason for failure).
np.array(l).dtype != np.array(r).dtype
You generally don't want to use np.sum
for this.
Simply flatten your list of lists the Pythonic way using list comprehensions:
l = [x for lst in l for x in lst]
r = [x for lst in r for x in lst]
Upvotes: 2