How do i print the repetition output using regex it prints only first match

2024/11/15 13:43:35

I have task where I need to print the occurrence and count and non-occurrence and count

import resequence = '1222311'm = re.search(r'(\d)\1+',sequence)print(m)    

Exptected output :

(1, 1) (3, 2) (1, 3) (2, 1)

In sequence need to check and print

 (cnt , val) -> 1222311 -> 1 as come only once -> (1,1) (cnt,number)
Answer

You can use re.finditer to get start/end index of match and then construct your output (regex101):

import resequence = '1222311'out = [(m.end() - m.start(), int(m.group(1))) for m in re.finditer(r'(\d)\1*', sequence)]
print(out)

Prints:

[(1, 1), (3, 2), (1, 3), (2, 1)]
https://en.xdnf.cn/q/119047.html

Related Q&A

How do I get rid of all roles of the user discord.py

I was making my mute command and I thought of getting rid of all the members role but I dont know how to get rid of all the members roles I even tried for role in member.roles:await member.remove_roles…

How to pick the rows which contains all the keywords? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

Extract HTML Tables With Similar Data from Different Sources with Different Formatting - Python

I am trying to scrape HTML tables from two different HTML sources. Both are very similar, each table includes the same data but they may be structured differently, with different column names etc. For …

AttributeError: NoneType object has no attribute replace_with

I am getting the following error:Traceback (most recent call last):File "2.py", line 22, in <module>i.string.replace_with(i.string.replace(u\xa0, -)) AttributeError: NoneType object has…

How to expand out a Pyspark dataframe based on column?

How do I expand a dataframe based on column values? I intend to go from this dataframe:+---------+----------+----------+ |DEVICE_ID| MIN_DATE| MAX_DATE| +---------+----------+----------+ | 1|…

How can I trigger my python script to automatically run via a ping?

I wrote a script that recurses through a set of Cisco routers in a network, and gets traffic statistics. On the router itself, I have it ping to the loopback address of my host PC, after a traffic thre…

How do I make my bot delete a message when it contains a certain word?

Okay so Im trying to make a filter for my bot, but one that isnt too complicated. Ive got this:@bot.event async def on_message(ctx,message):if fuck in Message.content.lower:Message.delete()But it gives…

pyinstaller cant find package Tix

I am trying to create an executable with pyinstaller for a python script with tix from tkinter. The following script also demonstrates the error: from tkinter import * from tkinter import tixroot = ti…

form.validate_on_submit() doesnt work(nothing happen when I submit a form)

Im creating a posting blog function for social media website and Im stuck on a problem: when I click on the "Post" button(on create_post.html), nothing happens.In my blog_posts/views.py, when…

How to find determinant of matrix using python

New at python and rusty on linear Algebra. However, I am looking for guidance on the correct way to create a determinant from a matrix in python without using Numpy. Please see the snippet of code belo…