Scala: filter Seq[] (make a diff) - scala

I have two Seq[_]es in my Play application.
Now I want to make a diff of those and get as a result an Seq with all items which are not in the other one.
I tried to use .filter() but I don't know if thats a good way
How can I achieve this?
thanks in advance
Update ... PseudoCode Example
I have two Seq[]
1.) Seq[CarsInStock]
Attributes[ID, Brand, Color]
[{1,Porsche,Red},{3,Mercedes,Blue}]
2.) Seq[CarsAfterSale]
Attributes[ID, Brand, Color,Doors,Windows]
[{1,Porsche,Red,4,10}]
Now I wan't to make a diff between the two seq[]. As result I want to get the Object {3,Mercedes,Blue}] because it is in stock, but after sales I have to know which ones I have to remove from stock.
I want to recognize the difference by the ID of the elements

You can simply filter out all cars whose id exist in the other Seq.
stock.filterNot(c => afterSale.exists(_.id == c.id))

Unless you expect the second Seq to be short, you can probably optimize it by creating a Set of ids:
val afterSaleIds = afterSale.iterator.map(_.id).toSet
stock.filterNot(c => afterSaleIds.contains(c.id))

Related

How to update JSON node that matches criteria based on attribute value (instead of index)?

Postgresql 10+
Example from the documentation...
jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false)
results in...
[{"f1":[2,3,4],"f2":null},2,null,3]
Fair enough. But I need to find my target node by attribute value, not index. For the life of me, I cannot figure out how do something like...
jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{(where f1 = 1),f1}','[2,3,4]', false)
Any advice on how to accomplish this?
Thanks!
You can split the steps into two jobs:
Split in elements (jsonb_arral_elements)
Indentify wich elements must change (case when...)
Update that element (jsonb_set)
Join all together (jsonb_agg)
solution
select jsonb_agg(case when element->>'f1'='1' then jsonb_set(element, '{f1}', '[2,3,4]') else element end)
from jsonb_array_elements('[{"f1":1,"f2":null},2,null,3,{"f1":3},{"f1":1,"f2":2}]'::jsonb) element
note
I changed the input adding two more elements with "f1" key

How to display distinct list of column values from OData?

I have an OData model with a property column 'category'. In twenty rows are i. e. 3 different categories. Now I want to display a list of all different categories to use as filter for a table. How can I do that?
Thanks
I started to answer this earlier today but then didnt complete it as it may not be a full answer but this certainly is a good place to start...
Two options I guess: get a function import that just returns a set of the categories and push the problem to the server.
Or process on the client side by using reduce on the column in question.
The best way to do that is explained here.
So adapting that answer:
var categories = ["SAPUI5","OpenUI5","JavaScript","NodeJS","SAP HANA","JavaScript","SAPUI5"];
var uniq = categories.reduce(function (a,b) {
if (a.indexOf(b) < 0 ) a.push(b);
return a;
}, []);
console.log(uniq); // ["SAPUI5", "OpenUI5", "JavaScript", "NodeJS", "SAP HANA"]

Dynamic query in EF

