我有一个列表,它看起来像这样:如果x是列表与正则表达式
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
谢谢!决定去简单的列表理解 –