Re: on slices, negative indices, which are the equivalent procedures?

2021-08-06 Thread Boris Dorestand
Jach Feng  writes:

>> > s = "Jack Brandom" 
>> > s[3 : -13 : -1] 
>> >> 'kcaJ' 
>> >> I have no idea how to replace that -13 with a positive index. Is it 
>> >> possible at all? 
> That's not possible because a positive index is relative to the leftmost item > 0

And the middle index is always exclusive, so we can't go to the left of
0 and remain positive.  Okay, I think that answers it.  It's not
possible at all.

> Below is some rules of slice usage which I collected sometimes ago.
> -
> slice s[i:j:k]
>  The result is from start index i to stop index j (excluded) in step k 
> (default is 1).  i, j, k can be positive or negative. For example:  s = [1,  
> 2,  3,  4,  5]
> 1. Positive index is relative to the leftmost item (0), negative index is 
> relative to the rightmost item (-1). Note: out-of-range index is valid.
>s[-4:5] == s[1:5] == [2,3,4,5]  # index 5 is out-of-range
>s[4:-6:-1] == [5,4,3,2,1]  # index -6 is out-of-range
> 2. The default index of i and j (When index is omitted) was decided by the 
> sign of k. 
> For positive k, the start index is 0 (the leftmost item) and the stop 
> index is the one after the rightmost item.
>s[:3] == s[0:3] == [1,2,3]
>s[1:] == s[1:5] == [2,3,4,5]
>For negative k, the start index is -1 (the rightmost item) and the stop 
> index is one ahead of the leftmost item 
>s[:2:-1] == s[-1:2:-1] == [5,4]
>s[3::-1] == s[3:-6:-1] == [4,3,2,1]
> 3. The items pick-up order was decided by the sign of k. For positive k, the 
> direction is toward the right. For negative k, it is toward the left.
> s[1:4] == [2,3,4]
> s[3:0:-1] == [4,3,2]
> 4. Invalid slice will return an empty []
> s[2:0] == []

Thank you.  This helped a bit more.  So now I propose the following
simulation.  Can anyone simplify this?  Thanks!

--8<---cut here---start->8---
def mySlice(it, beg, end, step = 1):
  """Simulates Python slices."""
  if step is None: 
step = 1
  assert step != 0, "step = 0 not allowed"
  ret = []
  cur = beg
  if step > 0:
return mySlicePositiveStep(it, beg, end, step)
  else:
return mySliceNegativeStep(it, beg, end, step)

def mySliceNegativeStep(it, beg, end, step = -1):
  """Assume STEP is always negative.  The difference between positive
and neative step is that it reverts the orientation of the loop, but the
loop always begins at BEG, never at END.
  """
  ret = []
  if beg == None:
beg = -1
  if end == None:
end = -len(it) - 1
  if beg >= 0: # how to translate positive indices to negative ones
beg = -len(it) + beg
  if end >= 0:
end = -len(it) + end
  cur = beg # begin here
  while cur > end: # stop as soon as you arrive at END
ret.append(it[cur])
cur += step
  return ret

def mySlicePositiveStep(it, beg, end, step = 1):
  """Assume STEP is always positive.  But if BEG or END are
negative, we apply the equivalence rule BEG == LEN(IT) + BEG.  
For example, if BEG is -1 and LEN(IT) == 10, then BEG is
equivalent to 9 == 10 + -1.  (Similarly for END.)
  """
  ret = []
  if beg == None:
beg = 0
  if end == None:
end = len(it)
  if beg < 0:
beg = len(it) + beg
  if end < 0:
end = len(it) + end
  cur = beg
  while cur < end:
ret.append(it[cur])
cur += step
  return ret
--8<---cut here---end--->8---
-- 
https://mail.python.org/mailman/listinfo/python-list


how to let argument be optional falling back to certain integer

2020-06-20 Thread Boris Dorestand
I just wrote

def f(y, N, k = None):
  k = k or (N - 1)
  return k

I was surprised to find out that 0 == False, so f(7, 31, 0) produces 31.

I'd like 0 to be a valid choice for k.  

How do you guys let k be an optional argument such that it defaults to 
N - 1?  

Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to let argument be optional falling back to certain integer

2020-06-20 Thread Boris Dorestand
Chris Angelico  writes:

> On Sun, Jun 21, 2020 at 2:02 AM Boris Dorestand  
> wrote:
>>
>> I just wrote
>>
>> def f(y, N, k = None):
>>   k = k or (N - 1)
>>   return k
>>
>> I was surprised to find out that 0 == False, so f(7, 31, 0) produces 31.
>>
>> I'd like 0 to be a valid choice for k.
>>
>> How do you guys let k be an optional argument such that it defaults to
>> N - 1?
>>
>
> The easiest way is to explicitly check for None.
>
> if k is None: k = N - 1

Got it.  That's clear code.

> Zero being false shouldn't be a surprise. If None can count as false,
> then so should other "emptiness" values. (Remember, the canonical
> falseness value is False, not None.)

This is true.  I have written 0 as false in C so many times.  But
clearly for me times have changed...  I now look at numbers as a thing
in their own special class not to be confused as truth-values.  (So much
so that I fell for this.)  But I confess I still think of numbers as all
TRUE.  (Even zero!)

Anyway, I kind of replied just to thank you all for the great group this
is.  ChrisA, I don't know how can keep up with this newsgroup, but you
do.  This is crazy.  Years go by and when I come back, there you are
still.  You're priceless.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to let argument be optional falling back to certain integer