I have already searched in other questions for a solution, but didn't find it.
So, my problem is the following:
I have a page where the user can mount an expression. For example, if they want some professors with course 1 and course 2 then, they create an expression like this:
(course 1 AND course 2) in the page.
But when I use EF, if I put the "AND", I get no professor... if I change to "OR" I get some professors with 1 or 2 and maybe one of then have two courses.
I need the professors who have always the two courses (course 1 AND course 2)
How can I accomplish this?
(If my explanation get too confusing, let me know, I'll try in a other way!)
I tried to understand your explanation, Try something as following and let usknow if is the logic you are looking for? or you want something other result.
from x in db.professors.Where(x => x. professorId == professorId && (x.courseid == 'course1' && x.ukat == 'course2'))
Maybe:
var result = db.Professors.Where(p =>
p.Cursos.Count(c => searchedCourses.Contains(c.CourseId)) == searchedCourses.Count());
This way you get all the professors, filter their courses to match that in the specified search, and get only the professors with the same amount of filtered courses and the searched ones.

dataFrame keying using pandas groupby method

I new to pandas and trying to learn how to work with it. Im having a problem when trying to use an example I saw in one of wes videos and notebooks on my data. I have a csv file that looks like this:
filePath,vp,score
E:\Audio\7168965711_5601_4.wav,Cust_9709495726,-2
E:\Audio\7168965711_5601_4.wav,Cust_9708568031,-80
E:\Audio\7168965711_5601_4.wav,Cust_9702445777,-2
E:\Audio\7168965711_5601_4.wav,Cust_7023544759,-35
E:\Audio\7168965711_5601_4.wav,Cust_9702229339,-77
E:\Audio\7168965711_5601_4.wav,Cust_9513243289,25
E:\Audio\7168965711_5601_4.wav,Cust_2102513187,18
E:\Audio\7168965711_5601_4.wav,Cust_6625625104,-56
E:\Audio\7168965711_5601_4.wav,Cust_6073165338,-40
E:\Audio\7168965711_5601_4.wav,Cust_5105831247,-30
E:\Audio\7168965711_5601_4.wav,Cust_9513082770,-55
E:\Audio\7168965711_5601_4.wav,Cust_5753907026,-79
E:\Audio\7168965711_5601_4.wav,Cust_7403410322,11
E:\Audio\7168965711_5601_4.wav,Cust_4062144116,-70
I loading it to a data frame and the group it by "filePath" and "vp", the code is:
res = df.groupby(['filePath','vp']).size()
res.index
and the output is:
[E:\Audio\7168965711_5601_4.wav Cust_2102513187,
Cust_4062144116, Cust_5105831247,
Cust_5753907026, Cust_6073165338,
Cust_6625625104, Cust_7023544759,
Cust_7403410322, Cust_9513082770,
Cust_9513243289, Cust_9702229339,
Cust_9702445777, Cust_9708568031,
Cust_9709495726]
Now Im trying to approach the index like a dict, as i saw in examples, but when im doing
res['Cust_4062144116']
I get an error:
KeyError: 'Cust_4062144116'
I do succeed to get a result when im putting the filepath, but as i understand and saw in previouse examples i should be able to use the vp keys as well, isnt is so?
Sorry if its a trivial one, i just cant understand why it is working in one example but not in the other.
Rutger you are not correct. It is possible to "partial" index a multiIndex series. I simply did it the wrong way.
The index first level is the file name (e.g. E:\Audio\7168965711_5601_4.wav above) and the second level is vp. Meaning, for each file name i have multiple vps.
Now, this is correct:
res['E:\Audio\7168965711_5601_4.wav]
and will return:
Cust_2102513187 2
Cust_4062144116 8
....
but trying to index by the inner index (the Cust_ indexes) will fail.
You groupby two columns and therefore get a MultiIndex in return. This means you also have to slice using those to columns, not with a single index value.
Your .size() on the groupby object converts it into a Series. If you force it in a DataFrame you can use the .xs method to slice a single level:
res = pd.DataFrame(df.groupby(['filePath','vp']).size())
res.xs('Cust_4062144116', level=1)
That works. If you want to keep it as a series, boolean indexing can help, something like:
res[res.index.get_level_values(1) == 'Cust_4062144116']
The last option is a bit less readable, but sometimes also more flexibile, you could test for multiple values at once for example:
res[res.index.get_level_values(1).isin(['Cust_4062144116', 'Cust_6073165338'])]

Mongoengine filtering on a listField with __contains not working

I have a field in my document place_names which is a list of all possible place names for a location. Example New York City with have New York City, NYC, big apple etc.
I want the user to be able to query on any of these values or any part of the above values.
For example if they search for "apple" i want them to get New York City back. I was trying to use the __contains filter in mongoengine as below
place_names is of type ListField()
pn = request.POST.get('place_name', None)
try:
places_list = Places.objects()
if pn is not None and pn != "":
places_list.filter(place_names__contains = pn)
In the above example the filter doesn't work the way I expect it to. It works as a regular filter and doesn't do the "_contains". The same filter works fine if the type is StringField(). Is it possible to use "_contains" with ListFields? If not is there any way around this? thanks :)
__contains is a string lookup using a regex under the hood. To check if an item is in a listfield you should use the __in however, that does an exact match.
You could denormalise and create a ListField with the place names split into single words and lowercased, then you can use __in to determine if there is a match.