Jexl - How to check one list contains other list - jexl

For me, the below logic, single value contains in an array is working perfectly.
value = "a"
list1 = ["a","b","c","d"]
value =~ list1
But if both are list, as given below, it is not working.
list1 = ["a","b","c","d"]
list2 = ["b","c"]
How will I check list2 =~ list1

This is a limitation in JEXL 3.2.1 fixed in JEXL 3.3 (snapshot).
You may overcome it by deriving JexlArithmetic and overriding the 'contains' method which is called when evaluating the 'in/matches' operator.
You may want to look at https://github.com/apache/commons-jexl/blob/master/src/main/java/org/apache/commons/jexl3/JexlArithmetic.java around line 1610 which will give you a potential solution.

Related

Why Groovy Pop() function is removing the first element from list?

I'm new to Groovy. I was trying Pop function, which should remove last element from the list acc to documentation. But it pops the first element in my case instead of last. Can any one tell me if that's a bug or am I missing something here?
This is the list i'm using:
def primes = [25,3,5,7,11,13];
Here is the output screenshot:
https://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/List.html#pop()
Removes the initial item from the List.
def list = ["a", false, 2]
assert list.pop() == 'a'
assert list == [false, 2]
This is similar to pop on a Stack where the first item in the list represents the top of the stack. Note: The behavior of this method changed in Groovy 2.5 to align with Java. If you need the old behavior use 'removeLast'.

Most practical way to add a value to a particular list, based on a variable

The title may sound a bit weird, but the situation is not so much.
I have some lists.
Here they are initialized (global variables):
sensor0, sensor1, sensor2, sensor3 = ([] for i in range(4))
Now we have a variable that indicates the list that i can insert data to. Let's say it's selector = 3.
This means i should append() my value to the sensor3 list.
What is the most practical way to do this in python?
If it was a C style language, i would use a switch-case.
But there is no switch-case syntax in python. Of course i could do multiple ifs, but this seems not the best way to do it.
I wonder, since there is only one letter to the lists that change everytime, perhaps there is a better way to select the proper list to append() to, based on the selector variable.
For your specific case, use eval
sensor0, sensor1, sensor2, sensor3 = ([] for i in range(4))
sensor3 = 20
selector =3
result = eval("sensor"+str(selector))
print(result)
But using a list may be a better option.
Using List
sensor = [[] for i in range(4)]
selector = 3
sensor[selector] = 20
print(sensor[selector])

What does this syntax mean in Python 3?

I've been seeing this syntax, but I'm not quite sure what it means. It's when two square brackets are next to the name of one list. I'm assuming this does some type of list slicing?
mylist[x][y]
mylist[][]
These are just some examples of what I've seen. (I've used variables x&y to represent an arbitrary number)
This notation can be used when the list contains some other lists as elements, which is helpful to represent the matrices. For example:
a=[[1,2,3],[4,5,6],[7,8,9]]
a[0][0] #This gives the number 1.
In this case, a[0] (the first index) chooses the 1st element, which is [1,2,3]. Then the second index (a[0][0]) chooses the first element of the list defined by a[0], thus giving the answer 1.
The top line just indexes into a list within a list. So for example, you could have
mylist = [
[1,2,3],
[4,5,6],
[7,8,9],
]
value = mylist[1][2]
which will get the value 6.
The bottom line doesn't look like valid Python to me.
EXPLANATION:
Consider that mylist[1] just extracts the second element from mylist (second because of 0-based indexing), which is [4,5,6]. Then adding [2] looks up the third item in that list, which is 6. You could also write
inner_list = mylist[1]
value = inner_list[2]
or
value = (mylist[1]) [2]
which both do the same thing.

Modify tuple element inside a list

I have a list which has tuple element. I need to modify the tuple element.
list1 = [1, (2, 'A'), 'B']
I need to modify 'A' to 'Z'
Thanks in Advance!
My solution is:
list1[1] = list(list1[1])
list1[1][1] = 'Z'
list1[1] = tuple(list1[1])
Is there any other feasible solution for this?
Generally speaking, a tuple is an immutable object - i.e. one that can't be changed. Instead, you're creating a new tuple using (part of) the data from the old tuple.
So, you can write your code in a way that reflects this:
list1[1] = (list1[1][0],'Z')
Tuples are immutable, so you can either convert the tuple to a list, replace the element in the list and convert it back to a tuple.
Or construct a new tuple by concatenation.

Simple adding two arrays using numpy in python?

This might be a simple question. However, I wanted to get some clarifications of how the following code works.
a = np.arange(8)
a
array([1,2,3,4,5,6,7])
Example Function = a[0:-1]+a[1:]/2.0
In the Example Function, I want to draw your attention to the plus sign between the array a[0:-1]+a[1:]. How does that work? What does that look like?
For instance, is the plus sign (addition) adding the first index of each array? (e.g 1+2) or add everything together? (e.g 1+2+2+3+3+4+4+5+5+6+6+7)
Then, I assume /2.0 is just dividing it by 2...
A numpy array uses vector algebra in that you can only add two arrays if they have the same dimensions as you are adding element by element
a = [1,2,3,4,5]
b = [1,1,1]
a+b # will throw an error
whilst
a = [1,2,3,4,5]
b = [1,1,1,1,1]
a+b # is ok
The division is also element by element.
Now to your question about the indexing
a = [1,2,3,4,5]
a[0:-1]= [1,2,3,4]
a[1:] = [2,3,4,5]
or more generally a[index_start: index_end] is inclusive at the start_index but exclusive at the end_index - unless you are given a a[start_index:]where it includes everything up to and including the last element.
My final tip is just to try and play around with the structures - there is no harm in trying different things, the computer will not explode with a wrong value here or there. Unless you trying to do so of course.
If arrays have identical shapes, they can be added:
new_array = first_array.__add__(second_array)
This simple operation adds each value from first_array to each value in second_array and puts result into new_array.

Resources