neo4j cypher delete permutations in collection - collections

I've a query in Neo4j/Cypher which returns a collection like that:
MATCH z = (a:X)-[b:Y*2..]->(a)
RETURN relationships(z)
It returns (the paths) respectively the names of the edges:
[name 1, name 2, name 3, name 5],
[name 2, name 3, name 5, name 1],
[name 3, name 5, name 1, name 2],
[name 5, name 1, name 2, name 3],
[name 6, name 7],
[name 7, name 6],
[name 4, name 8],
[name 8, name 4]
In the result above the first 4 rows are equal, then the next two are equal and the last two rows are equal. I want to have no permutations like above i want something like that:
[name 1, name 2, name 3, name 5],
[name 6, name 7],
[name 4, name 8]
Does someone know how I would solve the problem?
Thanks in advance ;)

Cypher does not have a direct way to sort a collection. But there's a way to do it: use UNWIND followed by a WITH ... ORDER BY for sorting on a row level and use collect() to aggregate again.
Your case is even more complex since you want to partially order things on a pre-path base. First you need to build a kind of hashsum for each path (we're here concating relationship's ids) then do the unwind followed by order by and a collect based on the hash calculated previously.
I assume you want to use id of your relationship for ordering.
MATCH z = (a:X)-[b:Y*2..]->(a)
WITH relationships(z) as rels,
reduce(s="", x in relationships(z) | s+x+"_") as hash
UNWIND rels as r
WITH hash, id(r) as r_id order by hash, r_id
WITH hash, collect(r_id) as sorted_r_id
RETURN distinct sorted_r_id

Related

Shallow copy for one dimensional list

While reading shallow copy. It says that copy.copy(x) create shallow copy. But I don't see shallow copy behavior in case of the single dimensional list.
Example:
>> new = [1,2,3,4,5,6]
>> original = copy.copy(new)
>> new
[1, 2, 3, 4, 5, 6]
>> original
[1, 2, 3, 4, 5, 6]
>> id(new)
65022912
>> id(original)
65022512
>> new[2]=13
>> new
[1, 2, 13, 4, 5, 6]
>> original
[1, 2, 3, 4, 5, 6]
So here I assume updating "new" list should update "original" list but it is not happening.
In the case of the multidimensional list, the shallow copy is working properly.
Example:
>> parent_list = [1, 2, [3,4], [5,6]]
>> child_list = copy.copy(parent_list)
>> parent_list[2][1] = "Python"
>> parent_list
[1, 2, [3, 'Python'], [5, 6]]
>> child_list
[1, 2, [3, 'Python'], [5, 6]]
Please guide me, why the shallow copy is not working in case of a one-dimensional list.
Thanks.
There are actually 3 "Levels" to what you want.
1) Create a new reference to the same list. This aliasing is a trivial operation and would look like "original = new" or something like that. original[1] =x will update new[1]; This is the equivalent of copying a pointer in a pointer based language.
original = new
original[1] = x
new[1] will now be updated to x
This should be obvious but for completeness, If you follow the above with:
new = other
original is NOT affected at all.
2) Create a "Copy" of the list. It will allocate a new area and copy elements of the list. This is a "Shallow" copy. The children copied will be references, but the list itself will contain copies. original[1] =x will NOT update new[1], but original[1].childValue =x will update new[1].childValue
original = shallow copy of new
original[1].value = x
new[1].value WILL change to x
original[1] = y
new[1] will NOT be affected
3) Create a deep copy of the list. This will allocate a new area and shallow copy the list, but then will recurse and copy each child referenced in the list. No updates to original will modify new, or vice-a-versa.
original = deep copy of new
original[1].value = x
new[1].value will NOT be affected
original[1] = y
new[1] will NOT be affected
A shallow copy of a list is not usually what you want because your list is left in a hybrid state with some members referencing other lists and some not which will lead to unpredictable behavior, but it may be necessary if your tree is fairly deep and/or you never modify the child nodes.
original[1].value = x is giving error :AttributeError: 'int' object has no attribute 'values'
After writing the below code:
new = [1,2,3,4,5,6]
original = new.copy()
original[1] = 10
print("new:",new , "\n " ,"original:", original)
the output I am getting is :
new: [1, 2, 3, 4, 5, 6]
original: [1, 10, 3, 4, 5, 6]
changes done in the original are not getting reflected for a new list where an index is the same or a shallow copy

How to get sequence of items from list in Qore

