我有两个列表
--with-iconv-include
如果名称(忽略扩展名)包含在list2中,我想创建一个包含list1名称的列表。对于上面的例子,正确的答案是
list1 = ['image1.png', 'image2.png', 'image3.png', 'image3.png']
list2 = ['image1.pdf', 'image2.eps', 'image3.ps']
任何想法如何做到这一点? 谢谢 卡尔
答案 0 :(得分:4)
from os.path import splitext
list1 = ['image1.png', 'image2.png', 'image3.png', 'image3.png', 'image4.png', 'image3.jpg']
list2 = ['image1.pdf', 'image2.eps', 'image3.ps', 'image5.doc']
# Create a lookup set of the document names sans extensions.
documents = set([splitext(filename)[0] for filename in list2])
# Compare each stripped filename in list1 to the list of stripped document filenames.
matches = [filename for filename in set(list1) if splitext(filename)[0] in documents]
print matches
输出:
['image1.png', 'image2.png', 'image3.png', 'image3.jpg']
请注意,如果需要,它必须适用于具有多个扩展名的文件,例如.tar.gz
(filename.partition(".")[0]
可以执行此操作)。但这意味着点不能放在文件名中的任何位置,因为第一个点现在界定了扩展名。
答案 1 :(得分:1)
您可以尝试使用set
获取唯一身份和列表理解来进行比较:
from os.path import splitext
list1 = ['image1.png', 'image2.png', 'image3.png', 'image3.png']
list2 = ['image1.pdf', 'image2.eps', 'image3.ps']
reference = set([splittext(item)[0] for item in list2]) # Strip the extension
outcome = set([item for item in list1 if splittext(item)[0] in reference]) # compare
print(outcome)
>>>
{'image3.png', 'image2.png', 'image1.png'}
答案 2 :(得分:1)
将列表理解与 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]]
<强>输出:强>
['image1.png', 'image2.png', 'image3.png']
答案 3 :(得分:0)
def filename(name):
return name.split('.')[0]
list2_filenames = [filename(name) for name in list2]
found_filenames = [name for name in list1 if filename(name) in list2_filenames]