Dhorka
Dhorka

Reputation: 696

Group a list of strings with the same suffix

I have a folder structure like this:

exp_name_seed_$INT/$STR_1
exp_name_seed_$INT/$STR_2
exp_name_seed_$INT/$STR_3
exp_name_seed_$INT/$STR_4
exp_name_seed_$INT/$STR_5

exp_name1_seed_$INT/$STR_1
exp_name1_seed_$INT/$STR_2
exp_name1_seed_$INT/$STR_3
exp_name1_seed_$INT/$STR_4
exp_name1_seed_$INT/$STR_5

I would like to group this in structured data(such as dictionary) like this:

-exp_name
-- exp_name_seed_$INT/$STR_1
-- exp_name_seed_$INT/$STR_2
-- exp_name_seed_$INT/$STR_3
-- exp_name_seed_$INT/$STR_4
-- exp_name_seed_$INT/$STR_5
-exp_name1
-- exp_name1_seed_$INT/$STR_1
-- exp_name1_seed_$INT/$STR_2
-- exp_name1_seed_$INT/$STR_3
-- exp_name1_seed_$INT/$STR_4
-- exp_name1_seed_$INT/$STR_5

Take into account that at this moment exp_name have variable size but the the ending has the same structure _seed_$INT/$STR_1

Is there any efficient way in python to achieve this functionality?

Upvotes: 1

Views: 710

Answers (2)

Sav
Sav

Reputation: 656

Of cause such things a pretty easy in python. Read about powerful groupby and setdefault

from itertools import groupby

ls=["exp_name_seed_$INT/$STR_1",
"exp_name_seed_$INT/$STR_2",
"exp_name_seed_$INT/$STR_3",
"exp_name_seed_$INT/$STR_4",
"exp_name_seed_$INT/$STR_5",
"exp_name1_seed_$INT/$STR_1",
"exp_name1_seed_$INT/$STR_2",
"exp_name1_seed_$INT/$STR_3",
"exp_name1_seed_$INT/$STR_4",
"exp_name1_seed_$INT/$STR_5"]

result = {}
for key, val in groupby(ls, lambda s: s.split('_seed_', 1)[0]):
    result.setdefault(key, []).extend(val)

print(result)

emits

{'exp_name': ['exp_name_seed_$INT/$STR_1',
  'exp_name_seed_$INT/$STR_2',
  'exp_name_seed_$INT/$STR_3',
  'exp_name_seed_$INT/$STR_4',
  'exp_name_seed_$INT/$STR_5'],
 'exp_name1': ['exp_name1_seed_$INT/$STR_1',
  'exp_name1_seed_$INT/$STR_2',
  'exp_name1_seed_$INT/$STR_3',
  'exp_name1_seed_$INT/$STR_4',
  'exp_name1_seed_$INT/$STR_5']}

Upvotes: 4

Karl
Karl

Reputation: 5822

Here is one way you could do this, i.e. create a dictionary for your data to get stored into. Loop through the input list, take the substrings you need and then create dictionary entries.

ls=["exp_name_seed_$INT/$STR_1",
"exp_name_seed_$INT/$STR_2",
"exp_name_seed_$INT/$STR_3",
"exp_name_seed_$INT/$STR_4",
"exp_name_seed_$INT/$STR_5",
"exp_name1_seed_$INT/$STR_1",
"exp_name1_seed_$INT/$STR_2",
"exp_name1_seed_$INT/$STR_3",
"exp_name1_seed_$INT/$STR_4",
"exp_name1_seed_$INT/$STR_5"]

postfix_len=len("seed_$INT/$STR_N") # assume length is fixed

result_dict={}
for item in ls:
    body_len=len(item)-postfix_len # this length will vary

    body=item[:body_len-1] # get for example "exp_name"
    postfix=item[body_len+5:len(item)] # get for example "$INT/$STR_3"

    if result_dict.get(body):
        result_dict[body].append(postfix) #if entry exists, add to list
    else:
        result_dict[body]=[postfix] # if entry doesn't exist yet, create list


print(result_dict)

Upvotes: 0

Related Questions