compare two lists of files, ignoring file extension in one list

后端 未结 4 1494
我在风中等你
我在风中等你 2021-01-20 21:59

I have two lists

list1 = [\'image1.png\', \'image2.png\', \'image3.png\', \'image3.png\']
list2 = [\'image1.pdf\', \'image2.eps\', \'image3.ps\']

4条回答
  •  感情败类
    2021-01-20 22:13

    Use a list comprehension with set:

    list1 = ["image1.png", "image2.png", "image3.png", "image3.png"]
    list2 = ["image1.pdf", "image2.eps", "image3.ps"]
    
    print [x for x in set(list1) for y in set(list2) if x.split('.')[0] == y.split('.')[0]]
    

    Output:

    ['image1.png', 'image2.png', 'image3.png']
    

提交回复
热议问题