EhsanYaghoubi
EhsanYaghoubi

Reputation: 145

Group elements of a long list in python based on the element name

I have a long list like below which I have sorted based on the (element[0], element[3], element[2]), respectively. By element, I mean digits between underlines.

list3=['20180406_145813_4_1.jpg',
 '20180406_145813_5_1.jpg',
 '20180406_145813_6_1.jpg',
 '20180406_175827_10_12.jpg',
 '20180406_175827_11_12.jpg',
 '20180409_190651_7_2.jpg',
 '20180409_190651_8_2.jpg',
...]

Now, I want to split the list based on element[3].My desired result is:

[['20180406_145813_4_1.jpg',
 '20180406_145813_5_1.jpg',
 '20180406_145813_6_1.jpg'],
 ['20180406_175827_10_12.jpg',
 '20180406_175827_11_12.jpg'],
 ['20180409_190651_7_2.jpg',
 '20180409_190651_8_2.jpg'],
...]

I have this code that prints each name as a list. I don˙t know how to group by element3 in this code:

for imagename in list3:
    element3 = imagename.split("_")[3]
    for j,m in groupby(list3):
        print(list(m))

Upvotes: 1

Views: 70

Answers (2)

Aaron_ab
Aaron_ab

Reputation: 3758

Try this (No importing anything):

list3=['20180406_145813_4_1.jpg',
       '20180406_145813_5_1.jpg',
       '20180406_145813_6_1.jpg',
       '20180406_175827_10_12.jpg',
       '20180406_175827_11_12.jpg',
       '20180409_190651_7_2.jpg',
       '20180409_190651_8_2.jpg',
       ...]

res = []
for first, second, third in zip(*[iter(list3)]*3):
    res.append([first, second, third])

Just append a list of first, seconds, third to the res list

print(res)

[['20180406_145813_4_1.jpg', '20180406_145813_5_1.jpg', '20180406_145813_6_1.jpg'], 
 ['20180406_175827_10_12.jpg', ...]]

Upvotes: 1

Dani Mesejo
Dani Mesejo

Reputation: 61930

You can use itertools.groupby like this:

from itertools import groupby

list3 = ['20180406_145813_4_1.jpg',
         '20180406_145813_5_1.jpg',
         '20180406_145813_6_1.jpg',
         '20180406_175827_10_12.jpg',
         '20180406_175827_11_12.jpg',
         '20180409_190651_7_2.jpg',
         '20180409_190651_8_2.jpg']

result = [list(group) for _, group in groupby(list3, key=lambda x: x.split('_')[3])]
print(result)

Output

[['20180406_145813_4_1.jpg', '20180406_145813_5_1.jpg', '20180406_145813_6_1.jpg'], ['20180406_175827_10_12.jpg', '20180406_175827_11_12.jpg'], ['20180409_190651_7_2.jpg', '20180409_190651_8_2.jpg']]

The above list comprehension is equivalent to the following for loop:

result = []
for _, group in groupby(list3, key=lambda x: x.split('_')[3]):
    result.append(list(group))

Upvotes: 3

Related Questions