5

I need some help on sorting 2 lists..one with file listings and one with directory listings. These lists are generated through another part in a much larger script that I cannot put on here.

filelist = ['EN088_EFH_030_comp_v011.mov', 'EN086_EHA_010_comp_v031.mov', 'EN083_WDA_400_comp_v021.mov', 'EN086_EHA_020_comp_v010.mov', 'EN083_WDA_450_comp_v012.mov']

folderlist = ['[EN086_EHA_010_comp_v031]', '[EN083_WDA_400_comp_v021]', '[EN086_EHA_020_comp_v010]', '[EN083_WDA_450_comp_v012]']

using .sort I can get the data to output like this.

[CB083_WDA_400_comp_v021]
[CB083_WDA_450_comp_v012]
[CB086_EHA_010_comp_v031]
[CB086_EHA_020_comp_v010]
CB083_WDA_400_comp_v021.mov
CB083_WDA_450_comp_v012.mov
CB086_EHA_010_comp_v031.mov
CB086_EHA_020_comp_v010.mov
CB088_EFH_030_comp_v011.mov

But I need it to output like this

[CB083_WDA_400_comp_v021]
CB083_WDA_400_comp_v021.mov
[CB083_WDA_450_comp_v012]
CB083_WDA_450_comp_v012.mov
[CB086_EHA_010_comp_v031]
CB086_EHA_010_comp_v031.mov
[CB086_EHA_020_comp_v010]
CB086_EHA_020_comp_v010.mov
CB088_EFH_030_comp_v011.mov

How can I go about sorting it but ignoring the [] during the sort?
Or what would I do to get the second output?
I'm kind of stumped on what I should do.
Any tips or suggestions?

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124

1 Answers1

16
....sort(key=lambda x: x.strip('[]'))
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Would that remove the [] from my final output? I need to keep the brackets in. – user1995322 Jan 20 '13 at 20:28
  • 4
    No; that just sorts them based on the version of the string that doesn't have the brackets there. – Danica Jan 20 '13 at 20:33
  • Thanks for this, I am somewhat new to python and still elarning the ins and outs of it. the strip method functions however because the data still have the extensions it still causes the data to sort wrong. I believe I can get it to ignore the extension some how during the sort using a similar method as above correct? – user1995322 Jan 20 '13 at 21:04
  • @user1995322 - What do the extensions for which sort doesn't work look like - examples? – siddharthlatest Jan 20 '13 at 21:16
  • using the x.strip it lists them out like this. CB083_WDA_400_comp_v021.mov CB083_WDA_450_comp_v012.mov CB086_EHA_010_comp_v031.mov CB086_EHA_020_comp_v010.mov CB088_EFH_030_comp_v011.mov [CB083_WDA_400_comp_v021] [CB083_WDA_450_comp_v012] [CB086_EHA_010_comp_v031] [CB086_EHA_020_comp_v010] Basically it lists the files first then the directories. Looking around I saw I could do sort(key = lambda x: x.rsplit('.', 1)[0]) I'm trying to figure out how to use both the .strip and the .rsplit, python only seems to allow just one of them added. – user1995322 Jan 20 '13 at 21:36
  • 1
    `lambda x: x.strip('[]').rsplit('.', 1)[0]` – Ignacio Vazquez-Abrams Jan 20 '13 at 21:38