Python Syntax error on colon in List - python

I'm trying to make a time series plot, and I have data points every second for about 50 seconds of time (which in my case is in UTC). Python is yelling at me about my array of data in the x axis of my plot, which is as follows:
%run "C:/Users/Jeff/Desktop/Python/STEPS_data.py"
File "C:\Users\Jeff\Desktop\Python\STEPS_data.py", line 3
x = [23:13:51,23:13:52,23:13:53,23:13:54,23:13:55,23:13:56,23:13:57,23:13:58,23:13:59,23:14:00,23:14:01,23:14:02,23:14:03,23:14:04,23:14:05,23:14:06,23:14:07,23:14:08,23:14:09,23:14:10,23:14:11,23:14:12,23:14:13,23:14:14,23:14:15,23:14:16,23:14:17,23:14:18,23:14:19,23:14:20,23:14:21,23:14:22,23:14:23,23:14:24,23:14:25,23:14:26,23:14:27,23:14:28,23:14:29,23:14:30,23:14:31,23:14:32,23:14:33,23:14:34,23:14:35,23:14:36]
^
SyntaxError: invalid syntax
There's a bunch of other info about the plot after this, but it gets hung up on this line, where it says that I have an invalid syntax error at the first colon in the array element 23:14:23, which doesn't really make sense to me. I tried making the array its own variable x1 and just saying x = x1, but that only pushed the syntax error point back by one character.
This seems like a really stupid problem but I'm stumped.

The problem is that : is not allowed everywhere, for example:
>>> a = 10:2
File "<ipython-input-12-63c21fb7e990>", line 1
a = 10:2
^
SyntaxError: invalid syntax
I think you wanted them as strings (in strings the : are allowed):
l = ['23:13:51', '23:13:52', '23:13:53', '23:13:54', '23:13:55', '23:13:56',
'23:13:57', '23:13:58', '23:13:59', '23:14:00', '23:14:01', '23:14:02', '23:14:03',
'23:14:04', '23:14:05', '23:14:06', '23:14:07', '23:14:08', '23:14:09', '23:14:10',
'23:14:11', '23:14:12', '23:14:13', '23:14:14', '23:14:15', '23:14:16', '23:14:17',
'23:14:18', '23:14:19', '23:14:20', '23:14:21', '23:14:22', '23:14:23', '23:14:24',
'23:14:25', '23:14:26', '23:14:27', '23:14:28', '23:14:29', '23:14:30', '23:14:31',
'23:14:32', '23:14:33', '23:14:34', '23:14:35', '23:14:36']
In case you don't want to add all these '' manually just wrap the whole thing as a string and split it:
>>> l = "[23:13:51,23:13:52,23:13:53,23:13:54,23:13:55,23:13:56,23:13:57,23:13:58,23:13:59,23:14:00,23:14:01,23:14:02,23:14:03,23:14:04,23:14:05,23:14:06,23:14:07,23:14:08,23:14:09,23:14:10,23:14:11,23:14:12,23:14:13,23:14:14,23:14:15,23:14:16,23:14:17,23:14:18,23:14:19,23:14:20,23:14:21,23:14:22,23:14:23,23:14:24,23:14:25,23:14:26,23:14:27,23:14:28,23:14:29,23:14:30,23:14:31,23:14:32,23:14:33,23:14:34,23:14:35,23:14:36]"
>>> l[1:-1].split(',')
or did you want them as datetimes?
>>> import datetime
>>> [datetime.datetime.strptime(t, '%H:%M:%S') for t in l[1:-1].split(',')]
or times?
>>> [datetime.datetime.strptime(t, '%H:%M:%S').time() for t in l[1:-1].split(',')]

Related

