D
Dennis Lee Bieber
Step one: Obtain a processor that uses ones-complement arithmetic.So how do I express a -0? Which should point to the gap after the last
item.
Step two: Port Python to said processor...
...
Step one: Obtain a processor that uses ones-complement arithmetic.So how do I express a -0? Which should point to the gap after the last
item.
Ah, the good old Univac 418 ... [drools into beard and mumbles]Dennis said:Step one: Obtain a processor that uses ones-complement arithmetic.
Unfortunately the 418 would probably come in at about a micro-pystone,Step two: Port Python to said processor...
Terry said:You just did ;-) but I probably do not know what you mean.
The slice index of the gap after the last item is len(seq).
As I posted before (but perhaps it arrived after you sent this), one number
indexing rounds down, introducing a slight asymmetry.
All of the following get the center 'd' from the string.
a = 'abcdefg'
print a[3] # d 4 gaps from beginning
print a[-4] # d 5 gaps from end
It is 3 and 4 gaps *from* the left and right end to the left side of the
'd'. You can also see the asymmetry as coming from rounding 3.5 and -3.5
down to 3 and down to -4.
print a[3:4] # d
print a[-4:-3] # d
These are is symmetric, as we claimed.
print a[3:2:-1] # d These are symetric?!
print a[-4:-5:-1] # d
print a[3:-5:-1] # d
print a[-4:2:-1] # d
The pattern seems to be: left-gap-index : farther-to-left-index : -1 is
somehow equivalent to left:right, but I never paid much attention to
strides and don't know the full rule.
Stride slices are really a different subject from two-gap slicing. They
were introduced in the early years of Python specificly and only for
Numerical Python. The rules were those needed specificly for Numerical
Python arrays. They was made valid for general sequence use only a few
years ago. I would say that they are only for careful mid-level to expert
use by those who actually need them for their code.
The problem with negative index's are that positive index's are zero
based, but negative index's are 1 based. Which leads to a non
symmetrical situations.
Note that you can insert an item before the first item using slices. But
not after the last item without using len(list) or some value larger
than len(list).
"[Zbrx(2)]: 'end'">>> zlast = Zbrx(0)
>>> zbr10 = Zbrxlist(range(10))
>>> zbr10[zlast] '[Zbrx(0)]: 9'
>>> zbr10[zlast:] '[9:None:None]: [9]'
>>> zbr10[zlast:zlast] = ['end']
>>> zbr10 Zbrxlist([0, 1, 2, 3, 4, 5, 6, 7, 8, 'end', 9])
>>> ztop = Zbrx(-1)
>>> zbr10[ztop:ztop] = ['final']
>>> zbr10 Zbrxlist([0, 1, 2, 3, 4, 5, 6, 7, 8, 'end', 9, 'final'])
>>> zbr10[zlast:] "[11:None:None]: ['final']"
>>> zbr10[zlast] "[Zbrx(0)]: 'final'"
>>> zbr10[zlast+1] '[Zbrx(1)]: 9'
>>> zbr10[zlast+2]
Zbrxlist(['a', 'b', 'c', 'd', 'e'])['a', 'b', 'c', 'd', 'e', 'end']a = list('abcde')
a[len(a):len(a)] = ['end']
a
['a', 'b', 'c', 'd', 'e', 'last', 'end'] # Second to last.a[-1:-1] = ['last']
a
['a', 'b', 'c', 'd', 'e', 'last', 'end', 'final']a[100:100] = ['final']
a
Zbrxlist(['a', 'b', 'c', 'd', 'e', 'end'])>>> a[len(a.value):len(a.value)] = ['end']
>>> a
Zbrxlist(['a', 'b', 'c', 'd', 'e', 'last', 'end'])>>> a[lastx] "[Zbrx(0)]: 'end'"
>>> a[lastx:lastx] = ['last']
>>> a
Zbrxlist(['a', 'b', 'c', 'd', 'e', 'last', 'end', 'final'])>>> a[lastx-1:lastx-1] = ['final']
>>> a
Ron Adam said:Terry said:You just did ;-) but I probably do not know what you mean.
b[-1:] = ['Z'] # replaces last item
b[-1:-0] = ['Z'] # this doesn't work
If you are using negative index slices, you need to check for end
conditions because you can't address the end of the slice in a
sequential/numerical way.
I didn't see that one,
I don't think people would miss negative strides much if they were
removed. Replacing these case's with reverse() methods shouldn't be that
difficult.
Terry said:b[-1:] = ['Z'] # replaces last item
b[-1:-0] = ['Z'] # this doesn't work
If you are using negative index slices, you need to check for end
conditions because you can't address the end of the slice in a
sequential/numerical way.
OK, now I understand your question, the answer 'get a one's complement
machine', and your point that without a -0 separate from +0, there is a
subtle asymmetry that is easy to overlook.
Perhaps you did not see my other long post, where I drew ascii pictures?
In the US (and UK?), the ground level floor of a multifloor building is the
first floor. In continental Europe (all or just some?), the ground floor
is the ground (effectively zeroth) floor while the first floor up is the
first stage (resting place on the stairway).
Bengt said:IMO the problem is that the index sign is doing two jobs, which for zero-based
reverse indexing have to be separate: i.e., to show direction _and_ a _signed_
offset which needs to be realtive to the direction and base position.
A list-like class, and an option to use a zero-based reverse index will illustrate:
... def __init__(self, value=0):
... self.value = value
... def __repr__(self): return 'Zbrx(%r)'%self.value
... def __sub__(self, other): return Zbrx(self.value - other)
... def __add__(self, other): return Zbrx(self.value + other)
...... def normslc(self, slc):
... sss = [slc.start, slc.stop, slc.step]
... for i,s in enumerate(sss):
... if isinstance(s, Zbrx): sss = len(self.value)-1-s.value
... return tuple(sss), slice(*sss)
... def __init__(self, value):
... self.value = value
... def __getitem__(self, i):
... if isinstance(i, int):
... return '[%r]: %r'%(i, self.value)
... elif isinstance(i, Zbrx):
... return '[%r]: %r'%(i, self.value[len(self.value)-1-i.value])
... elif isinstance(i, slice):
... sss, slc = self.normslc(i)
... return '[%r:%r:%r]: %r'%(sss+ (list.__getitem__(self.value, slc),))
... def __setitem__(self, i, v):
... if isinstance(i, int):
... list.__setitem__(self, i, v)
... elif isinstance(i, slice):
... sss, slc = self.normslc(i)
... list.__setitem__(self.value, slc, v)
... def __repr__(self): return 'Zbrxlist(%r)'%self.value
..."[Zbrx(2)]: 'end'"zlast = Zbrx(0)
zbr10 = Zbrxlist(range(10))
zbr10[zlast] '[Zbrx(0)]: 9'
zbr10[zlast:] '[9:None:None]: [9]'
zbr10[zlast:zlast] = ['end']
zbr10 Zbrxlist([0, 1, 2, 3, 4, 5, 6, 7, 8, 'end', 9])
ztop = Zbrx(-1)
zbr10[ztop:ztop] = ['final']
zbr10 Zbrxlist([0, 1, 2, 3, 4, 5, 6, 7, 8, 'end', 9, 'final'])
zbr10[zlast:] "[11:None:None]: ['final']"
zbr10[zlast] "[Zbrx(0)]: 'final'"
zbr10[zlast+1] '[Zbrx(1)]: 9'
zbr10[zlast+2]
Zbrxlist(['a', 'b', 'c', 'd', 'e'])
Forgot to provide a __len__ method ;-)Zbrxlist(['a', 'b', 'c', 'd', 'e', 'end'])a[len(a.value):len(a.value)] = ['end']
a
lastx refers to the last items by zero-based reverse indexingZbrxlist(['a', 'b', 'c', 'd', 'e', 'last', 'end'])a[lastx] "[Zbrx(0)]: 'end'"
a[lastx:lastx] = ['last']
a
As expected, or do you want to define different semantics?
You still need to spell len(a) in the slice somehow to indicate
beond the top. E.g.,
Zbrxlist(['a', 'b', 'c', 'd', 'e', 'last', 'end', 'final'])a[lastx-1:lastx-1] = ['final']
a
Perhaps you can take the above toy and make something that works
they way you had in mind? Nothing like implementation to give
your ideas reality ;-)
Ron Adam said:(I was wondering why list's couldn't have len,min, and max attribute
that are updated when ever the list is modified in place of using
len,min, and max functions?
Would the overhead be that much?)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.