I have some code that I have been running in Python 3.6, but when I move to Python 3.9 I recieve the below error:
SyntaxError: can't use starred expression here
I understand some syntax related to expressions of the form (*something) was implemented in 3.9 that is not backwards compatible (see, for example, here).
Here is a minimal working example of what my code tries to do:
# Get some data
y = np.random.randn(100,100,100)
# Indexes stored as a tuple
x = (1,2)
# Result I'm after
result = y[...,(*x)]
In the above example, I am trying to essentially return y[:,1,2], but in practice, my tuple may have more values, and my array may be larger.
The above code works fine in Python 3.6 but doesn't work in Python 3.9. I cannot work out what the equivalent piece of code would be in Python 3.9 and above. I don't want to assume the number of dimensions in Y (e.g. I want to keep the ...), but I want to retain the behaviour I have above. How can I do this?
You were almost there:
result = y[(..., *x)]
Intermediate:
(..., *x)
# (Ellipsis, 1, 2)
Related
I am encountering an error in outputting a formatted string. I am using Python2.6
lat = 23.456
long = 34.129
# I want to output lat and long, both occupying 8 columns in width, right justified and with 2 values beyond the decimal point
site_file = open('test.site','w')
site_file.write('[{:8.2f}{:8.2f}]'.format(lat,long))
I get the following error:
ValueError: zero length field name in format
Any ideas on what I am doing wrong?
thanks!
Python 2.6 and earlier require that you specify the index of the formatted values. Here is an example:
'[{} {}]'.format(a, b) # Python 2.7 and later
'[{0} {1}]'.format(a, b) # Python 2.6 and earlier
I do not have a Python 2.6 interpreter handy, but I believe this is the syntax that you need:
site_file.write('[{0:8.2f}{1:8.2f}]'.format(lat,long))
I am running a function developed by Esri to get list of values in a integer column of a spatial table (however, the same behaviour is observed even when running the function on a non-spatial table). According to the help, I should get NumPy structured array. After running the function, I have a numpy array. I run print in this format:
in_table = r"C:\geodb101#server.sde\DataTable" #
data = arcpy.da.TableToNumPyArray(in_table, "Field3")
print data
Which gives me back this in IDE (copy/pasted from IDE interpreter):
[(20130825,) (20130827,) (20130102,)]
I am running:
allvalues = data.tolist()
and getting:
[(20130825,), (20130827,), (20130102,)]
Same result when running data.reshape(len(data)).tolist() as suggested in comments.
Running type() lets me know that in the first case it is <type 'numpy.ndarray'> and in the second case <type 'list'>. I am expecting to get my output list in another format [20130825, 20130827, 20130102]. What am I doing wrong or what else should I do to get the output list in the specified format?
I have a possible approach, but I'm not 100% sure it will work, as I can't figure out how you got tuples into an array (when I tried to create an array of tuples, it looks like the tuples got converted to arrays). In any case, give this a shot:
my_list = map(lambda x: x[0], my_np_array_with_tuples_in_it)
This assumes you're dealing specifically with the single element tuples you describe above. And like I said, when I tried to recreate your circumstances, numpy did some conversion moves that I don't fully understand (not really a numpy expert).
Hope that helps.
Update: Just saw the new edits. Not sure if my answer applies anymore.
Update 2: Glad that worked, here's a bit of elaboration.
Lambda is basically just an inline function, and is a construct common in a lot of languages. It's essentially a temporary, anonymous function. You could have just as easily done something like this:
def my_main_func():
def extract_tuple_value(tup):
return tup[0]
my_list = map(extract_tuple_value, my_np_array_with_tuples_in_it)
But as you can see, the lambda version is more concise. The "x" in my initial example is the equivalent of "tup" in the more verbose example.
Lambda expressions are generally limited to very simple operations, basically one line of logic, which is what is returned (there is no explicit return statement).
Update 3: After chatting with a buddy and doing some research, list comprehension is definitely the way to go (see Python List Comprehension Vs. Map).
From acushner's comment below, you can definitely go with this instead:
my_list = [tup[0] for tup in my_np_array_with_tuples_in_it]
I have a piece of code that broke the other day and I can't find the problem. I need to do something if I find a coincidence between a user input and the first value of any element of a list of lists.
I had this code running in another computer, but somehow I can't make it run anymore:
if any(orderinput == x[0] for x in order):
orderinput is the user input and order is the list of lists. This worked once and should be working based on what I've read here on stackoverflow, but it throws a syntax error at the r in for.
I tried moving it between lines or adding spaces, but the error follows the r.
I'm working in Python 2.2. I don't remember the version in the machine I made the code.
Generator expressions are available since python 2.4. Try changing to a list comprehension:
if any([orderinput == x[0] for x in order]):
Python 2.2 is twelve years old. A lot of things were different.
2nd Edit
This is how a "stokes_line" is generated:
os.system('pdv -t %s > temp.txt ' % epoch_name)
stokes_line = np.genfromtxt('temp.txt', usecols=3, dtype=[('stokesI','float')], skip_header=1)
os.system('vap -nc "mjd" %s >> mjd.txt' % new_y[i])
stokes_list[b] = stokes_line
"pdv" is a command from another programme which generated some numbers used in the "stokes_line".
stokes_list just gets a new stokes_line added too each in a loop each time.
EDIT:
A stokes_line is created with a numpy.genfromtxt command (and so extracts from a txt file) and is made up of floats and they look like this:
[(0.00126596,) (0.000621272,) (0.00058694,) ..., (0.0015368,)
(0.000926448,) (7.95851e-05,)]
A bunch of them together make up a stokes_list, and each stokes_line is made up of the same number of floats.
Is the problem because the genfromtxt is not creating a list which would be all square brackets, but something else (is it called a sequence)??
I wrote a python code which worked find on my laptop (python version 2.7.3), but it doesn't seem to run on my university desktop (python version 2.5.2).
I don't think that showing lots of the code here will be useful, but the error message is on the second of these two lines:
os.system('vap -nc "mjd" %s >> mjd.txt' % new_y[i])
stokes_list[b] = stokes_line
Where "vap" is a command from another programme which returns some values. Anyway, the crux of the matter is that I get the following error message ONLY when I run on the older version of Python:
ValueError: setting an array element with a sequence.
Now I did a little research, and people say that the problem is due to incompatible lengths of lists of some such - but if that was the case, why does it work fine when I run it on my newer version of Python.
Any help would be great, thank you!
.format, the Formatter class was added in 2.6.
os.system('vap -nc "mjd" %s >> mjd.txt' % new_y[i]) should work.
When you're using np.genfromtxt(…, usecols=3, dtype=[("stokesI","float")]), you're constructing a structured array with only one field ("stokesI"). Of course, when you print the array, this structure appears:
[(0.00126596,) (0.000621272,) (0.00058694,) ..., (0.0015368,) (0.000926448,) (7.95851e-05,)]
Each item is a np.void consisting of a named float field.
Looks a bit wasteful, you could probably simplify your expression as :
np.genfromtxt(…,usecols=3, dtype=float)
You then get:
[ 1.26596000e-03 6.21272000e-04 5.86940000e-04 ... 1.53680000e-03
9.26448000e-04 7.95851000e-05]
The probable reason why it apparently fails on an older version of Python is that you're using an older version of NumPy, like 1.4 or 1.5. There have been some improvements on the construction of ndarrays since (better handling of generators, automatic conversion from strings…)
So, I've got a quickhull implementation in python that I'm trying to use in python 3.2. Mostly it's fine, but there's a list indexing problem I'm having. The code does:
axis = sample[:,0]
This doesn't work, as python complains that list indices need to be integers, no tuples. I'm having trouble trying to understand what the line is trying to do. Anyone have any ideas?
Here's some surrounding code, if that helps:
if len(sample) > 2:
axis = sample[:,0]
base = numpy.take(sample, [numpy.argmin(axis), numpy.argmax(axis)], axis=0)
return link(dome(sample, base),
dome(sample, base[::-1]))
else:
return sample
(Additionally, I'm not sure what the base[::-1] means, but that at least works.)
axis is a Python list, but it should be a numpy array. The code is using numpy's special indexing rules for arrays to extract the first column.