2017-05-06 57 views
-1

我在尝试验证列表中的每个字符串在另一个列表中至少存在一次。我一直在困扰的问题是字符串永远不会完全匹配,所以我需要某种形式的正则表达式/通配符。有没有一种方法来验证一个列表中的每个项目至少存在一次从另一个列表中存在一次?

must_have_list = ['APPLE SSD', 'APPLE HDD'] 
example_device_list = [u'APPLE SSD SM128E', u'APPLE HDD HTS541010A9E662'] 
example_device_list2 = [u'APPLE SSD SD0128F', u'APPLE HDD ST3000DM001'] 

的想法是返回True如果给定的设备列表包含must_have_list每个设备串的至少一个。如果给定的设备列表仅包含在must_have_list的项目之一(或无),然后返回False

[u'APPLE SSD SM128E', u'APPLE HDD HTS541010A9E662'] 

True各一个发现

[u'APPLE HDD HTS541010A9E662', u'APPLE HDD HTS541010A9E662'] 

False只有2个APPLE HDD发现,没有APPLE SSD上市

[u'APPLE HDD HTS541010A9E662', u'APPLE HDD HTS541010A9E662', u'APPLE SSD SM128E'] 

True其中每一个发现,甚至吨霍夫有不止一个APPLE HDD

[u'APPLE SSD SM128E'] 

False只有APPLE SSD上市,没有APPLE HDD上市

如何使用正则表达式来验证一个列表中每个项目在另一列表中存在?

+1

什么是你的问题? – mkrieger1

+0

你想使用正则表达式来查找字符串模式吗?为什么你的资料照片?你想看到世界在烈焰中燃烧吗? profaner;)(只是在开玩笑) –

+1

如果你只是使用[any和all?](https://docs.python.org/3/library/functions.html)而不是're.sub'会怎么样? ' 在_list [example_device_list,example_device_list2]: 所有([任何(我在must_have_list j适用于_list j),其中i]) ' – 2017-05-06 01:47:48

回答

1

如果,在你的榜样,模式进行测试始终是字符串的初始部分,它是有点简单:

for must_have in must_have_list: 
    notInList = True 
    for example in example_device_list: 
     if example.startswith(must_have): 
      notInList = False 
      break 
    if notInList: return False 
return True 

如果它可以是一个内部串,那么你” d必须使用must_have in example而不是startswith,这会增加算法的复杂性。

其他优化将删除一个示例设备,发现它不会对其他必备项进行测试。

最后,您可以将整个过程从里面翻出来,遍历每个示例设备上的示例列表,删除必须找到的示例的前缀,直到没有必须离开。根据必须列表和示例列表的大小,将必须复制到新词典(或从集合中设置)以提高搜索时间是有意义的。

1

不使用regex。这是使用str.startswith()你的问题的方法:

def check (a=list, b=list): 
    checked = [] 
    for k in a: 
     c = False 
     for j in b: 
      if j.startswith(k): 
       c = True 
       break 
     checked.append(c) 
    return all(checked) 

# inputs 
must_have_list = ['APPLE SSD', 'APPLE HDD'] 
example_device_list = ['APPLE SSD SM128E', 'APPLE HDD HTS541010A9E662'] 
example_device_list2 = ['APPLE SSD SD0128F', 'APPLE HDD ST3000DM001'] 
example_device_list3 = ['APPLE ASD SD0128F', 'APPLE HDD ST3000DM001'] 
example_device_list4 = ['APPLE SSD SD0128F', 'APPLE ADD ST3000DM001'] 
example_device_list5 = ['APPLE HDD HTS541010A9E662', 'APPLE HDD HTS541010A9E662', 'APPLE SSD SM128E'] 

# Some tests with this lists 
check_list = check(must_have_list, example_device_list) 
check_list2 = check(must_have_list, example_device_list2) 
check_list3 = check(must_have_list, example_device_list3) 
check_list4 = check(must_have_list, example_device_list4) 
check_list5 = check(must_have_list, example_device_list5) 

# Outputs 
print "All items of %s exists at least once in %s: %r" % ("must_have_list", "example_device_list", check_list) 
print "All items of %s exists at least once in %s: %r" % ("must_have_list", "example_device_list2", check_list2) 
print "All items of %s exists at least once in %s: %r" % ("must_have_list", "example_device_list3", check_list3) 
print "All items of %s exists at least once in %s: %r" % ("must_have_list", "example_device_list4", check_list4) 
print "All items of %s exists at least once in %s: %r" % ("must_have_list", "example_device_list5", check_list5) 

输出:

All items of must_have_list exists at least once in example_device_list: True 
All items of must_have_list exists at least once in example_device_list2: True 
All items of must_have_list exists at least once in example_device_list3: False 
All items of must_have_list exists at least once in example_device_list4: False 
All items of must_have_list exists at least once in example_device_list5: True 
0

您可以使用allany来测试你的条件:

must_have_list = ['APPLE SSD', 'APPLE HDD'] 
examples=[[u'APPLE SSD SM128E', u'APPLE HDD HTS541010A9E662'], 
      [u'APPLE SSD SD0128F', u'APPLE HDD ST3000DM001'], 
      [u'APPLE HDD HTS541010A9E662', u'APPLE HDD HTS541010A9E662', u'APPLE SSD SM128E'], 
      [u'APPLE SSD SM128E'], 
      [u'APPLE SSD SM128E',u'APPLE SSD SM128E']] 

for ex in examples: 
    print ex, all(any(r in s for s in ex) for r in must_have_list) 

打印:

[u'APPLE SSD SM128E', u'APPLE HDD HTS541010A9E662'] True 
[u'APPLE SSD SD0128F', u'APPLE HDD ST3000DM001'] True 
[u'APPLE HDD HTS541010A9E662', u'APPLE HDD HTS541010A9E662', u'APPLE SSD SM128E'] True 
[u'APPLE SSD SM128E'] False 
[u'APPLE SSD SM128E', u'APPLE SSD SM128E'] False 

可与列表理解可以用来产生符合条件的列表:

>>> [ex for ex in examples if all(any(r in s for s in ex) for r in must_have_list)] 
[[u'APPLE SSD SM128E', u'APPLE HDD HTS541010A9E662'], [u'APPLE SSD SD0128F', u'APPLE HDD ST3000DM001'], [u'APPLE HDD HTS541010A9E662', u'APPLE HDD HTS541010A9E662', u'APPLE SSD SM128E']] 

一个正则表达式是不是在这种情况下必须的,但如果测试每个字符串需要它,你可以使用re.search而非in

例如,假设你想知道的是,测试中的子独自不是站在一个单词的一部分像SSDHYBRID

for ex in examples: 
    print ex, all(any(re.search(r'\b{}\b'.format(r), s) for s in ex) for r in must_have_list) 
# same output... 
相关问题