2020-06-21 Thread Boris Dorestand
Chris Angelico  writes:

[...]

>> Anyway, I kind of replied just to thank you all for the great group this
>> is.  ChrisA, I don't know how can keep up with this newsgroup, but you
>> do.  This is crazy.  Years go by and when I come back, there you are
>> still.  You're priceless.
>
> You're most welcome! I hang out here a lot because the people here are
> awesome. I've learned a lot, helped a lot of people, the language has
> shifted, it's been great.
>
> I like to think of myself as an empowerer - I help YOU to be able to
> do amazing things. It's you who are the awesome creative person who's
> making code do magic, and I'm just removing barriers from your way :)

I appreciate that a lot!  But of course if that's your view of things,
then you are the great one here.  No doubt about it.  Have a great day!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to let argument be optional falling back to certain integer

2020-06-22 Thread Boris Dorestand
David Raymond  writes:

>> This is true.  I have written 0 as false in C so many times.  But
>> clearly for me times have changed...  I now look at numbers as a thing
>> in their own special class not to be confused as truth-values.  (So much
>> so that I fell for this.)  But I confess I still think of numbers as all
>> TRUE.  (Even zero!)
>
> Also remember that in Python bool is a subclass of int:
 isinstance(False, int)
> True
 0 == False
> True
 1 == True
> True
 ["A", "B"][False]
> 'A'
 ["A", "B"][True]
> 'B'
>
> So if you're trying to do something slightly different based on the
> type of the input you might fall into this trap
>
> if isinstance(foo, float):
> do float stuff
> elif isinstance(foo, int):
> do int stuff
> elif isinstance(foo, bool):
> this line will never run because it would have triggered the int line

In my case I was only interested in ints, so I actually did try
isinstance(k, int) and it seemed to work because I didn't try 

  k = False 

I settled for the explicit check done by Chris Angelico.
-- 
https://mail.python.org/mailman/listinfo/python-list


on generating combinations among a variable list of lists

2020-06-28 Thread Boris Dorestand
Say we have [1,3,5,7], [2,3], [1,10].  I'd like to generate

  [1,2,1]
  [1,2,10]
  [1,3,1]
  [1,3,10]
  [3,2,1]
  [3,2,10]
  [3,3,1]
  [3,3,10]
  [5, ...]
  ... 
  [7,3,10]

The number of input lists is variable.  The example shows three lists,
but there could be only one or ten lists or any other number of lists.

I looked at itertools.  There doesn't seem to be any procedure ready for
this.  I might have to combine some of them but I'm not yet sure how.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: on generating combinations among a variable list of lists

2020-06-28 Thread Boris Dorestand
Peter Otten <__pete...@web.de> writes:

> Boris Dorestand wrote:
>
>> Say we have [1,3,5,7], [2,3], [1,10].  I'd like to generate
>> 
>>   [1,2,1]
>>   [1,2,10]
>>   [1,3,1]
>>   [1,3,10]
>>   [3,2,1]
>>   [3,2,10]
>>   [3,3,1]
>>   [3,3,10]
>>   [5, ...]
>>   ...
>>   [7,3,10]
>> 
>> The number of input lists is variable.  The example shows three lists,
>> but there could be only one or ten lists or any other number of lists.
>> 
>> I looked at itertools.  There doesn't seem to be any procedure ready for
>> this.  I might have to combine some of them but I'm not yet sure how.
>
> itertools.product() seems to do what you want:
>
>>>> for t  in itertools.product([1,3,5,7], [2,3], [1,10]):
> ...print(t)
> ... 
> (1, 2, 1)
> (1, 2, 10)
> (1, 3, 1)
> (1, 3, 10)
> (3, 2, 1)
> (3, 2, 10)
> (3, 3, 1)
> (3, 3, 10)
> (5, 2, 1)
> (5, 2, 10)
> (5, 3, 1)
> (5, 3, 10)
> (7, 2, 1)
> (7, 2, 10)
> (7, 3, 1)
> (7, 3, 10)

That's precisely it.  I missed product.  Thanks!

> If you need lists instead of tuples convert them
>
>>>> list(t)
> [7, 3, 10]
>
> To pass a varying number of lists use a list of lists and a star argument:
>
>>>> lists = [[[1, 2], [3, 4]], [[10, 20, 30], [40, 50]]]
>>>> for item in lists:
> ... print(list(itertools.product(*item)))
> ... 
> [(1, 3), (1, 4), (2, 3), (2, 4)]
> [(10, 40), (10, 50), (20, 40), (20, 50), (30, 40), (30, 50)]

The star-syntax I didn't even know.  And it was very useful.  Thank you!
-- 
https://mail.python.org/mailman/listinfo/python-list


how to plot the FFT of a list of values

2020-12-05 Thread Boris Dorestand
I have 16 values of the period sequence 1, 2, 4, 8, 1, 2, 4, 8, ...  I
compute its fourier transform using

>>> from scipy import fft, ifft
>>> x = [1,2,4,8,1,2,4,8]
>>> fft(x)
array([ 30. +0.j,   0. +0.j,  -6.+12.j,   0. +0.j, -10. +0.j,   0. +0.j,
-6.-12.j,   0. +0.j])

Now how can I plot these values?  I would like to plot 16 values.  What
do I need to do here?  Can you show an example?
-- 
https://mail.python.org/mailman/listinfo/python-list