Repeat regex n time error in python3 - python-3.x

In my code I got answer [('hathatat', 'hat')] but I want [('hathatat')]. And please some one tell how to repeater any whole regex for n time.
import re
x='hathatatlatrat'
y = re.findall("((hat){2}at)",x)
print(y)

Try like this:
y = re.findall('((?:hat){2}at)', x)
hat will not be captured this way.

Related

There's a mistake But I don't know how to solve it

IndentationError: expected an indented block.....
import time
def countdown(n) :
while n > 0: #here Error
print (n)
n = n - 1
if n ==0:
print('BLAST OFF!')
countdown(50)
The best practice for programming is to learn how to google. Whenever you get an error, search for that on internet.
The below code snippet will work for you.
import time
def countdown(n) :
while n > 0: #here Error
print (n)
n = n - 1
if n ==0:
print('BLAST OFF!')
countdown(50)
Python don't have curly braces. So to mark everything you need to ident. Try above code it should work.
You need to have a look into the python docs:
In Python the indentation is not just cosmetics, but part of the syntax to define blocks and block scope. In your case, you obviously want to define a function named countdown. And this function is a block which must be indented. And that's what your error message states.
Your code should look like:
import time
def countdown(n) :
while n > 0: #here you need to indent
print (n)
n = n - 1
if n ==0: #this may or may not be within the while loop as it's entirely redundant
print('BLAST OFF!') #and the statements here is also a block
countdown(50)
Just have a look into the docs. E. g. here https://www.python-course.eu/python3_blocks.php
Good luck!

creating variable-names through loop and adding extension

I'm new to Stack Overflow. I searched for tips on my problem but unfortunately i could not find the solution i was looking for. I think it is not a very hard problem but i don't see the syntax error and it's driving me crazy. so the code:
MP = []
MP_ext = 2019
MP_num = 3
for x in range(0:MP_num):
MP[x] = MP.append('MP%s_%s' %MP_ext %x)
print(MP)
alternatively i tried this:
for x in range(0:MP_num):
MP[x] = MP.append('MP' + str(MP_ext) + '_' + str(x))
print(MP)
What i try to get is the vector MP like this:
[
MP2019_1,
MP2019_2,
MP2019_3
]
Thanks in advance for any advices
Welcome to Stack Overflow! I hope this helps:
MP = []
MP_ext = 2019
MP_num = 3
# range takes a single argument: an integer
# (it has optional arguments, see: https://www.w3schools.com/python/ref_func_range.asp)
# it will return a range of values like this: range(3) = [0,1,2]
for x in range(MP_num):
# string formatting using f-strings (Python 3.6+)
# list.append is in-place - list is mutable, don't need to reassign it
MP.append(f'MP{MP_ext}_{x+1}')
print(MP)

Numpy array into list

I have a numpy array that looks like below:
x = ['11BIT' '4FUNMEDIA' 'ABCDATA' 'ABPL' 'ACAUTOGAZ' 'ADIUVO']
The output should be like this:
x = ['11BIT', '4FUNMEDIA', 'ABCDATA', 'ABPL', 'ACAUTOGAZ', 'ADIUVO']
I tried to use x.tolist() but it didn't help. Basically I need a coma between values. Anyone could help ?
Thanks All
"A comma between values" as in a string?
",".join(x)
if you want a true list:
x = list(x)
or
x = [i for i in x]
should do the trick

How to give a Subclip a new name in Python using PyAutoGUI?

Complete beginner here making my first program with PyAutoGui as I cannot get access to the software's API. My issues are currently being that I am unable to come up with a solution to name each subclip with a different appended letter on the end. The naming convention should go like so, MI899001~AA, MI899001~AB, MI899001~AC, MI899001~AD. The only thing that changes is the last letter.
Below is the relevant code I'm currently using for the program I am writing;
def naming_promo():
x = string.ascii_uppercase
pyautogui.typewrite('DNH~P336007A' + x[0][0])
for i in range(7):
if i == 0:
sub_clip_clean()
else:
if i >= 1:
pyautogui.typewrite('567890qwe', 0.2)
sub_clip_package()
naming_promo() # See above Fn for method
pyautogui.moveTo(646, 404, duration=0.50)
pyautogui.click()
move_clips()
The naming_promo() takes the ascii_uppercase and types the first letter. I however can't figure out how to iterate through each character in the string as the For Loop repeats. I've googled many solutions, but I guess I can't get my head around how to do a loop in a loop and increment the x value used each time.
This is my first post so apologies for any etiquette I break. Any help and explanation would be greatly appreciated.
This is my first answer so apologies for any etiquette I break.
I'm not sure I understand everything here, since there's a few functions in the code that I don't know about. However, are you just looking for something like:
def naming_promo(n):
x = string.ascii_uppercase
pyautogui.typewrite('DNH~P336007A' + x[0][n])
and further down in your code, simply create a variable and increment it up one after use:
m = 0
for i in range(7):
if i == 0:
sub_clip_clean()
else:
if i >= 1:
pyautogui.typewrite('567890qwe', 0.2)
sub_clip_package()
naming_promo(m) # See above Fn for method
m += 1
pyautogui.moveTo(646, 404, duration=0.50)
pyautogui.click()
move_clips()

Evaluating and modifying theano tensors when stuff is on GPU

I am seriously stuck with something for ages now. I need some help.
I am running a theano conv network on GPU.
The network has a loss function as such
def mse(x, t):
return T.mean((x - t) ** 2)
Here x is the predicted value of a rectified liner unit and t is the expected value.
Now for a particular learning problem I am trying to modify the function such that I want to threshold the value of x. So essentially something simple as this
x[x>ts] = ts
But I am really struggling with this. I tried so many things
ts = 0.91
Y = T.vector()
#x_update = (x, T.set_subtensor(x[(x > ts).eval()], Y))
#f = function([Y], updates=[x_update])
#v=np.empty(len((x > ts).eval()))
#v.fill(ts)
#f(v)
#x.shape.eval()
x_arr = x.flatten()
print type(x_arr)
print type(t)
print type(x)
#print T.shape(x_arr).eval()
#print x.shape.eval()
#print x_arr.shape.eval()
#print t.shape.eval()
#print x.eval()
#print x_arr.get_value()
#x_newarr = x_arr.eval()
#x_newarr[x_newarr>ts] = ts
#x = T.shared(x_newarr)
return T.mean((x - t) ** 2)
Apart from the three prints, which all print <class 'theano.tensor.var.TensorVariable' > everything else gives me error.
So I am at my wits end how to do this simple stuff.
Is it because this stuff is on GPU ?
I did test the code on local python prompt, by constructing a numpy array and converting it into a tensor shared variable. The different stuff above works.
But I am conscious that the type is theano.tensor.sharedvar.TensorSharedVariable and not theano.tensor.var.TensorVariable.
I would really appreciate if some one gives me a helping hand here.
Regards
Please find the answer to this question given by pascal at
https://groups.google.com/forum/#!topic/theano-users/cNnvw2rUHc8
The failures are correct because the input values are not being provided at the time the function is being called, since it is symbolic.
The answer is to use T.minimum(x,threshold)

Resources