Python analogue of Matlab's interval numbers like 1:2:5 [duplicate] - python

This question already has answers here:
How to create a range of numbers with a given increment
(6 answers)
Closed 8 years ago.
Does anybody know whether Python can do the same thing as for i= 1:2:5 in Matlab? So i=1,3,5.
I know I can use other approaches to do this, but I want to know the equivalent form in Python.

try:
for i in xrange(1,6,2):
print i
This print:
1
3
5
Use xrange instead of range if you are using python 2.x because it is more efficient as it generates an iterable object, and not the whole list.

Use the range function:
for i in range(1, 6, 2):
print(i)

Related

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.

Python List as List Index [duplicate]

This question already has answers here:
Unpacking tuples/arrays/lists as indices for Numpy Arrays
(4 answers)
Closed 3 years ago.
Really not sure the right question to ask for this, but is it possible to have a list as the index of a list?
Ex:
pixelAddr=[50,50] # list
img[pixelAddr[0], pixelAddr[1]]=[255,255,255] # This is the way I know
# Is something like this possible? I get syntax errors when I try it...
img[*pixelAddr]=[255,255,255]
Btw, using python 3.7
when you do: img[pixelAddr[0], pixelAddr[1]] you are actually just re-packing the indices as a tuple so that is really all you need:
pixelAddr=(50,50) # NOTE THESE ARE ROUND PARENTHASIS
img[pixelAddr]=[255,255,255]
# or
pixelAddrList = [50,50]
img[tuple(pixelAddr)]=[255,255,255]

How to add elements in list at odd indices? [duplicate]

This question already has answers here:
creating sum of odd indexes python
(3 answers)
Closed 6 years ago.
Say I call this function getSumOdds([1,2,3,4,5]). This should return 6 since 2 + 4 are located at odd indices. I have figured out how to get it to return what numbers are located at odd indices, but I am unsure how to add them.
getSumOdds(aList):
return aList[1::2]
All this does is return what numbers are at the odd locations. I also want it just to return aList. I have tried using sum() in various ways but nothing seems to work. Anything would help!!
getSumOdds(aList):
return sum(aList[1::2])
Are you looking for sum(aList[1::2]) ?

C command similar to "if x in list" of Python [duplicate]

This question already has answers here:
Check if a value exists in an array in Cython
(2 answers)
Closed 6 years ago.
In Python we could use if x in list: so I was wondering if there's a similar command in C, so that we don't have to go through the whole thing using a for.
How can you know whether a value is contained in an array without cycling through it? This is exactly what Python does under the hood. No, there's no magical way to instantly know this.

Why does Python's slice notation go from [m,n-1]? [duplicate]

This question already has answers here:
Why is this list slice treating 0 and 1 as the same in Python? [duplicate]
(2 answers)
Understanding slicing
(38 answers)
Closed 8 years ago.
Let me preface this by saying I'm new to Python, come from Ruby, and I don't have much specific knowledge about how Python works.
For one of my current projects, I'm creating a new feature in a computational chemistry Django application that reads in PDBs and then does calculations on them. After adding my code, I was getting an error that Python can't typecast a string as a float, and looked at the library that parses the PDBs.
I was quickly confused by how Python's slice notation works. For example:
str = 'Hello this is Josh'
str[0:2] #=> 'He'
str[2] #=> 'l'
What I thought calling str[0:2] would result it would be Hel, not He, since index 0 to 2 is 3 big.
Is there a reason that this happens this way, and why str[m:n] gives from m to n-1, not from m to n?
It's so that:
str[0:2] + str[2:4] == str[0:4]
And
str[0:len(str)] == str
In general, it's conventional for sets of numbers to be defined this way; inclusive of the first listed number, exclusive of the second.
Esdgar Dijkstra wrote up a fairly well known argument for both this convention, and the convention of starting array indices at 0.

Categories

Resources