Using CYPHER I can get an ordered list of things using the collect() function. Is it possible to convert such a list into a simple string so that it behaves as a single string object?
If this isn't possible is it possible to somehow concatenate sequentially the contents of two (or more) collect statements so that in a single row I can produce output such as 'A,B,C a,b,c' where A,B,C is the ordered product of the 1st collect statement and a,b, c the second?
To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.
You can convert an integer list to a string list by using the expression list(map(str, a)) combining the list() with the map() function. The latter converts each integer element to a string.
substring() and String. replaceAll() method. The toString() method on a list returns a string that is surrounded by square brackets and has commas between items. The idea is to get rid of square brackets using the substring() method and comma and space replace using the replaceAll() method.
To convert a list to a string in one line, use either of the three methods: Use the ''. join(list) method to glue together all list elements to a single string. Use the list comprehension method [str(x) for x in lst] to convert all list elements to type string.
To flesh out Dave's comment: First, you'll want to combine your collections, then use REDUCE()
to append each item to a string. Like so:
WITH COLLECT(first_group) AS a, COLLECT(second_group) AS b
WITH a + b AS c
WITH REDUCE(s = HEAD(c), n IN TAIL(c) | s + ', ' + n) AS result
RETURN result
Check out the docs on REDUCE
to get a better idea how it works in Cypher.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With