python-sortedcontainers icon indicating copy to clipboard operation
python-sortedcontainers copied to clipboard

float index problem.

Open theoneisneo opened this issue 3 years ago • 4 comments
trafficstars

All index related part, can use index = -1.0 or 0.0 But float should not be allowed for index using.

theoneisneo avatar May 17 '22 10:05 theoneisneo

Why?

grantjenks avatar May 17 '22 12:05 grantjenks

Users may think SortList is a kind of list. So the index behavior should be the same. But normal list do not allow float index.

On the other hand, SortedList seems only accept float index 0.0 or -1.0, can not use float like 1.0, -2.0 I think the behavior should be the same.

But this is not a big problem.

could see ipython code and log below:

Python 3.9.7 (default, Sep 16 2021, 08:50:36) Type 'copyright', 'credits' or 'license' for more information IPython 8.3.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from sortedcontainers import SortedList

In [2]: sl = SortedList()

In [3]: sl.update([4,65,72,2,99,42])

In [4]: sl Out[4]: SortedList([2, 4, 42, 65, 72, 99])

In [5]: normal_list = list(sl)

In [6]: normal_list Out[6]: [2, 4, 42, 65, 72, 99]

In [7]: sl[0.0] Out[7]: 2

In [8]: normal_list[0.0]

TypeError Traceback (most recent call last) Input In [8], in <cell line: 1>() ----> 1 normal_list[0.0]

TypeError: list indices must be integers or slices, not float

In [9]: sl[1.0]

TypeError Traceback (most recent call last) Input In [9], in <cell line: 1>() ----> 1 sl[1.0]

File /opt/anaconda3/lib/python3.9/site-packages/sortedcontainers/sortedlist.py:894, in SortedList.getitem(self, index) 891 raise IndexError('list index out of range') 893 if 0 <= index < len(_lists[0]): --> 894 return _lists[0][index] 896 len_last = len(_lists[-1]) 898 if -len_last < index < 0:

TypeError: list indices must be integers or slices, not float

In [10]: sl[-2.0]

TypeError Traceback (most recent call last) Input In [10], in <cell line: 1>() ----> 1 sl[-2.0]

File /opt/anaconda3/lib/python3.9/site-packages/sortedcontainers/sortedlist.py:899, in SortedList.getitem(self, index) 896 len_last = len(_lists[-1]) 898 if -len_last < index < 0: --> 899 return _lists[-1][len_last + index] 901 pos, idx = self._pos(index) 902 return _lists[pos][idx]

TypeError: list indices must be integers or slices, not float

theoneisneo avatar May 17 '22 13:05 theoneisneo

🤷‍♂️ seems low priority

grantjenks avatar May 17 '22 15:05 grantjenks

Users may think SortList is a kind of list. So the index behavior should be the same. But normal list do not allow float index.

On the other hand, SortedList seems only accept float index 0.0 or -1.0, can not use float like 1.0, -2.0 I think the behavior should be the same.

But this is not a big problem.

could see ipython code and log below:

Python 3.9.7 (default, Sep 16 2021, 08:50:36) Type 'copyright', 'credits' or 'license' for more information IPython 8.3.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from sortedcontainers import SortedList

In [2]: sl = SortedList()

In [3]: sl.update([4,65,72,2,99,42])

In [4]: sl Out[4]: SortedList([2, 4, 42, 65, 72, 99])

In [5]: normal_list = list(sl)

In [6]: normal_list Out[6]: [2, 4, 42, 65, 72, 99]

In [7]: sl[0.0] Out[7]: 2

In [8]: normal_list[0.0]

TypeError Traceback (most recent call last) Input In [8], in <cell line: 1>() ----> 1 normal_list[0.0]

TypeError: list indices must be integers or slices, not float

In [9]: sl[1.0]

TypeError Traceback (most recent call last) Input In [9], in <cell line: 1>() ----> 1 sl[1.0]

File /opt/anaconda3/lib/python3.9/site-packages/sortedcontainers/sortedlist.py:894, in SortedList.getitem(self, index) 891 raise IndexError('list index out of range') 893 if 0 <= index < len(_lists[0]): --> 894 return _lists[0][index] 896 len_last = len(_lists[-1]) 898 if -len_last < index < 0:

TypeError: list indices must be integers or slices, not float

In [10]: sl[-2.0]

TypeError Traceback (most recent call last) Input In [10], in <cell line: 1>() ----> 1 sl[-2.0]

File /opt/anaconda3/lib/python3.9/site-packages/sortedcontainers/sortedlist.py:899, in SortedList.getitem(self, index) 896 len_last = len(_lists[-1]) 898 if -len_last < index < 0: --> 899 return _lists[-1][len_last + index] 901 pos, idx = self._pos(index) 902 return _lists[pos][idx]

TypeError: list indices must be integers or slices, not float

I think when you convert it to a list, the behavior when you use it belongs to the list (of Python). I think we don't need to change because the behavior of 2 separate data structures

nguyenchicuongvn avatar Jun 20 '22 11:06 nguyenchicuongvn