List of sets - Python node

Hello
We have an input table that each row is a list of sets

image
We want to put run this python code- but how we can read it as a list of sets?
import knime.scripting.io as knio

This example script simply outputs the node’s input table.

sets = knio.input_tables[0][‘sets’]

k = len(sets)

distances =
for s1, s2 in combinations(sets, 2):
intersection = s1.intersection(s2)
union = s1.union(s2)
jaccard_distance = 1 - len(intersection) / len(union)
distances.append(jaccard_distance)

diversity = sum(distances) / len(distances)

@malik maybe you can take a look at this example how to transfer Sets between KNIME and Python and also put them to a similar use like you might have tried to do (comparing the ‘distance’ between string item sets):


a

The results would look something like this:

Sets 3 and 5 are deliberately identical :slight_smile:

2 Likes