How can I use the {:<#} string alignment with a variable lenght (instead of #)?

I want to print some data on a left justification (alignment) but the spacing might need to change in real-time before printing so I would like to use a variable instead of a fixed hard-coded space width.
I tried using a variable to hold the number of spaces but that does work:
string_alignment_spacing = 15
print("|{:<string_alignment_spacing }|{:<string_alignment_spacing }|{:<string_alignment_spacing }|".format('Butter', 'Toast', 'Gravy'))
I was hoping to get the following output:
|Butter |Toast |Gravy |
But instead get:
ValueError: Invalid format specifier
Put string_alignment_spacing inside {} and in the format() method set keyword argument string_alignment_spacing=string_alignment_spacing:
string_alignment_spacing = 15
print("|{:<{string_alignment_spacing}}|{:<{string_alignment_spacing}}|{:<{string_alignment_spacing}}|".format('Butter', 'Toast', 'Gravy', string_alignment_spacing=string_alignment_spacing))
Prints:
|Butter |Toast |Gravy |
EDIT (With f-strings):
string_alignment_spacing = 15
print(f'|{"Butter":<{string_alignment_spacing}}|{"Toast":<{string_alignment_spacing}}|{"Gravy":<{string_alignment_spacing}}|')
In the following code,string_alignment_spacing is only str and there's an illegal space behind it.
print("|{:<string_alignment_spacing }|{:<string_alignment_spacing }|{:<string_alignment_spacing }|".format('Butter', 'Toast', 'Gravy'))
Try the following code
formatstr = "|{:<%s}|{:<%s}|{:<%s}|"%(15,15,15)
print(formatstr.format('Butter', 'Toast', 'Gravy'))

I'm trying to make a simple script that says two different two phrase lines(Python)

So, I'm just starting to program Python and I wanted to make a very simple script that will say something like "Gabe- Hello, my name is Gabe (Just an example of a sentence" + "Jerry- Hello Gabe, I'm Jerry" OR "Gabe- Goodbye, Jerry" + "Jerry- Goodbye, Gabe". Here's pretty much what I wrote.
answers1 = [
"James-Hello, my name is James!"
]
answers2 = [
"Jerry-Hello James, my name is Jerry!"
]
answers3 = [
"Gabe-Goodbye, Samuel."
]
answers4 = [
"Samuel-Goodbye, Gabe"
]
Jack1 = (answers1 + answers2)
Jack2 = (answers3 + answers4)
Jacks = ([Jack1,Jack2])
import random
for x in range(2):
a = random.randint(0,2)
print (random.sample([Jacks, a]))
I'm quite sure it's a very simple fix, but as I have just started Python (Like, literally 2-3 days ago) I don't quite know what the problem would be. Here's my error message
Traceback (most recent call last):
File "C:/Users/Owner/Documents/Test Python 3.py", line 19, in <module>
print (random.sample([Jacks, a]))
TypeError: sample() missing 1 required positional argument: 'k'
If anyone could help me with this, I would very much appreciate it! Other than that, I shall be searching on ways that may be relevant to fixing this.
The problem is that sample requires a parameter k that indicates how many random samples you want to take. However in this case it looks like you do not need sample, since you already have the random integer. Note that that integer should be in the range [0,1], because the list Jack has only two elements.
a = random.randint(0,1)
print (Jacks[a])
or the same behavior with sample, see here for an explanation.
print (random.sample(Jacks,1))
Hope this helps!
random.sample([Jacks, a])
This sample method should looks like
random.sample(Jacks, a)
However, I am concerted you also have no idea how lists are working. Can you explain why do you using lists of strings and then adding values in them? I am losing you here.
If you going to pick a pair or strings, use method described by Florian (requesting data by index value.)
k parameter tell random.sample function that how many sample you need, you should write:
print (random.sample([Jacks, a], 3))
which means you need 3 sample from your list. the output will be something like:
[1, jacks, 0]

perform derivative inside a loop

Hi I wrote the following code,
Z=1 #particle position
dz=0.1
time=1
dt=0.1
v=0
Fz=0
for time in np.arange(1, 10, dt):
#####simulation loop#######
theta=np.arccos(-Z/R) #contact angle
theta_e=((math.pi*110)/180) #equilibrium contact angle
Z_e=-R*np.cos(theta_e)#equilibrium position of particle
C=3.14*gamma*(R-Z_e) #additive constant
Fsz= (gamma*math.pi*(Z-Z_e)**2)+(tau*2*math.pi*math.sqrt(R**2-Z**2))+C
Fz=Fsz+(0.5*deltaF*np.sin((2*math.pi/lamda)*(Z-Z_e)-phi))#surface force
w_a=gamma*lamda_m**2*(1-np.cos(theta_e)) #work of adhesion
epsilon_z=2*math.pi*R*np.sin(theta)*mu*(nu/(lamda_m**3))*np.exp(w_a/KbT)#transitional drag
epsilon_s=khi*mu*((4*math.pi**2*R**2)/math.sqrt(Ad))*(1-(Z/R)**2)
epsilon=epsilon_z+epsilon_s
Ft=math.sqrt(2*KbT*epsilon)*series #thermal force
v=(-np.diff(Fz,Z)+Ft)/epsilon ##new velocity
Z=v*dt #new position
I was trying to calculate dFz/dzbut it is gave me following error,
File "C:/Users/mohammad.hossain1/Desktop/particle.py", line 62, in <module>
v=(-np.diff(Fz,Z)+Ft)/epsilon ##new velocity
File "C:\Users\mohammad.hossain1\AppData\Local\Continuum\anaconda3\lib\site-packages\numpy\lib\function_base.py", line 1924, in diff
slice1[axis] = slice(1, None)
IndexError: list assignment index out of range
As my initial condition is Fz=0 Z=0and it changes with time I suppose to get dFz/dz. I have imported all necessary module and all variables are defined properly at the beginning of the code.But I got error while I have introduced the derivative. So most likely my approach is not going with argument. Is it possible to show me the mistake that I have done during my coding.
It's hard too say for sure even with the stack trace. Obviously numpy is trying to access a list called slice1 with an axis index that the list does not have. Not sure why this is the case, but it must be something with this line:
v=(-np.diff(Fz,Z)+Ft)/epsilon
I suspect specifically the part of this line that is causing this issue is the np.diff(), since it is np code throwing this error. My best guess is that Fz and Z are equal in this case, or otherwise unacceptable values for the .diff method. Try adding the following if like so:
if Fz != Z:
v=(-np.diff(Fz,Z)+Ft)/epsilon
If that doesn't stop the crash try printing the values of Fz and Z right before this line and seeing if they look weird/suspicious.

I dont understand how to feed pygeocoder a variable instead of straight up number

I have a database of coordinates which i set to a list like so
point = []
#sqlcode to append the database to point[0] point [1] point [2] et cetera.
So in for example point[0] i have the output:
(45.424571, -75.695661)
Then i want to do some reverse geocoding with the pygeocoder module. It works when i add the number manually like so:
results = Geocoder.reverse_geocode(45.424571, -75.695661)
print(results[0])
This gives me a print out of a correct address.
However if i do this instead:
results = Geocoder.reverse_geocode(point[0])
print(results[0])
It does not work.
Im not sure how to troubleshoot this issue. essentially i want to make this work:
point = []
point.append("45.424571, -75.695661")
results = Geocoder.reverse_geocode(point[0])
print(results[0])
But i only get this error message in that case:
results = revgeo.reverse_geocode(point[0])
TypeError: reverse_geocode() takes at least 3 arguments (2 given)
Try this one:
point = []
point.append([45.424571, -75.695661])
results = Geocoder.reverse_geocode(*point[0])
The point should be a list, not string.

python pyfits, reading header information and using in calculation

I am running some code with python and pyfits and I am reading out a line of information from the header. I am getting the correct line but due to how it is written in the header it is printing out with colons separating the numbers I need.
the line I am running is
print header[0].header['opp']
this prints
34:04:32.04
I need to do a calculation where I add these numbers together, but do not know how to do this as they are separated by colons.
Something like this should solve your problem:
header[0].header['opp'] = "34:04:32.04"
print (sum(float(x) for x in header[0].header['opp'].split(":")))
... which outputs:
70.03999999999999
(EDIT)
Or, if the values actually make up a time in hours, minutes and seconds:
s = "34:04:32.04"
ss = [float(x) for x in s.split(":")]
print (ss[0] + ss[1]/60 + ss[2]/3600)
... which outputs the value in hours:
34.07556666666667

Categories

Resources