Group items in list by x

Hi all,

I’m looking to split a list, group it by a particular criterion, getkeys and then repeat the keys, for use in a dictionary. I’m wondering if there is a better more flexible workflow that doesn’t use “string starts with”

I’ve developed a workflow to do the above. Any advice greatly appreciated. dyn file attached.
Thanks,


GroupKeysRepeatKeys.dyn (12.1 KB)

You’re overthinking it. You don’t actually need most of those nodes.

Thanks for the reply Nick.

I do think it could be simpler but not sure your suggestion achieves what I’m looking for.
Maybe I haven’t explained it clearly enough. I’ve just simulated 1 long list as that’s the level of data that I’d be using in the actual graph.

I want all those starting with 01 to have the same key, 02 same key, 03 same key. The important part is to repeat the keys as many times as they appear

e.g 01001, 01002, 01003 - all assigned the same key (1), and the key (1) repeats 3 times.

A bonus would be to make it more flexible to be able to group a list by a different search function other than “string starts with”.

Thanks.

I believe that’s what I’m showing, I just left off the last part because it’s the same. But you’re right, I did remove the String.StartsWith node for this example.

You also don’t seem to be using the actual list data except to sort by a function, then create keys for the number of elements in each newly sorted sublist. If that’s the case, the real question is “What’s the best way to sort my data?” which I think is your second point. The answer to that question really depends on your data and how you want to sort it.

The main point I’m trying to make is that you can sort your data however you like and once it’s sorted you just plug it into the GetKeys and Count nodes and you should be good from there.

I know I didn’t really answer your question but hopefully that helps for now. I might be able to give a little more input if you gave some more examples on what kind of data you’ll be trying to sort.

Yup totally understand your point regarding sorting. It’s part of the reason I asked if there was a better workflow. I guess for now this is doing what I want. Thanks for the help so far.

@Vikram_Subbaiah any ideas? Sorry to call you out I’ve just seen a few of your posts that seem to almost super simplify things.

Thanks.

Just a quick test:

data = ["0101","0102","0103","0201","0202"]
keys = {"01":"a","02":"b"}
out = {}
for i in data:
  if i[:2] in keys:
      out[i] = keys[i[:2]]

Then, print(out) will give you {‘0202’: ‘b’, ‘0201’: ‘b’, ‘0101’: ‘a’, ‘0103’: ‘a’, ‘0102’: ‘a’}

The rationale is to get the first two characters of a data item as key to search value in keys.

@Kotey.Nikoi Thanks for boosting my already inflated ego :slight_smile:

Not sure if I’ve understood your intent. But seems to me grouping isn’t required…

However, if you need to group, this might be a better approach…

2 Likes

Nice this looks like what I’d like to achieve, even though I’m just a beginner in python it’s pretty simple so thanks!

Haha well maybe the ego is justified as it looks like you’ve done it again :sunglasses:… I think I’ve seen a similar post of you suggesting use of the substring. I’ll work with this and see how flexible I can make it.

Thanks!

Sorry to interrupt this post @Kotey.Nikoi.

@Vikram_Subbaiah, I have come across this problem before. If the string is of three and four digit combination then String.Substring method doesn’t work. I solved it using Python but is there a way in to solve this simple thing using Designscript by including a simple If condition?

1 Like

@Raja Like this?

3 Likes

Yes, perfect simple and neat. Thanks @Vikram_Subbaiah !!

1 Like

Greatly simplified graph attached thanks again man!

GroupKeysRepeatKeys.dyn (8.8 KB)

2 Likes