简体   繁体   中英

looping through a list of strings and edit them with a regex condition (python)

So, I have a list like that:

lst = ['A1.', 'A1.0.', '1.', '2.', 'A2.', '1.1.', 'A3.', 'A3.0.', '1.1.1.']

And would like to iterate through each string, and if this string does not start with a (^A\d+\.) , take this pattern from the previous string item and add it to the beginning of the current one. So, the final list should look like that:

target = ['A1.', 'A1.0.', 'A1.1.', 'A1.2.', 'A2.', 'A2.1.1.', 'A3.', 'A3.0', 'A3.1.1.1.']

What is the most efficient way to achieve this without too many 'for' loops? I am quite new in Python.

Something like this?

import re

lst = ['A1.', 'A1.0.', '1.', '2.', 'A2.', '1.1.', 'A3.', 'A3.0.', '1.1.1.']
new_lst=[]
last_correct_item = ""
pattern = re.compile("(^A\d+\.)")


for item in lst:
    match = pattern.match(item)
    if match:
        last_correct_item = item[match.start():match.end()]
        new_lst.append(item)
    else:
        new_item = last_correct_item + item
        new_lst.append(new_item)

print(new_lst)
# ['A1.', 'A1.0.', 'A1.1.', 'A1.2.', 'A2.', 'A2.1.1.', 'A3.', 'A3.0.', 'A3.1.1.1.']

try:

import re 
pat2 = re.compile('(A\d+)|(^\d.*.$)')

for l in range(1,len(lst)):
    a1, val1 = re.findall(pat2, lst[l-1])[0]
    a2, val2 = re.findall(pat2, lst[l])[0]
    if not a2:
        lst[l] = f"{a1}.{lst[l]}"

lst:

['A1.',
 'A1.0.',
 'A1.1.',
 'A1.2.',
 'A2.',
 'A2.1.1.',
 'A3.',
 'A3.0.',
 'A3.1.1.1.']

explanation:

  • I am changing the original list.
  • I will take the previous list element find the prefix and if my current doesn't have one I will append the previous one prefix.

pat2 will capture

'1.0.0.' ==> [('', '1.0.0.')]

'A1.0.0.' ==> [('A1', '')]

I think one simple loop should solve your case. You basically have to keep the prefix stored in a variable, update it whenever a new one shows, or insert it at the beggining of the string when needed. A code sample would be the following:

import re

lst = ['A1.', 'A1.0.', '1.', '2.', 'A2.', '1.1.', 'A3.', 'A3.0.', '1.1.1.']
print('Before:', lst)

pattern = re.compile(r'^A\d+\.')

prefix = 'A1.'  # Default prefix
for i, s in enumerate(lst):
    match = pattern.match(s)
    if match:
        prefix = match.group()  # Update prefix to the matching pattern
    else:
        lst[i] = prefix + s  # Insert prefix into beggining of string

print('After:', lst)

One simple and easily to understand way:

lst = ['A1.', 'A1.0.', '1.', '2.', 'A2.', '1.1.', 'A3.', 'A3.0.', '1.1.1.']
latest_prefix = ''
new_lst = []
for i in lst: 
    if i.startswith('A'):
        new_lst.append(i)
        latest_prefix = i
        if latest_prefix.endswith('0.'):
            latest_prefix = latest_prefix.replace('0.','')
    else:
        i = latest_prefix + i
        new_lst.append(i)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM