Created a list where in #{test} XTEAMK01, KKTEAM01, XTEAMO01, KKTEAM02, XJSTEAMD00, KKTEAM03 and then I want to get the following values that start with "KK"
KKTEAM01, KKTEAM02,KKTEAM03
Using #WindCheck you can format the code to work in robot. This code will produce the result you want.
***Variables***
#{test} XTEAMK01 KKTEAM01 XTEAMO01 KKTEAM02 XJSTEAMD00 KKTEAM03
*** Test Cases ***
Example
${x}= Evaluate [item for item in ${test} if item.startswith("KK")]
Log ${x}
alternatively you can use the other answers as well.
You're probably looking for startwith. Here it is used with a list comprehension.
In [1]: l = ["XTEAMK01", "KKTEAM01", "XTEAMO01", "KKTEAM02", "XJSTEAMD00", "KKTEAM03"]
In [2]: [x for x in l if x.startswith("KK")]
Out[2]: ['KKTEAM01', 'KKTEAM02', 'KKTEAM03']
You can use list compreenhension and startswith
kk_list = [item for item in full_list if item.startswith("KK")]
You could use a list comprehension:
inp = ["XTEAMK01", "KKTEAM01", "XTEAMO01", "KKTEAM02", "XJSTEAMD00", "KKTEAM03"]
output = [x for x in inp if x[:2] == 'KK']
print(output) # ['KKTEAM01', 'KKTEAM02', 'KKTEAM03']
Or, using regex for a more flexible option:
inp = ["XTEAMK01", "KKTEAM01", "XTEAMO01", "KKTEAM02", "XJSTEAMD00", "KKTEAM03"]
output = [x for x in inp if re.search(r'^KK', x)]
print(output) # ['KKTEAM01', 'KKTEAM02', 'KKTEAM03']
You can use robot's inline python evaluation to do a list comprehension:
log matches: ${{[x for x in $test if x.startswith('KK')]}}
Or, you can use Evaluate to do the same list comprehension in a slightly more readable format:
#{matches}= Evaluate [x for x in $test if x.startswith('KK')]
Related
I have two numpy arrays:
left = np.array([2, 7])
right = np.array([4, 7])
right_p1 = right + 1
What I want to do is
rand = np.zeros(left.shape[0])
for i in range(left.shape[0]):
rand[i] = np.random.randint(left[i], right_p1[i])
Is there a way I could do this without using a for loop?
You could try with:
extremes = zip(left, right_p1)
rand = map(lambda x: np.random.randint(x[0], x[1]), extremes)
This way you will end up with a map object. If you need to save memory, you can keep it that way, otherwise you can get the full np.array passing through a list conversion, like this:
rand = np.array(list(map(lambda x: np.random.randint(x[0], x[1]), extremes)))
I am trying to filter evident measurement mistakes from my data using the 3-sigma rule. x is a numpy array of measurement points and y is an arrray of measured values. To remove wrong points from my data, I zip x.tolist() and y.tolist(), then filter by the second element of each tuple, then I need to convert my zip back into two lists. I tried to first covert my list of tuples into a list of lists, then convert it to numpy 2D array and then take two 1D-slices of it. It looks like the first slice is correct, but then it outputs the following:
x = np.array(list(map(list, list(filter(flt, list(zap))))))[:, 0]
IndexError: too many indices for array
I don't understand what am I doing wrong. Here's the code:
x = np.array(readCol(0, l))
y = np.array(readCol(1, l))
n = len(y)
stdev = np.std(y)
mean = np.mean(y)
print("Stdev is: " + str(stdev))
print("Mean is: " + str(mean))
def flt(n):
global mean
global stdev
global x
if abs(n[1] - mean) < 3*stdev:
return True
else:
print('flt function finds an error: ' + str(n[1]))
return False
def filtration(N):
print(Fore.RED + 'Filtration function launched')
global y
global x
global stdev
global mean
zap = zip(x.tolist(), y.tolist())
for i in range(N):
print(Fore.RED + ' Filtration step number ' + str(i) + Style.RESET_ALL)
y = np.array(list(map(list, list(filter(flt, list(zap))))))[:, 1]
print(Back.GREEN + 'This is y: \n' + Style.RESET_ALL)
print(y)
x = np.array(list(map(list, list(filter(flt, list(zap))))))[:, 0]
print(Back.GREEN + 'This is x: \n' + Style.RESET_ALL)
print(x)
print('filtration fuction main step')
stdev = np.std(y)
print('second step')
mean = np.mean(y)
print('third step')
Have you tried to test the problem line step by step?
x = np.array(list(map(list, list(filter(flt, list(zap))))))[:, 0]
for example:
temp = np.array(list(map(list, list(filter(flt, list(zap))))))
print(temp.shape, temp.dtype)
x = temp[:, 0]
Further break down might be needed, but since [:,0] is the only indexing operation in this line, I'd start there.
Without further study of the code and/or some examples, I'm not going to try to speculate what the nested lists are doing.
The error sounds like temp is not 2d, contrary to your expectations. That could be because temp is object dtype, and composed of lists the vary in length. That seems to be common problem when people make arrays from downloaded databases.
I have three numpy arrays
x =np.array([1,2,3,4,2,1,2,3,3,3])
y =np.array([10,20,30,40,20,10,20,30,39,39])
z =np.array([100,200,300,400,200,100,200,300,300,300])
I want to check if x[i]==x[j] and y[i]==y[j] and z[i]!=z[j]. If this is true I want to remove z[j].
In pseudo code:
label: check
for i in range(0,np.size(x)):
for j in range(0,np.size(x)):
If x[i] == x[j] and y[i]==y[j] and z[i]!=z[j] and i<j:
x = delete(x,j)
y = delete(y,j)
z = delete(z,j)
print "start again from above"
goto check
Since I use goto and I don't know any other way around this I want to ask if there is any quick and elegant way to do this (maybe based on numpy predefined functions)?
This should do it:
np.unique(np.array([x, y, z]), axis=1)
I haven't found any straight answers.
I need to find the words in text / string that is being repeated the most.
E.g.
String that has following values:
000587\local_users
000587\local_users
4444\et-4444
et\pmostowiak
et\pmostowiak
et\pmostowiak
Then the results needs to be et\pmostowiak
How should I accomplish this?
EDIT:
I'm using older version of jython so I can't use the collections library with Counter function
This prints all values that are found more than ones:
d = {}
for x in users:
d[x] = x in d
_result = [x for x in d if d[x]] # [1]
If I could reuse this further?
Once you have some iterable container of words, collections does exactly what you need.
>>> import collections
>>> words = ['000587\local_users', '000587\local_users', '4444\et-4444', 'et\pmostowiak', 'et\pmostowiak', 'et\pmostowiak']
>>> print collections.Counter(words).most_common(1)
[('et\\pmostowiak', 3)]
This begs the question of how to split a string.
This works:
>>> str = """000587\local_users
... 000587\local_users
... 4444\et-4444
... et\pmostowiak
... et\pmostowiak
... et\pmostowiak"""
>>> str.split('\n')
['000587\\local_users', '000587\\local_users', '4444\\et-4444', 'et\\pmostowiak', 'et\\pmostowiak', 'et\\pmostowiak']
>>> words = str.split('\n')
I want to solve an optimization problem with the method 'COBYLA' in scipy.optimize.minimize as follows:
test = spopt.minimize(testobj, x_init, method='COBYLA', constraints=cons1)
y = test.x
print 'solution x =', y
However, since the program is quite large, a scalable way to write the objective function (and the constraints) is to use a general index for the arguments. For example, if I could use x['parameter1'] or x.param1 instead of x[0], then the program would be easier to read and debug. I tried both writing x as an object or a pandas Series with general indexing like x['parameter1'], as follows:
def testobj(x):
return x['a']**2 + x['b'] + 1
def testcon1(x):
return x['a']
def testcon2(x):
return x['b']
def testcon3(x):
return 1 - x['a'] - x['b']
x_init = pd.Series([0.1, 0.1])
x_init.index = ['a','b']
cons1 = ({'type': 'ineq', 'fun': testcon1}, \
{'type': 'ineq', 'fun': testcon2}, \
{'type': 'ineq', 'fun': testcon3})
but whenever I pass that into the minimize routine, it throws an error:
return x['a']**2 + x['b'] + 1
ValueError: field named a not found
It works perfectly if I use the normal numpy array. Perhaps I'm not doing it right, but is that a limitation of the minimize function that I have to use numpy array and not any other data structure? The scipy documentation on this topic mentions that the initial guess has to be ndarray, but I'm curious how is the routine calling the arguments, because for pandas Series calling the variable with x[0] or x['a'] are equivalent.
As you note, scipy optimize uses numpy arrays as input, not pandas Series. When you initialize with a pandas series, it effectively converts it to an array and so you cannot access the fields by name anymore.
Probably the easiest way to go is to just create a function which re-wraps the parameters each time you call them; for example:
def make_series(params):
return pd.Series(params, index=['a', 'b'])
def testobj(x):
x = make_series(x)
return x['a']**2 + x['b'] + 1
def testcon1(x):
x = make_series(x)
return x['a']
def testcon2(x):
x = make_series(x)
return x['b']
def testcon3(x):
x = make_series(x)
return 1 - x['a'] - x['b']
x_init = make_series([1, 1])
test = spopt.minimize(testobj, x_init, method='COBYLA', constraints=cons1)
print('solution x =', test.x)
# solution x = [ 1.38777878e-17 0.00000000e+00]