[英]Given two binary lists of same length, how to find all the pair of indices corresponding to ones of alternating lists without ones in between?
[英]How to find alternating repetitive digit pair?
121426
< - 這里,1是交替的重復數字。
523563
< - 這里,NO數字是交替的重復數字。
552523
< - 這里,2和5都是交替的重復數字。
333567
< - 這里,3是交替的重復數字。
我發現re.findall(r'(?=(\\d)\\d\\1)',P)
作為編輯中的解決方案但不能理解它。
編輯 - if
條件不允許使用。
你可以在沒有使用列表解析中的zip()
的正則表達式的情況下執行此操作:
>>> s = '552523'
>>> [a for a, b in zip(s, s[2:]) if a == b]
['5', '2']
你可以使用lookaheads使用這個正則表達式:
(\d)(?=\d\1)
說明:
(\\d)
:匹配並捕獲組#1中的數字 (?=
:開始前瞻
\\d
:匹配任何數字 \\1
:對捕獲的組#1的反向引用 )
:結束先行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.