String from List Combining Duplicate Strings

Hi,
How do I combine the Duplicate items with a prefix as the number of items? For example 5 Walls, 2 Doors etc

hi Sujan,

Not the most elegant. But you could use ‘String.CountOccurrences’?

I used a code block because its neater, but i’m not that proficient with them yet so had to use nodes else where. But i included a node so you can see I was just replicating its function.

Best,

Ben

1 Like

Using List.CountOccurrences from Clockwork you can do this pretty easily (I don’t have the String From List node so I substituted it in this case):

OUT = [', '.join(sublist) for sublist in IN[0]]

If it doesn’t matter that the order of categories in the sublist can change, this python script works:

datalist = IN[0]
OUT = []
for data in datalist:
	d_dict = dict()
	for d in data:
		if d in d_dict:
			d_dict[d] += 1
		else: d_dict[d] = 1
	str_list = ["{} {}".format(v, k) for k, v in d_dict.items()]
	d_string = ", ".join(str_list)
	OUT.append(d_string)

Edit: If it does matter, then this version works:

def ConcatOccurance(_data):
	d_dict = dict()
	keys = []
	for d in _data:
		if d in d_dict:
			d_dict[d] += 1
		else: 
			d_dict[d] = 1
			keys.append(d)
	str_list = ["{} {}".format(d_dict[k], k) for k in keys]
	d_string = ", ".join(str_list)
	return d_string

OUT = [ConcatOccurance(data) for data in IN[0]]
1 Like