簡體   English   中英

是否有正則表達式遞歸修改列表並在python3中更改其元素?

[英]Is there a regex to recursively modify a list and change its elements in python3?

我有以下格式的字符串列表:

["6,7",
"6-8",
"10,12",
"15-18"]

我需要將字符串拆分為單獨的元素。 如果,在那里,我只需要拆分元素。 如果-在那里,我需要生成一系列數字以包含它們之間的數字。

示例: '6,7'被拆分為['6','7']'6-8'被更改為['6','7','8']

我寫了這個功能非常適合這個:

def process_nums(verse_nums_):
     if ',' in verse_nums_:
         verse_nums = [i for i in map(str.strip,verse_nums_.split(','))]
     elif '-' in verse_nums_:
         beg_end = [int(i) for i in map(str.strip,verse_nums_.split('-'))]
         verse_nums = [i for i in range(beg_end[0],beg_end[1]+1)]
     else:
         verse_nums = [verse_nums_]
     return verse_nums

但是我被一個字符串卡住了: '6-8,10' 這應該更改為['6','7','8','10'] 我可以進行初始拆分以獲得['6-8','10']

我寫了一個關於代碼的略圓的代碼來通過:

verse_nums = process_nums('6-8,10')

        for x in verse_nums:
            if '-' in x:
                verse_nums.extend(process_nums(x))
                verse_nums.pop(verse_nums.index(x))
        verse_nums = [int(i) for i in verse_nums].sort()

有沒有更優雅的方法來做到這一點?

注意:我不確定如何在標題中正確地描述問題。 請隨意修改。

我覺得你很接近。 不需要正則表達式。 我會做的總是用逗號分割,然后新部分要么包含一個范圍,要么是單個項目。

def process_nums(nums):
  parts = nums.split(',')
  for part in parts:
    if '-' in part:
      a, b = part.split('-')
      yield from (str(i) for i in range(int(a), int(b)+1))
    else:
      yield part

print(list(process_nums('6-8,10')))

IMO 正則表達式更好,因為str.split可能無法檢測到無效輸入,例如:“,-,-2”

import re
from typing import List


def process(numbers: List[str]) -> List[str]:
    output = []
    for no_idea_what_this_is in numbers:
        for value in no_idea_what_this_is.split(","):
            match = re.fullmatch(r"(\d+)-(\d+)", value)
            if match:
                start = int(match.group(1))
                stop = int(match.group(2)) + 1
                output.extend([str(i) for i in range(start, stop)])
            elif re.fullmatch("\d+", value):
                output.append(value)
            else:
                raise ValueError(f"Unable to parse {value}")
    return output


print(process(["4-8,10"]))
# ['4', '5', '6', '7', '8', '10']

嘗試這個:

mylist = ["6,7","6-8","10,12","15-18"]
new_list = []



for i in mylist :
    if ',' in i :
        splited = i.split(',')
        new_list.append(splited[0])
        new_list.append(splited[1])
    elif '-' in i :
        splited = i.split('-')
        x = range(int(splited[0]),int(splited[1])+1)
        for y in x :
            new_list.append(str(y))

print(new_list)

輸出 :

['6', '7', '6', '7', '8', '10', '12', '15', '16', '17', '18']

我們可以對從 HYPHENATED AND COMMA SEPARETED STRING LIKE "1-5,25-30,4,5" (PYTHON RECIPE) 的 GENERATE LIST OF NUMBERS FROM 進行編碼

這種方法(過度發布)的優點是它能夠處理具有重疊的更復雜的范圍,例如:

為了:

'2,3,4-8,2-5,9'

生產

['2', '3', '4', '5', '6', '7', '8', '9'] 

雖然接受的解決方案產生

['2', ' 3', '4', '5', '6', '7', '8', '2', '3', '4', '5', ' 9']

其中有重復索引

def hyphen_range(s):
    """ Takes a range in form of "a-b" and generate a list of numbers between a and b inclusive.
    Also accepts comma separated ranges like "a-b,c-d,f" will build a list which will include
    Numbers from a to b, a to d and f"""
    s= "".join(s.split())#removes white space
    r= set()
    for x in s.split(','):
        t=x.split('-')
        if len(t) not in [1,2]: raise SyntaxError("hash_range is given its argument as "+s+" which seems not correctly formatted.")
        r.add(int(t[0])) if len(t)==1 else r.update(set(range(int(t[0]),int(t[1])+1)))
    l=list(r)
    l.sort()
    return list(map(str, l))  # added string conversion

# Test shows handling of overlapping ranges and commas in pattern
# i.e. '2, 3, 4-8, 2-5, 9'
for x in ["6,7", "6-8", "10,12", "15-18", '6-8,10', '2, 3, 4-8, 2-5, 9']:
  print(f"'{x}' -> {hyphen_range(x)}")

輸出

'6,7' -> ['6', '7']
'6-8' -> ['6', '7', '8']
'10,12' -> ['10', '12']
'15-18' -> ['15', '16', '17', '18']'6-8,10' -> ['6', '7', '8', '10']
'6-8,10' -> ['6', '7', '8', '10']
'2, 3, 4-8, 2-5, 9' -> ['2', '3', '4', '5', '6', '7', '8', '9']

發電機版本

def hyphen_range_generator(s):
    """ yield each integer from a complex range string like "1-9,12, 15-20,23"

    >>> list(hyphen_range('1-9,12, 15-20,23'))
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 15, 16, 17, 18, 19, 20, 23]

    >>> list(hyphen_range('1-9,12, 15-20,2-3-4'))
    Traceback (most recent call last):
        ...
    ValueError: format error in 2-3-4
    """
    for x in s.split(','):
        elem = x.split('-')
        if len(elem) == 1: # a number
            yield int(elem[0])
        elif len(elem) == 2: # a range inclusive
            start, end = map(int, elem)
            for i in range(start, end+1):
                yield str(i)  # only mod to posted software
        else: # more than one hyphen
            raise ValueError('format error in %s' % x)

# Need to use list(...) to see output since using generator
for x in ["6,7", "6-8", "10,12", "15-18", '6-8,10', '2, 3, 4-8, 2-5, 9']:
  print(f"'{x}' -> {list(hyphen_range_generator(x))}")

輸出

Same as the non-generator version above

暫無
暫無

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

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