Meaning of # in python [duplicate] - python

This question already has answers here:
What does the "at" (#) symbol do in Python?
(14 answers)
Closed 5 years ago.
After quite some effort, I'm still unable to find any clue about the meaning of the # character in python syntax, such as in the (provided to me) function
def PI(pi0,P=P,T=T):
# Function PI computes the state probability vectors
# of the Markov chain until time T
pi_ = array([pi0])
for i in range(T):
pi_ = vstack((pi_,pi_[-1] # P))
return pi_
(where pylab has been previously imported). At parsing, this character rises a SyntaxError message.Any clue welcome !

The name of the operator being used is the matrix multiplication operator. Here is the description of the operator from the documentation:
Return a # b.
New in version 3.5.
As you can see, it was first added in Python 3.5. Thus, if your getting a SyntaxError, you're likely using Python version 3.4 or lower.

Related

What is the Python ` symbol [duplicate]

This question already has answers here:
What do backticks mean to the Python interpreter? Example: `num`
(3 answers)
Meaning of the backtick character in Python
(2 answers)
Closed 1 year ago.
Lots of old python code I look in has this ` symbol around a lot of stuff, what does it do? Now it is not considered valid syntax, obviously.
And I don't think it is just another string identifier, its sometimes wrapped around functions in the code I'm looking at.
Any help will be appreciated.

How could I translate the command find of MATLAB to Python? [duplicate]

This question already has answers here:
MATLAB-style find() function in Python
(9 answers)
Is there a NumPy function to return the first index of something in an array?
(20 answers)
Replacement of matlab find function in python
(1 answer)
Converting find() in matlab to python
(3 answers)
Closed 1 year ago.
I have this iteration in a program in Matlab and want to translate it to Python, but my problem is in the parameters for 'n' and 'direction'.
for i=1:size(labels)
idx_V=[idx_V;find(y(idxUnls(trial,:))==labels(i),l/length(labels),'first')]
end
There isn't a one-to-one swap for MATLAB's find function in Python. Taking inspiration from another answer here, I would propose the following solution:
% MATLAB
inds = find(myarray == condition, n, 'first')
# Python
import numpy as np
inds = [ind for (ind, val) in np.ndenumerate(myarray == condition) if val]
inds = inds[0:n]
I'm sure there is probably some trickery to think about in terms of which dimension find operates over first, compared to ndenumerate. The Python expression could also be constructed as a generator.
If you want a similar implementation, you'll have to write it yourself in Python.

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

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

Writing million digits of pi in an array. Output : "<map object at 0x0000000005748EB8>" [duplicate]

This question already has answers here:
Getting a map() to return a list in Python 3.x
(11 answers)
Closed 2 years ago.
I was trying to write a million digits of pi in an array with each digit as an individual element of the array. The values are loaded from a '.txt' file from my computer. I have seen a similar question here. Written by the help of this, my code is:
import numpy as np
data = np.loadtxt('pi.txt')
print(map(int, str(data)))
But the output is this:
<map object at 0x0000000005748EB8>
Process finished with exit code 0
What does it mean?
A few operations with Python version 3 become "lazy" and for example mapping now doesn't return a list of values but a generator that will compute the values when you iterate over it.
A simple solution is changing the code to
print(list(map(int, str(data))))
This is quite a big semantic change and of course the automatic 2->3 migration tool takes care of it... if you programmed in Python 2 for a while however is something that will keep biting you for quite some time.
I'm not sure about why this change was considered a good idea.
map in Python 3 will give you a generator, not a list, but in python 2 it will give a list.
The stack overflow link you have given refers to Python 2 where as you are writing code in python 3.
you can refer to these ideone links.
python 2 https://ideone.com/aAhvLD
python 3 https://ideone.com/MjA5nj
so if you want to print list you can do
print(list(map(int, str(data))))

Python " ".join() syntax error [duplicate]

This question already has answers here:
Why is parenthesis in print voluntary in Python 2.7?
(4 answers)
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 6 years ago.
I haven't done much with Python, but the code-academy course has seemed to give me some enjoyment however when I have taken my project to the actual python compiler I get an error compiling. Highly possible that code-academy just hasn't updated to a more recent version, as I've seen here that people have used this previously.
C:\Python>battleship.py
File "C:\Python\battleship.py", line 10
print " ".join(row)
^
SyntaxError: invalid syntax
For some reason I am having issues placing the source in the question, as this is my first question, so ill describe the code. Using an empty list there is a "board" created to play battleship on, with a for loop that loops through a set range appending < board.append["O"] * 5 >. Then it loops through each row in the array and then uses < print " ".join(row) >
How can I get by this and is there an alternative way that is better?
Edit: Python version 3.5.2

Categories

Resources