使用lambda表达式按多个条件排序文件

时间:2016-02-20 20:45:58

标签: python lambda

Full_Movie_da-DK_11-31-17.tsv
Full_Movie_de-AT_11-31-17.tsv
Full_Movie_de-CH_11-31-17.tsv
Full_Movie_de-DE_11-31-17.tsv
Full_TvEpisode_da-DK_11-31-17.tsv
Full_TvEpisode_de-AT_11-31-17.tsv
Full_TvEpisode_de-CH_11-31-17.tsv
Full_TvEpisode_de-DE_11-31-17.tsv
Full_TvSeason_da-DK_11-31-17.tsv
Full_TvSeason_de-AT_11-31-17.tsv
Full_TvSeason_de-CH_11-31-17.tsv
Increment_Movie_da-DK_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv
Increment_Movie_de-AT_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv
Increment_Movie_de-CH_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv
Increment_Movie_de-DE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv
Increment_TvEpisode_da-DK_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv
Increment_TvEpisode_de-AT_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv
Increment_TvEpisode_de-CH_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv
Increment_TvEpisode_de-DE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv
Increment_TvSeason_da-DK_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv
Increment_TvSeason_de-AT_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv
Increment_TvSeason_de-CH_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv

我想按以下顺序对其进行排序:

  1. 完全增加
  2. TvEpisode之前的TvSeason之前的电影
  3. 我该怎么做?到目前为止,我有:

    >>> files = ['Full_Movie_da-DK_11-31-17.tsv', 'Full_Movie_de-AT_11-31-17.tsv', 'Full_Movie_de-CH_11-31-17.tsv', 'Full_Movie_de-DE_11-31-17.tsv', 'Full_Movie_en-AU_11-31-17.tsv', 'Full_Movie_en-CA_11-31-17.tsv', 'Full_Movie_en-GB_11-31-17.tsv', 'Full_Movie_en-IE_11-31-17.tsv', 'Full_Movie_en-NZ_11-31-17.tsv', 'Full_Movie_en-US_11-31-17.tsv', 'Full_Movie_es-ES_11-31-17.tsv', 'Full_Movie_es-MX_11-31-17.tsv', 'Full_Movie_fi-FI_11-31-17.tsv', 'Full_Movie_fr-BE_11-31-17.tsv', 'Full_Movie_fr-CA_11-31-17.tsv', 'Full_Movie_fr-CA_11-31-17.tsv.bz2', 'Full_TvEpisode_da-DK_11-31-17.tsv', 'Full_TvEpisode_de-AT_11-31-17.tsv', 'Full_TvEpisode_de-CH_11-31-17.tsv', 'Full_TvEpisode_de-DE_11-31-17.tsv', 'Full_TvEpisode_en-AU_11-31-17.tsv', 'Full_TvEpisode_en-CA_11-31-17.tsv', 'Full_TvEpisode_en-GB_11-31-17.tsv', 'Full_TvEpisode_en-IE_11-31-17.tsv', 'Full_TvEpisode_en-NZ_11-31-17.tsv', 'Full_TvEpisode_en-US_11-31-17.tsv', 'Full_TvEpisode_es-ES_11-31-17.tsv', 'Full_TvEpisode_es-MX_11-31-17.tsv', 'Full_TvEpisode_fi-FI_11-31-17.tsv', 'Full_TvEpisode_fr-BE_11-31-17.tsv', 'Full_TvSeason_da-DK_11-31-17.tsv', 'Full_TvSeason_de-AT_11-31-17.tsv', 'Full_TvSeason_de-CH_11-31-17.tsv', 'Full_TvSeason_de-DE_11-31-17.tsv', 'Full_TvSeason_en-AU_11-31-17.tsv', 'Full_TvSeason_en-CA_11-31-17.tsv', 'Full_TvSeason_en-GB_11-31-17.tsv', 'Full_TvSeason_en-IE_11-31-17.tsv', 'Full_TvSeason_en-NZ_11-31-17.tsv', 'Full_TvSeason_en-US_11-31-17.tsv', 'Full_TvSeason_es-ES_11-31-17.tsv', 'Full_TvSeason_es-MX_11-31-17.tsv', 'Full_TvSeason_fi-FI_11-31-17.tsv', 'Full_TvSeason_fr-BE_11-31-17.tsv', 'Increment_Movie_da-DK_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_de-AT_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_de-CH_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_de-DE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_en-AU_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_en-CA_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_en-GB_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_en-IE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_en-NZ_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_en-US_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_es-ES_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_es-MX_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_fi-FI_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_Movie_fr-BE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_da-DK_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_de-AT_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_de-CH_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_de-DE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_en-AU_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_en-CA_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_en-GB_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_en-IE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_en-NZ_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_en-US_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_es-ES_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_es-MX_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_fi-FI_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvEpisode_fr-BE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_da-DK_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_de-AT_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_de-CH_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_de-DE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_en-AU_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_en-CA_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_en-GB_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_en-IE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_en-NZ_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_en-US_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_es-ES_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_es-MX_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_fi-FI_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv', 'Increment_TvSeason_fr-BE_2016-02-19_11-48-26_2016-02-20_11-31-17.tsv']
    
    >>> sorted(files, key= lambda x: ('Increment' in x, 'Movie' in x, 'TvSeason' in x, 'TvEpisode' in x))
    

    但这似乎按照我想要的相反顺序排序。这里的语法是什么?

1 个答案:

答案 0 :(得分:1)

因为lambda函数在True(1)之前对False(0)进行排序,所以您需要确保将参数计算为False以使其首先出现。这个,正确的表达方式是:

sorted(files, key=lambda x: (
                   ('Increment' not in x),
                   ('Movie' not in x),
                   ('TvSeason' not in x), 
                   ('TvEpisode' not in x)
               )
      )