簡體   English   中英

列表理解使用正則表達式條件

[英]list comprehension using regex conditional

我有一個字符串列表。 如果這些字符串中的任何一個具有4位數年份,我想在年末截斷該字符串。 否則我就把繩子單獨留下。

我試過用:

    for x in my_strings:   
      m=re.search("\D\d\d\d\d\D",x)  
      if m: x=x[:m.end()]  

我也嘗試過:

my_strings=[x[:re.search("\D\d\d\d\d\D",x).end()] if re.search("\D\d\d\d\d\D",x) for x in my_strings]  

這些都不起作用。

你能告訴我我做錯了什么嗎?

像這樣的東西似乎適用於瑣碎的數據:

>>> regex = re.compile(r'^(.*(?<=\D)\d{4}(?=\D))(.*)')                         
>>> strings = ['foo', 'bar', 'baz', 'foo 1999', 'foo 1999 never see this', 'bar 2010 n 2015', 'bar 20156 see this']
>>> [regex.sub(r'\1', s) for s in strings]
['foo', 'bar', 'baz', 'foo 1999', 'foo 1999', 'bar 2010', 'bar 20156 see this']

看起來你對結果字符串的唯一限制是在end() ,所以你應該使用re.match()代替,並將你的正則表達式修改為:

my_expr = r".*?\D\d{4}\D"

然后,在您的代碼中,執行:

regex = re.compile(my_expr)
my_new_strings = []
for string in my_strings:
    match = regex.match(string)
    if match:
        my_new_strings.append(match.group())
    else:
        my_new_strings.append(string)

或者作為列表理解

regex = re.compile(my_expr)
matches = ((regex.match(string), string) for string in my_strings)
my_new_strings = [match.group() if match else string for match, string in matches]

或者,您可以使用re.sub

regex = re.compile(r'(\D\d{4})\D')
new_strings = [regex.sub(r'\1', string) for string in my_strings]

我不完全確定你的用例,但下面的代碼可以給你一些提示:

import re

my_strings = ['abcd', 'ab12cd34', 'ab1234', 'ab1234cd', '1234cd', '123cd1234cd']

for index, string in enumerate(my_strings):
    match = re.search('\d{4}', string)
    if match:
        my_strings[index] = string[0:match.end()]

print my_strings

# ['abcd', 'ab12cd34', 'ab1234', 'ab1234', '1234', '123cd1234']

你實際上與列表理解非常接近,但你的語法是關閉的 - 你需要使第一個表達式成為“條件表達式”, x if <boolean> else y

[x[:re.search("\D\d\d\d\d\D",x).end()] if re.search("\D\d\d\d\d\D",x) else x for x in my_strings]

顯然這非常難看/難以閱讀。 有幾種更好的方法可以將字符串分成4位數年份。 如:

[re.split(r'(?<=\D\d{4})\D', x)[0] for x in my_strings]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM