Operator :: in python to remove the group delay [duplicate] - python

This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
If a filter's group delay is N, the filtered signal using this filter is sig_filtered, what does sig_filtered[N::] mean in python?
I saw other usages of this python operator "::", e.g. A[::3] in another post (What is :: (double colon) in Python when subscripting sequences?), where A is a list. Can somebody give out a summary on how to use this python operator "::"?

sig_filtered[N::] is the same as sig_filtered[N:] and the same as sig_filtered[N::1], or sig_filtered[N:len(sig_filtered):1], or sig_filtered[N:len(sig_filtered)].
There are three values which define a slice: start, stop and step, e.g. data[start:stop:step]
You can omit start and will
default to 0.
You can omit stop and it will default to the full
length.
You can omit step and it will default to 1.
These behave the same way as the arguments to the range function

Related

Is the sequence of numbers generated by the range() function a tuple? [duplicate]

This question already has answers here:
What is the return value of the range() function in python?
(5 answers)
Closed 6 months ago.
On an online course, the teacher says that range() produces a tuple, adding that this is the reason why we have to convert it into a list (thanks to the list() function) if we want to modify it.
Is this statement true ?
Because in the official documentation, they dont talk about tuple at all in the range() section : https://docs.python.org/3/tutorial/controlflow.html#the-range-function
Starting with python3, range returns a range object which is a sequence type which, among other things, can be iterated to create a list out of it.
Relevant docs about the range type.
Before that, it used to return a list, like Klaus commented. (docs)

String backward another way [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 years ago.
I am very new to Python and I encountered one issue that I cannot understand. Let say I have string variable:
myVar = "abcdefgh"
I want to display it backward, no problem:
print(myVar[::-1])
and I get hgfedcba. Nothing surprising here. I should get the same with this somewhat verbose code:
print(myVar[len(myVar)-1:0:-1])
but this time the result is hgfedcb. Then I have tried not to subtract 1 from len(myVar) and the result was exactly the same. I do not understand why, especially that lines:
print(myVar[::1])
print(myVar[0:len(myVar):1])
display the same results.
So, my question is why print(myVar[len(myVar):0:-1]) does not display "a"?
The verbose equivalent of print(myVar[::-1]) would be:
print(myVar[-1:-1-len(myVar):-1])
# Or
# print(myVar[len(myVar)-1:-1-len(myVar):-1])
# but this makes the the length invariant less obvious
Note that the stop parameter is exclusive, and in order to get to the actual -1, you have to additionally subtract the full length as negative indexing starts at the end of the sequence. Note also how (stop-start)*step is still the length of the slice.

Processing of Booleans in Python [duplicate]

This question already has answers here:
Does Python support short-circuiting?
(3 answers)
Closed 5 years ago.
I have a question about a logical expression of the following sort:
for i in range (k): #k is large
if (a==b and test(c)==b): #test() takes some time to calculate
do something
Now I want to know, how the logical expression is processed. Are the two simple expressions calculated first and then combined via and? Or is a==b calculated, and in case it is False, test(c)==b neglected?
Thanks.
The a==b will be calculated first, and if it's true then the second expression will be evaluated. This is known as 'short-circuiting', see the docs.

Python Comprehensions troubleshooting [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 7 years ago.
I have problems to set up correctly my if statement.
This is my code:
def task_13():
Main_meal=['Meat','Cheese','Fish']
addons=['Potatoes','Rice','Salad']
my_meal=[(x+y) for x in Main_meal for y in addons if (x+y)!= 'FishRice' and 'CheeseRice']
print(my_meal)
My question is why Python filter out the 'CheeseRice' when is it stated there but only filter out the 'FishRice' option.
This is my output:
['MeatPotatoes', 'MeatRice', 'MeatSalad', 'CheesePotatoes', 'CheeseRice', 'CheeseSalad', 'FishPotatoes', 'FishSalad']
Thank you for your advice.
Here's the official reference on Python operator precedence, note that and is lower precedence than !=, so the != is evaluated first. Also and is a simple operator that takes the booleans on either side and returns a boolean representing their logical AND, it doesn't do what you tried to make it do.
Instead of
if (x+y)!= 'FishRice' and 'CheeseRice'
you need:
if (x+y)!= 'FishRice' and (x+y) != 'CheeseRice'
or alternatively
if (x+y) not in ('FishRice', 'CheeseRice')

slicing strings in python [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
reverse a string in Python
I tried searching the python documentation for a certain slicing exapmle
lets say I have this string
a = "ABCD"
when I write:
a[::-1]
I get the reversed string
"DCBA"
I can't understand how exectaly it works. None of the examples I saw were with two colons. what does it mean? how does it work?
thank you!
The full syntax of a slicing is
a[start:stop:step]
The step value denotes by how much to increase the index when going from one element of the slice to the next one. A value of -1 consequently means "go backwards". If start and stop are omitted as in your example, the default is to use the whole string (or more generally, the whole sequence, as this also works for lists and tuples).

Categories

Resources