- 220 名前:デフォルトの名無しさん [2021/07/01(木) 09:12:46.98 ID:47t/afFm.net]
- string = '<001>_<9>'
size = 3 から strings = ['001_9', '002_10', '003_11'] 文字列中のタグ部分を任意の長さだけカウントアップした配列が欲しい def multiple_counting(size, string): _pat = re.compile(r'<(\d+)>') _tags = pat.findall(string) #カウント作成 _counters = [] _for tag in tags: __counters.append([str(int(tag)+1).zfill(len(tag)) for i in range(size)]) #書き換え _strings = [string]*size _for counter in counters: __strings = [pat.sub(count, string, 1) for count, string in zip(counter,strings)] _return strings なんか便利な関数とか、もっとシュッとしたやり方とかあると思うんですが 知らないので教えてください.あと再帰関数使いたかったけど挫折しました
|

|