代码之家  ›  专栏  ›  技术社区  ›  Auto-learner

如何使用python将列表中的子字符串与另一个列表进行比较

  •  1
  • Auto-learner  · 技术社区  · 7 年前

    球门

    我试图获取文件夹中是否存在以dmaddleware开头的文件名,实际文件名类似于dmaddleware10.20.20.jar。我想检查列表(预期列表和实际列表)中是否存在子字符串dmaddleware

    问题

    有没有人能给我举个例子,或者如何做到这一点。提前谢谢。

    actual_file_names = ['', 'python', 'cmdb_dma_map.json', 'mappings.json', 'vendor_provided_binaries.json',
                         'vendor_provided_binaries_custom.json', 'DMAPremiumDatabase10.50.000.000.jar',
                         'DMAPremiumMiddleware10.50.000.000.jar', 'DMAPremiumUtilities10.50.000.000.jar',
                         'dma_oo_client_bin_linux.zip', 'dma_oo_client_bin_linux.zip.MD5', 'dma_oo_client_code_linux.zip',
                         'dma_oo_client_code_linux.zip.MD5', 'DCAFlowUtilities1.0.0.0.jar', 'DCAKafkaWriter1.0.0.0.jar',
                         'DCAUtilities1.0.0.0.jar']
    
    expected_file_names = ['python', 'cmdb_dma_map.json', 'mappings.json', 'vendor_provided_binaries.json',
                           'vendor_provided_binaries_custom.json', 'DMAPremiumDatabase.jar', 'DMAPremiumMiddleware.jar',
                           'DMAPremiumUtilities.jar']
    
    for f in expected_file_names:
        for g in actual_file_names:
            if f in g:
                print "All file names exists in " + g
    
            else:
                print "file name "+g+" doesn't exists"
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Ekaba Bisong    7 年前

    import re
    
    actual_file_names = ['', 'python', 'cmdb_dma_map.json', 'mappings.json', 
                         'vendor_provided_binaries.json',
                         'vendor_provided_binaries_custom.json', 
                         'DMAPremiumDatabase10.50.000.000.jar',
                         'DMAPremiumMiddleware10.50.000.000.jar', 
                         'DMAPremiumUtilities10.50.000.000.jar',
                         'dma_oo_client_bin_linux.zip', 
                         'dma_oo_client_bin_linux.zip.MD5', 
                         'dma_oo_client_code_linux.zip',
                         'dma_oo_client_code_linux.zip.MD5', 
                         'DCAFlowUtilities1.0.0.0.jar', 'DCAKafkaWriter1.0.0.0.jar',
                         'DCAUtilities1.0.0.0.jar']
    
    expected_file_names = ['python', 'cmdb_dma_map.json', 'mappings.json', 
                           'vendor_provided_binaries.json',
                           'vendor_provided_binaries_custom.json', 
                           'DMAPremiumDatabase.jar', 'DMAPremiumMiddleware.jar',
                           'DMAPremiumUtilities.jar']
    
    
    ##compare everything after first period:
    for expected in expected_file_names:
        part = expected.split('.',1)[0]
        ##print(part)
        matched = False
        for actual in actual_file_names:
            if part in actual:
                print('{} matches {}'.format(expected,actual))
                matched = True
    
        if not matched:
            print('{} could not be matched'.format(expected))
    
    print('-'*50)
    
    ##remove also numbers
    for expected in expected_file_names:
        part = re.sub('[0123456789]','',expected.split('.',1)[0])
        ##print(part)
        matched = False
        for actual in actual_file_names:
            if part in actual:
                print('{} matches {}'.format(expected,actual))
                matched = True
    
        if not matched:
            print('{} could not be matched'.format(expected))
    

    结果是:

    python matches python
    cmdb_dma_map.json matches cmdb_dma_map.json
    mappings.json matches mappings.json
    vendor_provided_binaries.json matches vendor_provided_binaries.json
    vendor_provided_binaries.json matches vendor_provided_binaries_custom.json
    vendor_provided_binaries_custom.json matches vendor_provided_binaries_custom.json
    DMAPremiumDatabase.jar matches DMAPremiumDatabase10.50.000.000.jar
    DMAPremiumMiddleware.jar matches DMAPremiumMiddleware10.50.000.000.jar
    DMAPremiumUtilities.jar matches DMAPremiumUtilities10.50.000.000.jar
    --------------------------------------------------
    python matches python
    cmdb_dma_map.json matches cmdb_dma_map.json
    mappings.json matches mappings.json
    vendor_provided_binaries.json matches vendor_provided_binaries.json
    vendor_provided_binaries.json matches vendor_provided_binaries_custom.json
    vendor_provided_binaries_custom.json matches vendor_provided_binaries_custom.json
    DMAPremiumDatabase.jar matches DMAPremiumDatabase10.50.000.000.jar
    DMAPremiumMiddleware.jar matches DMAPremiumMiddleware10.50.000.000.jar
    DMAPremiumUtilities.jar matches DMAPremiumUtilities10.50.000.000.jar
    

        2
  •  0
  •   Arockia    7 年前

    actual_file_names = ['', 'python', 'cmdb_dma_map.json', 'mappings.json', 'vendor_provided_binaries.json',
                         'vendor_provided_binaries_custom.json', 'DMAPremiumDatabase10.50.000.000.jar',
                         'DMAPremiumMiddleware10.50.000.000.jar', 'DMAPremiumUtilities10.50.000.000.jar',
                         'dma_oo_client_bin_linux.zip', 'dma_oo_client_bin_linux.zip.MD5', 'dma_oo_client_code_linux.zip',
                         'dma_oo_client_code_linux.zip.MD5', 'DCAFlowUtilities1.0.0.0.jar', 'DCAKafkaWriter1.0.0.0.jar',
                         'DCAUtilities1.0.0.0.jar']
    
    expected_file_names = ['python', 'cmdb_dma_map.json', 'mappings.json', 'vendor_provided_binaries.json',
                           'vendor_provided_binaries_custom.json', 'DMAPremiumDatabase.jar', 'DMAPremiumMiddleware.jar',
                           'DMAPremiumUtilities.jar']
    
    # 1'st way
    print [str(afn)+" is valid" if any(efn.split(".")[0] in afn for efn in expected_file_names) else str(afn)+"N/A" for afn in actual_file_names]
    
    # 2'nd way
    for efn in expected_file_names:
        for afn in actual_file_names:
            if efn.split(".")[0] in afn:
                print afn
    

    输出:

    ['N/A', 'python is valid', 'cmdb_dma_map.json is valid', 'mappings.json is valid', 'vendor_provided_binaries.json is valid', 'vendor_provided_binaries_custom.json is valid', 'DMAPremiumDatabase10.50.000.000.jar is valid', 'DMAPremiumMiddleware10.50.000.000.jar is valid', 'DMAPremiumUtilities10.50.000.000.jar is valid', 'dma_oo_client_bin_linux.zipN/A', 'dma_oo_client_bin_linux.zip.MD5N/A', 'dma_oo_client_code_linux.zipN/A', 'dma_oo_client_code_linux.zip.MD5N/A', 'DCAFlowUtilities1.0.0.0.jarN/A', 'DCAKafkaWriter1.0.0.0.jarN/A', 'DCAUtilities1.0.0.0.jarN/A']
    python
    cmdb_dma_map.json
    mappings.json
    vendor_provided_binaries.json
    vendor_provided_binaries_custom.json
    vendor_provided_binaries_custom.json
    DMAPremiumDatabase10.50.000.000.jar
    DMAPremiumMiddleware10.50.000.000.jar
    DMAPremiumUtilities10.50.000.000.jar