Is there a Qore operator/function to get sublist from a list without modifying source list, i.e. equivalent of substr(). extract operator removes items from original list.
list l = (1,2,3,4,5,6,7,8,9);
list l2 = extract l, 2, 4;
printf("l:%y\nl2:%y\n", l, l2);
l:[1, 2, 7, 8, 9]
l2:[3, 4, 5, 6]
select operator supports in condition argument $# macro expanded as index.
list l = (1,2,3,4,5,6,7,8,9);
list l2 = select l, $# >= 2 && $# <2+4;
printf("l:%y\nl2:%y\n", l, l2);
l:[1, 2, 3, 4, 5, 6, 7, 8, 9]
l2:[3, 4, 5, 6]
The select operator is the best solution as you stated in your answer to your own question.
The splice and extract operators both will modify the list operand, which is not what you want.
Note that there is an outstanding feature issue for this in Qore (1781) - not yet targeted to a release, but it could go in the next major release (0.8.13) if there is any interest.

Interleaving Elements of a Prolog list

I am new to Prolog and came across this practice excercise. The question asks to define a predicate
zipper([[List1,List2]], Zippered). //this is two lists within one list.
This predicate should interleave elements of List1 with elements of List2.
For example,
zipper([[1,3,5,7], [2,4,6,8]], Zippered) -> Zippered = [1,2,3,4,5,6,7,8].
zipper([[1,3,5], [2,4,6,7,8]], Zippered) -> Zippered = [1,2,3,4,5,6,7,8].
So far I have a solution for two different list:
zipper ([],[],Z).
zipper([X],[],[X]).
zipper([],[Y],[Y]).
zipper([X|List1],[Y|List2],[X,Y|List]) :- zipper(List1,List2,List).
I am not sure how I can translate this solution for one list. Any suggestion on where I can start would be greatly helpful!
Firstly you should change zipper ([],[],Z). to zipper ([],[],[]).. Then to make it work for one list you could do what mat recommended in the comment or you could change it a little. So my version is:
zipper([],[],[]).
zipper([X,[]],X).
zipper([[],Y],Y).
zipper([[X|List1],[Y|List2]],[X,Y|List]) :- zipper([List1,List2],List).
And for your examples:
?- zipper([[1,3,5,7], [2,4,6,8]], Zippered).
Zippered = [1, 2, 3, 4, 5, 6, 7, 8] ;
Zippered = [1, 2, 3, 4, 5, 6, 7, 8] ;
false.
?- zipper([[1,3,5],[2,4,6,7,8]],Zippered).
Zippered = [1, 2, 3, 4, 5, 6, 7, 8] ;
false.

How to remove\replace big bracket while printing List or array in groovy?

I have to give list of values into in clause of SQL query but while retrieving the values [ ] also come along with data which is not readable by query language.
For example I have list as:
def val = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8))
while doing println(val) output come as [1, 2, 3, 4, 5, 6, 7, 8] but in the query it is needed as: 1, 2, 3, 4, 5, 6, 7, 8
In java this one works as System.out.println(val.toString().replaceAll("[\\[\\]]", "")) but not in groovy. Can't we use collection to remove like this?
Instead of:
def val = new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8))
use:
def val = new ArrayList(Arrays.asList(1,2,3,4,5,6,7,8)).join(', ')
or simply:
def val = [1,2,3,4,5,6,7,8].join(', ')
Try using g-strings and the minus operator:
println "${val}" - '[' - ']'

Indexing an array with a tuple

Suppose I have a tuple of (1, 2, 3) and want to index a multidimensional array with it such as:
index = (1, 2, 3)
table[index] = 42 # behaves like table[1][2][3]
index has an unknown number of dimensions, so I can't do:
table[index[0]][index[1]][index[2]]
I know I could do something like this:
functools.reduce(lambda x, y: x[y], index, table)
but it's utterly ugly (and maybe also inefficient), so I wonder if there's a better, more Pythonic choice.
EDIT: Maybe a simple loop is best choice:
elem = table
for i in index:
elem = elem[i]
EDIT2: Actually, there's a problem with both solutions: I can't assign a value to the indexed array :-(, back to ugly:
elem = table
for i in index[:-1]:
elem = elem[i]
elem[index[-1]] = 42
The question is very interesting and also your suggested solution looks good (havn't checked it, but this kind of problem requires a recursive treatment and you just did it in one line).
However, the pythonic way I use in my programs is to use dictionaries of tuples. The syntax is array-like, the performance - of a dictionary, and there was no problem in it for me.
For example:
a = {(1, 2, 3): 'A', (3, 4, 5): 'B', (5, 6, 7, 8): 'C'}
print a[1, 2, 3]
print a[5, 6, 7, 8]
Will output:
A
B
And assigning to an index is super easy:
a[1, 4, 5] = 42. (But you might want to first check that (1, 4, 5) is within the dict, or else it will be created by the assignment)

Resources