How to do multiple where query without effect data in TypeORM? - postgresql

I want to do multiple where query without effect data. I want to get data that include at least 1 data per array. Pseudo code
data =[1,3]
array1 = [1,2]
array2 = [3,4]
if(data.IsIntersect(array1) and data.IsIntersect(array2))
IsIntersect checks are there a intersection beetween arrays
I did so far
queryBuilder.andWhere(
'properties.id IN (:...sizeIds) AND properties.id IN (:...colorIds)',
{ sizeIds: [1, 2], colorIds: [3, 4] },
);
It returns empty because firstly checks properties for 'sizeIds' then it checks for 'colorIds'. For example
properties includes 1,3
check for sizeIds, returns 1
check for colorIds, return empty
How can I do that with typeORM?

How can properties.id be 1 and 3? And if it is, how could 1 or 3 be in both? You're asking for the impossible.
I assume you mean to ask for when properties.id is 1 or 3, because if it is [1,3] then you should use the postgres array syntax {1,3} & the ANY keyword (some variation on this: Check if value exists in Postgres array).
tldr, I think all you need is brackets and OR instead of AND:
queryBuilder.andWhere(
'(properties.id IN (:...sizeIds) OR properties.id IN (:...colorIds))',
{ sizeIds: [1, 2], colorIds: [3, 4] },
);
If properties.id is in fact an array, then please add the entity definition to your question. If you want to merge the rows where properties.id is in the list you will need a GROUP BY (https://orkhan.gitbook.io/typeorm/docs/select-query-builder).

Related

MongoDB aggregation find document pairs using list of pairs that cannot go together

If I have the following data structure
{
"_id": <id_of_document>,
"name": <name_of_person>
}
I am trying to find a way of matching each person randomly with another person in the collection.
I have the following constraints a list of ids that can't go together, i.e.
[
[1, 2],
[10, 6],
]
the following shows that 1 and 2 cannot go together and 10 and 6 cannot go together but 1 and 10 could go together and 6 and 2 could go together.
How would I show this in a mongoDB aggregation?

Can I retrieve the second to last value of a stream?

I'm building a function that requires both the previous and the current value of a stream.
I managed to work that around, but I was wondering if that is some way to retrieve the second to last value of it.
You can use rxdart pairwise:
RangeStream(1, 4)
.pairwise()
.listen(print); // prints [1, 2], [2, 3], [3, 4]
You will always get a List containing the current emitted value and the last one as well. Just be aware this will only emit after there are 2 items to be emitted, so if you need the first value ASAP, this might not be the best solution for you.
A simple way to solve this is to just save the emitted value to an external variable, this usually isn't much recommended as Streams are supposed to be encapsulated from external code, but for many cases this would be simpler.
If you really need the first value you can duplicate your stream and consume the first value only once, then let pairwise() do it's magic, here's one solution using the async and rxdart packages:
Stream<int> stream = Stream.fromIterable([0, 1, 2, 3, 4, 5]);
List<Stream<int>> splitted = StreamSplitter.splitFrom(stream);
splitted[0].take(1).listen(print); // prints 0 immediately
splitted[1].pairwise().listen(print); // prints [0, 1], [1, 2], [2, 3], [3, 4], [4, 5]
Of course you can also merge them and get all of it in one stream.

Sort all dimensions of a cell array from min to max in MATLAB

I have created a cell array called Items of size (10,5). It contains the following as the first column in the cell array:
Item name Price (in $) Shipping (in $) Total price (in $) Total price (in €)
I have it all filled up, but what I need to do is to sort the cell array according to the total price in € from the smallest to the largest, but I can't seem to find a way to do it. I tried sort(Items,5); in order to sort according to the values in € but it returns an error. It could be useful to find a way to make the sorting automatic, so if I wanted to add more items it would still sort them in the global list.
sortrows will likely do exactly what you want to do. It will sort based on a specific column, assuming the datatype is constant in the entire column.
>> a ={'a',8,9;'b',5,6;'c',2,3};
>> a_sorted = sortrows(a,3)
a_sortred =
'c' [2] [3]
'b' [5] [6]
'a' [8] [9]
Edit
From your comments below, you can easily just sort the array first and then add a row to the cell array the same way you would combine regular arrays. Documentation
>> a = {7,8,9;4,5,6;1,2,3};
>> a_sorted = sortrows(a,3);
>> headers = {'col1','col2','col3'};
>> a_header = [headers;a_sorted]
a_header =
'col1' 'col2' 'col3'
[ 1] [ 2] [ 3]
[ 4] [ 5] [ 6]
[ 7] [ 8] [ 9]
EDIT #2
You can round the values that you are presenting using the second argument of the round function. After you round it, you can change the format of how things are displayed. Normally it is set as short, which is 4 decimal places. If you set it to shortg it will show as few decimals as possible, up to 4.
>> a = [1.23456789;2.3456789;3.456789]
a =
1.2346
2.3457
3.4568
>> a_rounded = round(a,2)
a_rounded =
1.2300
2.3500
3.4600
>> format shortg
>> a_rounded
a_rounded =
1.23
2.35
3.46
If changing the format is not an option, you could always just convert the number into a string and then display that. That gets a little more complicated, but a quick google will help you there.
EDIT #3
I did not know this existed before, but you can apparently use the format style called bank. This will display all numbers as two decimal points even if they are 0.
First place all of the prices in a separate array, sort on this array individually then use the indices of sorting to rearrange the rows of your cell array.
Try something like this:
price = [Items{:,5}];
[~,ind] = sort(price);
ItemsSorted = Items(ind,:);
Alternatively you can use the sortrows function that MZimmerman6 mentioned and operate along the fifth column of your cell array. I wasn't aware it worked on cell arrays, so I learned something new!

Is it possible to concatenate lists by including one list in another?

I have two lists that will be created during runtime. I want to combine the lists that have been made so that the data can be accessed later on within the code , with the end goal of simplifying my code and improving my model efficiency. Can lists be concatenated by the inclusion of one within the other or is there another way?
Thanks.
The usual way to concatenate lists is by using the sentence primitive. This will give you a new list made with the elements of your two original lists, like in Jen's answer.
Alternatively, you could use the list primitive to build a list with your two original lists included as sub-lists.
The following example shows both methods:
to setup
let list1 [ 1 2 3 ]
let list2 [ 4 5 6 ]
print sentence list1 list2 ; will print: [1 2 3 4 5 6]
print list list1 list2 ; will print: [[1 2 3] [4 5 6]]
end
Which one you should prefer depends, of course, on what you want to do with it...
The sentence command can combine two lists without brackets left
to setup
let mylist1 [1 2 3]
let mylist2 [4 5 6]
set mylist1 sentence mylist1 mylist2
show mylist1
end

Select One Element in Each Row of a Numpy Array by Column Indices [duplicate]

This question already has answers here:
NumPy selecting specific column index per row by using a list of indexes
(7 answers)
Closed 2 years ago.
Is there a better way to get the "output_array" from the "input_array" and "select_id" ?
Can we get rid of range( input_array.shape[0] ) ?
>>> input_array = numpy.array( [ [3,14], [12, 5], [75, 50] ] )
>>> select_id = [0, 1, 1]
>>> print input_array
[[ 3 14]
[12 5]
[75 50]]
>>> output_array = input_array[ range( input_array.shape[0] ), select_id ]
>>> print output_array
[ 3 5 50]
You can choose from given array using numpy.choose which constructs an array from an index array (in your case select_id) and a set of arrays (in your case input_array) to choose from. However you may first need to transpose input_array to match dimensions. The following shows a small example:
In [101]: input_array
Out[101]:
array([[ 3, 14],
[12, 5],
[75, 50]])
In [102]: input_array.shape
Out[102]: (3, 2)
In [103]: select_id
Out[103]: [0, 1, 1]
In [104]: output_array = np.choose(select_id, input_array.T)
In [105]: output_array
Out[105]: array([ 3, 5, 50])
(because I can't post this as a comment on the accepted answer)
Note that numpy.choose only works if you have 32 or fewer choices (in this case, the dimension of your array along which you're indexing must be of size 32 or smaller). Additionally, the documentation for numpy.choose says
To reduce the chance of misinterpretation, even though the following "abuse" is nominally supported, choices should neither be, nor be thought of as, a single array, i.e., the outermost sequence-like container should be either a list or a tuple.
The OP asks:
Is there a better way to get the output_array from the input_array and select_id?
I would say, the way you originally suggested seems the best out of those presented here. It is easy to understand, scales to large arrays, and is efficient.
Can we get rid of range(input_array.shape[0])?
Yes, as shown by other answers, but the accepted one doesn't work in general so well as what the OP already suggests doing.
I think enumerate is handy.
[input_array[enum, item] for enum, item in enumerate(select_id)]
How about:
[input_array[x,y] for x,y in zip(range(len(input_array[:,0])),select_id)]