Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Convert a List (from Collect) Into a String

Tags:

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?

like image 278
wikitect Avatar asked Oct 03 '16 13:10

wikitect


People also ask

Can I convert a list to a string?

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.

How do I convert a list of numbers to string?

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.

Can we convert list to string in Java?

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.

How do I convert a list to a string in one line?

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.


1 Answers

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.

like image 127
Tore Eschliman Avatar answered Oct 03 '22 04:10

Tore Eschliman