如果 x 在 RegEx 列表中

if x is in list with RegEx

我有一个如下所示的列表:

mylist = [
          u'x|freq|x:y|||cbase', 
          u'x|freq|x:y||weights_UK18|c%', 
          u'x|freq||y|weights_UK18|c%', 
          u'x|mean|x[0,0.25]:y||weights|JFP', 
          u'x|median|x[0]:y||weights_UK18|JFP_q1'
          ]

我想根据两个条件查找项目

1. if the item startswith('x|frequency||y|') 
2. and if something exists in between the 4th and 5th "|"

现在我正在循环执行此操作:

for item in mylist:
    vkey = v.split('|')
    weight = vkey[4]
    if v.startswith('x|frequency||y|') and weight!='':
        chart_data_type = 'weighted'

但是有没有一种方法可以在一行中完成此操作?

if this in mylist: 
      #blah blah 

您可以制作自己的发电机,即

def G(L):
    for item in L:
        vkey = item.split('|')
        weight = vkey[4]
        if item.startswith('x|frequency||y|') and weight!='':
            yield item

for item in G(mylist):
    print(item)

或使用列表理解(假设输入有效,因此 [4] 不会产生异常),例如,

for item in [el for el in mylist if el.startswith('x|frequency||y|') and el.split('|')[4]!='']:
    print(item)

您可以为此使用正则表达式:

import re
for item in mylist:
    if re.match('x\|frequency\|\|y\|[^|]+\|', item):
        chart_data_type = 'weighted'

但是由于 x|frequency||y| 是一个静态文本,它直接 出现在您要检查的第四部分之前,您可以通过检查来更快地完成此操作字符串:

prefix = 'x|frequency||y|'
for item in mylist:
    if item.startswith(prefix) and item[len(prefix)] != '|':
        chart_data_type = 'weighted'

这主要是检查前缀后面的字符是否为 |,在这种情况下您知道没有值。

如果坚持单行解决方案:

any(map(lambda i: i.startswith('x|freq||y|') and i.split('|')[4] != '', mylist))

如果您的列表包含至少 1 个满足条件 i.startswith('x|freq||y|') and i.split('|')[4] != ''

的项目,则上一行将 return True

解释:

  1. lambda i: i.startswith('x|freq||y|') and i.split('|')[4] != ''

是一个内联函数,用于检查您的情况。我想你很清楚我们是如何进行检查的。

  1. map 函数用于通过使用上述 lambda 函数处理列表中的每个项目来创建结果列表。通常你会传递一个函数的名字作为第一个参数,但我使用了一个 lambda(内联函数)来让它更简单。其结果将类似于:

    [假,假,真,假,假]

  2. any 如果给定列表至少包含 1 True 项,则 return 为真。